设置一个Cust_Num
在列表中包含所有 ' 的工作表,并在Company Name
其右侧对应。如下图所示:
然后,一旦您完成所有设置,请使用VLOOKUP
excel 中的函数。这是我的示例公式:
=VLOOKUP(A3,LookUp!$A$1:$B$9,2,FALSE)
VLOOKUP的A3
第一部分是对Cust_Num
行中的单元格引用。
Is 数组保存您的LookUp!$A$1:$B$9
查找值。
2
会将结果定向VLOOKUP
到第二列,在这种情况下Company Name
是您手动输入的。
并且FALSE
它只会返回完全匹配。
然后我只需要把这个公式拖下来,我就得到了这个:
*注意:*如果您需要帮助获取列表中的每个Cust_Num
列表,您可以执行以下操作:
选择所有的列表,Cust_Num
然后在数据选项卡的功能区中,选择高级过滤器:
然后在窗口中:
- 选择
Copy To Another Location
- 输入
Copy To
范围作为查找表
- 确保选中复选框
Unique Records Only
然后你可以只用这些值填写相应的Company Names
一次并使用VLOOKUP
前面描述的。
如果首选 Find 方法,您可以使用此方法:
Sub AddAllNames()
Call AddCompanyNameNextToCust_Num("37004", "Varco")
Call AddCompanyNameNextToCust_Num("44278", "Varco")
Call AddCompanyNameNextToCust_Num("44318", "Varco")
Call AddCompanyNameNextToCust_Num("12345", "Name1")
Call AddCompanyNameNextToCust_Num("12344", "Name1")
Call AddCompanyNameNextToCust_Num("12346", "Name1")
Call AddCompanyNameNextToCust_Num("98765", "Name2")
Call AddCompanyNameNextToCust_Num("56789", "Name2")
Call AddCompanyNameNextToCust_Num("89756", "Name2")
End Sub
Function AddCompanyNameNextToCust_Num(strCust_Num As Variant, strCompanyName As String)
Dim rngCust_nums As Range, rngFoundCell As Range, rngFirstCellFound As Range
Dim rngCust_NumsColumn As Long
Dim boolFinished As Boolean
'Get Column With header "cust_num"
rngCust_NumsColumn = WorksheetFunction.Match("cust_num", Rows(1), 0)
'Set The Search range to column from last line
Set rngCust_nums = ActiveSheet.Columns(rngCust_NumsColumn)
'Get the first matching value of Cust_Num (passed into sub)
Set rngFoundCell = rngCust_nums.Find(What:=strCust_Num, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'Check to make sure a match was found/ "Not Nothing"
If Not rngFoundCell Is Nothing Then
'Save the First Found Cell
Set rngFirstCellFound = rngFoundCell
'Add Company Name One Column to the Right of First Found Cust_Num
rngFoundCell.Offset(, 1).Value = strCompanyName
'Start Looping a "FindNext"
Do While boolFinished = False
'Set each new match into an overwriting Variable
Set rngFoundCell = rngCust_nums.FindNext(After:=rngFoundCell)
'Make sure the match is "Something"
If Not rngFoundCell Is Nothing Then
'Make sure We have not gone through the whole list and that we
'are not back to the begining
If rngFoundCell.Address = rngFirstCellFound.Address Then Exit Do
'If a new match is found and is not the starting point then add
'the company name in the next column
rngFoundCell.Offset(, 1).Value = strCompanyName
Else
'When nothing is Found End loop
boolFinished = True
End If
Loop
Else 'If not even one match was found
MsgBox strCust_Num & " not Found"
End If
End Function