首页 文章

从Sheet1读入CustomerID,在工作表2上找到CustomerID,将CustomerName复制/粘贴到Sheet1

提问于
浏览
0

我很擅长excel,所以请耐心等待 .

在我的工作簿的Sheet1上,我有像CostumerID,CostumerName,Email等数据在A-G列中传播 . CostumerID填充ID,CostumerName为空 .

在我的工作簿的Sheet2上,我有2列(A和B) . A列包含CustomerID,B列包含该CustomerID的正确CustomerName .

我的问题是:

我需要一个转到Sheet1的宏,读取A列中的CustomerID,然后转到Sheet2,在A列中找到CustomerID,在B列中获取CustomerName并将该名称粘贴到Sheet1中靠近Cell的列B中得到了第一个CustomerID .

进一步说明:

步骤1:获取工作表1上A列中的单元格值

第2步:转到Sheet2,在A列中找到该值,然后从同一行复制CustomerName .

步骤3:将CostumerName粘贴到与B上的A列上的原始值相同的行中的B列中 .

Sheet1可以是100-6000行,它需要检查Sheet1中A列中的所有CustomerID并找到相应的CustomerName . Sheet1上的列A中可能存在重复项,但列D,F和G的值都是唯一的 .

通常我会尝试如何做到这一点,但我不知道如何做到这一点 .

有人有想法吗?

任何帮助将不胜感激!

1 回答

  • 0

    " Mark Wickett " 已正确指出 . 但是,如果你仍然想要去vba而不是这里 .

    这里说的是你的第一张纸的截图
    enter image description here

    这是你的第二张纸的截图

    enter image description here

    这是守则

    Dim rngCust As Range
    Dim rNo As Long
    Dim strCust As String
    Dim rngSearch As Range
    Dim rngFound As Range
    Dim intFound As Long
    
    Application.ScreenUpdating = False
    
    Set rngCust = Sheets(1).Range(Cells(2, 1), Cells(2, 1).End(xlDown))
        For Each cell In rngCust.Cells
            rNo = cell.Row
            strCust = Cells(rNo, 1).Value
            Sheets(2).Select
                Set rngSearch = Sheets(2).Range(Cells(2, 1), Cells(2, 1).End(xlDown))
                Set rngFound = rngSearch.Find(What:=strCust, Lookat:=xlWhole, _
                               SearchOrder:=xlByRows, searchdirection:=xlNext, _
                               MatchCase:=False)
                If Not rngFound Is Nothing Then
                    intFound = rngFound.Row
                End If
            Sheets(1).Select
            Sheets(1).Cells(rNo, 2).Value = Sheets(2).Cells(intFound, 2).Value
    
        Next
        Application.ScreenUpdating = True
    

    here is the Output

    enter image description here

相关问题