如何在 VBA 中存储和写入 Vlookup 结果
How to store and write the Vlookup results in VBA
我对 VBA 中的 Vlookup 很生气!
设想:
我有两张纸:MySheet 和 OtherSheet。
查找值:MySheet 中的列 ID_Consegna
搜索范围:OtherSheet 中的矩阵
索引:6 和 7(所以我需要获取两个值)
然后我必须将这些从公式中获得的值写在 MySheet 的 G 和 H 列中。
到目前为止我完成的代码是:
Sub cerca_vert()
'definisco il foglio attivo
Dim mySheet As Worksheet
Set mySheet = Worksheets("data")
Set otherSheet = Worksheets("data (2)")
mySheet.Activate
'definisco la colonna ID_Consegna
Dim cella_indirizzo As Variant
Dim lLastRow As Long
lLastRow = mySheet.Cells(mySheet.Rows.Count, 6).End(xlUp).Row
Dim ID_Consegna As Variant
ID_Consegna = mySheet.Range("F2:F" & lLastRow)
'definisco la matrice di ricerca nel secondo foglio "data (2)"
Dim val1_cap As Integer
Dim val2_citta As Variant
Dim lLastRow_matrice As Long
lLastRow_matrice = otherSheet.Cells(otherSheet.Rows.Count, 4).End(xlUp).Row
Dim matrice As Variant
matrice = otherSheet.Range("D2:I" & lLastRow)
For Each cella_indirizzo In ID_Consegna
On Error Resume Next
val1 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 6, False)
val2 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 7, False).Value
Next cella_indirizzo
End Sub
我的问题是:
- 为什么 val1 和 val2 仍然是空的?
- 我怎么能"fill"他们?
我怎么可能"fill"他们?
非常感谢任何愿意帮助我的人!
如果您希望从 F 列和 G 列中获得结果,那么
Sub cerca_vert()
'definisco il foglio attivo
Dim mySheet As Worksheet, OtherSheet As Worksheet
Dim cella_indirizzo As Range ' in loop
Dim lLastRow As Long
Dim ID_Consegna As Range
Dim val1, val2
Dim lLastRow_matrice As Long
Dim matrice As Range
Set mySheet = Worksheets("data")
Set OtherSheet = Worksheets("data (2)")
'definisco la colonna ID_Consegna
lLastRow = mySheet.Cells(mySheet.Rows.Count, 6).End(xlUp).Row
Set ID_Consegna = mySheet.Range("F2:F" & lLastRow)
lLastRow_matrice = OtherSheet.Cells(OtherSheet.Rows.Count, 4).End(xlUp).Row
Set matrice = OtherSheet.Range("D2:I" & lLastRow)
'definisco la matrice di ricerca nel secondo foglio "data (2)"
For Each cella_indirizzo In ID_Consegna.Cells
On Error Resume Next
val1 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 3, False)
val2 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 4, False)
cella_indirizzo.Offset(, 1) = val1
cella_indirizzo.Offset(, 2) = val2
Next cella_indirizzo
End Sub
我对 VBA 中的 Vlookup 很生气! 设想: 我有两张纸:MySheet 和 OtherSheet。 查找值:MySheet 中的列 ID_Consegna 搜索范围:OtherSheet 中的矩阵 索引:6 和 7(所以我需要获取两个值)
然后我必须将这些从公式中获得的值写在 MySheet 的 G 和 H 列中。
到目前为止我完成的代码是:
Sub cerca_vert()
'definisco il foglio attivo
Dim mySheet As Worksheet
Set mySheet = Worksheets("data")
Set otherSheet = Worksheets("data (2)")
mySheet.Activate
'definisco la colonna ID_Consegna
Dim cella_indirizzo As Variant
Dim lLastRow As Long
lLastRow = mySheet.Cells(mySheet.Rows.Count, 6).End(xlUp).Row
Dim ID_Consegna As Variant
ID_Consegna = mySheet.Range("F2:F" & lLastRow)
'definisco la matrice di ricerca nel secondo foglio "data (2)"
Dim val1_cap As Integer
Dim val2_citta As Variant
Dim lLastRow_matrice As Long
lLastRow_matrice = otherSheet.Cells(otherSheet.Rows.Count, 4).End(xlUp).Row
Dim matrice As Variant
matrice = otherSheet.Range("D2:I" & lLastRow)
For Each cella_indirizzo In ID_Consegna
On Error Resume Next
val1 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 6, False)
val2 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 7, False).Value
Next cella_indirizzo
End Sub
我的问题是:
- 为什么 val1 和 val2 仍然是空的?
- 我怎么能"fill"他们?
我怎么可能"fill"他们?
非常感谢任何愿意帮助我的人!
如果您希望从 F 列和 G 列中获得结果,那么
Sub cerca_vert()
'definisco il foglio attivo
Dim mySheet As Worksheet, OtherSheet As Worksheet
Dim cella_indirizzo As Range ' in loop
Dim lLastRow As Long
Dim ID_Consegna As Range
Dim val1, val2
Dim lLastRow_matrice As Long
Dim matrice As Range
Set mySheet = Worksheets("data")
Set OtherSheet = Worksheets("data (2)")
'definisco la colonna ID_Consegna
lLastRow = mySheet.Cells(mySheet.Rows.Count, 6).End(xlUp).Row
Set ID_Consegna = mySheet.Range("F2:F" & lLastRow)
lLastRow_matrice = OtherSheet.Cells(OtherSheet.Rows.Count, 4).End(xlUp).Row
Set matrice = OtherSheet.Range("D2:I" & lLastRow)
'definisco la matrice di ricerca nel secondo foglio "data (2)"
For Each cella_indirizzo In ID_Consegna.Cells
On Error Resume Next
val1 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 3, False)
val2 = Application.WorksheetFunction.VLookup(CStr(cella_indirizzo), matrice, 4, False)
cella_indirizzo.Offset(, 1) = val1
cella_indirizzo.Offset(, 2) = val2
Next cella_indirizzo
End Sub