这是我正在尝试做的事情:
我在A:A中有2000个订单号,例如:
125787
358946
358961
我有2000个字符串,包括B:B中的这些订单号,例如:
12542-MARLBORO-125787
19009-BRYN ATHYN-358946
21037-EDGEWATER-358961
我在C:C中有3000个人名,与B列中列出的城市相关联,例如:
Frank Smith - MARLBORO
John Park - BRYN ATHYN
Kevin Decker - EDGEWATER
我想匹配/找到B:B中A:A的订单号,并返回与该城市相关联的人名(在C:C中),并将名称放在新列D:D中。我希望这有道理......
答案 0 :(得分:1)
或者只是一个公式。在D1中,输入
=IF(A1<>"",INDEX(C:C,MATCH(CONCATENATE("*",A1),B:B,0)),"")
答案 1 :(得分:0)
这是一个按钮点击事件,可以满足您的需求。您需要为adodb记录集添加引用。在VBA IDE中,转到工具下拉菜单 - 参考。选择“Microsoft ActiveX数据对象2.8库”。
Private Sub CommandButton5_Click()
Dim rs As New ADODB.Recordset
Dim ws As Excel.Worksheet
Dim lRow As Long
Dim strCity As String
Dim strName As String
Dim iIndex As Integer
Set ws = Application.ActiveSheet
'Add fields to your recordset for storing data. You can store sums here.
With rs
.Fields.Append "Row", adChar, 20
.Fields.Append "ColumnB", adChar, 70
.Fields.Append "ColumnC", adChar, 70
.Open
End With
lRow = 1
'Loop through and record what is in the columns we want to look at
Do While lRow <= ws.UsedRange.Rows.Count
rs.AddNew
rs.Fields("Row").Value = lRow
rs.Fields("ColumnB").Value = ws.Range("B" & lRow).Value
rs.Fields("ColumnC").Value = ws.Range("C" & lRow).Value
rs.Update
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
If rs.EOF = False Then
rs.MoveFirst
End If
'Now go through and check the values of the second column against what we recorded from the first
lRow = 1
Do While lRow <= ws.UsedRange.Rows.Count
'Find the record with this order number.
rs.Filter = ""
rs.Filter = "ColumnB Like '%" & ws.Range("A" & lRow).Value & "%'"
If rs.RecordCount > 0 Then
strCity = rs.Fields("ColumnC").Value
iIndex = 0
iIndex = InStr(strCity, "-")
If iIndex <> 0 Then
strCity = Right(strCity, Len(strCity) - iIndex)
End If
iIndex = 0
iIndex = InStr(strCity, "-")
If iIndex <> 0 Then
strCity = Left(strCity, iIndex)
End If
'Now find the record with that name
rs.Filter = ""
rs.Filter = "ColumnC Like '%" & Trim(strCity) & "%'"
If rs.RecordCount > 0 Then
strName = ws.Range("C" & lRow).Value
iIndex = 0
iIndex = InStr(strName, "-")
if iIndex > 0 then
ws.Range("D" & lRow).Value = Trim(Left(strName, iIndex - 1))
else
ws.Range("D" & lRow).Value = "not found"
end if
End If
End If
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
End Sub
根据海报对所需内容的评论。让我们看看这是否有效。
Private Sub CommandButton5_Click()
Dim rs As New ADODB.Recordset
Dim ws As Excel.Worksheet
Dim lRow As Long
Dim strCity As String
Dim strName As String
Dim iIndex As Integer
Set ws = Application.ActiveSheet
'Add fields to your recordset for storing data. You can store sums here.
With rs
.Fields.Append "Row", adChar, 20
.Fields.Append "ColumnB", adChar, 70
.Fields.Append "ColumnC", adChar, 70
.Open
End With
lRow = 1
'Loop through and record what is in the columns we want to look at
Do While lRow <= ws.UsedRange.Rows.Count
rs.AddNew
rs.Fields("Row").Value = lRow
rs.Fields("ColumnB").Value = ws.Range("B" & lRow).Value
rs.Fields("ColumnC").Value = ws.Range("C" & lRow).Value
rs.Update
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
If rs.EOF = False Then
rs.MoveFirst
End If
'Now go through and check the values of the second column against what we recorded from the first
lRow = 1
Do While lRow <= ws.UsedRange.Rows.Count
'Find the record with this order number.
rs.Filter = ""
rs.Filter = "ColumnB Like '%" & ws.Range("A" & lRow).Value & "%'"
If rs.RecordCount > 0 Then
ws.Range("D" & lRow).Value = rs.Fields("ColumnC").Value
End If
lRow = lRow + 1
ws.Range("A" & lRow).Activate
Loop
End Sub