简体   繁体   中英

VBA on excel - using the result of a vlookup as criteria for a match

I'm using vlookup to find a match from table1 in table 2. I now want to use the result of the lookup to find the row number of the that particular string.

Private Sub CommandButton1_Click()
On Error Resume Next
Dim BPP_Row As Long
Dim BPP_Clm As Long
Dim result As String
Dim num As String
Table1 = Sheet1.Range("B6:C44")
Table2 = Sheet3.Range("B6:B147")
Table3 = Sheet1.Range("C1:C44")
BPP_Row = Sheet3.Range("D6").Row
BPP_Clm = Sheet3.Range("D6").Column
For Each cl In Table2
 result = Application.WorksheetFunction.VLookup(cl, Table1, 2, False)
 num = Application.Match(""" + result + """, Table3, 0)
 Sheet3.Cells(BPP_Row, BPP_Clm).Formula = "=""" + result + """ + """ + num + """"
 BPP_Row = BPP_Row + 1
 result = ""
 num = ""
Next cl
MsgBox "done"
End Sub

Managed to fix it.

Private Sub CommandButton1_Click()
On Error Resume Next
Dim BPP_Row As Long
Dim BPP_Clm As Long
Dim result As String
Dim num As String
Table1 = Sheet1.Range("B6:C44")
Table2 = Sheet3.Range("B6:B147")
Table3 = Sheet1.Range("C1:C44")
BPP_Row = Sheet3.Range("D6").Row
BPP_Clm = Sheet3.Range("D6").Column
For Each cl In Table2
 result = Application.WorksheetFunction.VLookup(cl, Table1, 2, False)
 num = Application.WorksheetFunction.Match(result, Table3, 0)
 Sheet3.Cells(BPP_Row, BPP_Clm).Formula = "=""" + result + """ + " + num + ""
 BPP_Row = BPP_Row + 1
 result = ""
 num = ""
Next cl
MsgBox "done"
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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