简体   繁体   English

从Sheet1中读入CustomerID,在Sheet 2上找到CustomerID,将CustomerName复制/粘贴到Sheet1

[英]Read-in CustomerID from Sheet1, find CustomerID on Sheet 2, copy/paste CustomerName to Sheet1

I'm quite new at excel, so please bear with me. 我是excel的新手,所以请多多包涵。

On Sheet1 of my workbook I have data like CostumerID, CostumerName, Email and so on spread in columns AG. 在我的工作簿的Sheet1上,我的数据如CostumerID,CosumerName,Email等分布在AG列中。 CostumerID is filled with ID's and CostumerName is empty. CostumerID填充为ID,而CostumerName为空。

On Sheet2 of my workbook I have 2 columns (A and B). 在工作簿的Sheet2上,我有2列(A和B)。 Column A contains CustomerID and column B contains the correct CustomerName for that CustomerID. 列A包含客户ID,列B包含该客户ID的正确客户名。

My problem is: 我的问题是:

I need a macro that goes to Sheet1, reads the CustomerID in column A, then goes to Sheet2, find that CustomerID also in column A, get the CustomerName in column B and paste that name to column B in Sheet1 next to cell from which it got the first CustomerID. 我需要一个宏,进入Sheet1,读取A列中的CustomerID,然后进入Sheet2,在A列中也找到CustomerID,在B列中获得CustomerName,并将该名称粘贴到Sheet1中的B列,从中将其粘贴到该单元格旁边获得了第一个客户ID。

Further ellaboration: 进一步ellaboration:

Step 1: Get value of cell in column A on Sheet 1 步骤1:在工作表1的A列中获取单元格的值

Step 2: Go to Sheet2, find that value in column A, then copy CustomerName from same row. 步骤2:转到Sheet2,在A列中找到该值,然后从同一行复制CustomerName。

Step 3: Paste that CostumerName into column B on the same row as original value from column A on Sheet1. 步骤3:将该CostumerName粘贴到Sheet1上A列原始值所在的同一行的B列中。

Sheet1 can be 100-6000 rows, and it needs to check all CustomerID's in column A in Sheet1 and find the corresponding CustomerName. Sheet1可以是100-6000行,并且它需要检查Sheet1的A列中的所有CustomerID,并找到相应的CustomerName。 There can be duplicates in column A on Sheet1, but the value of columns D, F and G are all unique. 在Sheet1的A列中可以有重复项,但是D,F和G列的值都是唯一的。

Normally I would present my attempt on how to do this, but I have no idea how to do this. 通常,我会提出如何执行此操作的尝试,但是我不知道如何执行此操作。

Anybody have an idea? 有人有主意吗?

Any help would be greatly appreciated! 任何帮助将不胜感激!

" Mark Wickett " has correctly pointed it out. “ Mark Wickett”已正确指出。 But if you still want to go to the vba than here it is. 但是,如果您仍然想比这里去vba。

say here is the screenshot of your first sheet 说这是第一张纸的屏幕截图 在此处输入图片说明

and here is screenshot of your second sheet 这是第二张纸的屏幕截图

在此处输入图片说明

Here is the Code 这是代码

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 这是输出

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM