VBA 跨多个工作表的 Vlookup
VBA Vlookup across multiple sheets
所以我有两个列表,每个列表都在不同的 sheet 中。我正在检查 Sheet B 中的值也在 Sheet A 中。我正在为此使用 VLookup,问题似乎与范围语句有关,因为此范围似乎是 'empty'.
我的 VBA 尝试是这样的,
Dim lookupVal As String
Dim myString As String
For i = 1 to N
lookupVal = Sheets("b").Cells(1 + i, 2)
myString = Application.WorksheetFunction.VLookup(lookupVal, Sheets("a").range(Sheets("a").Cells(9,3), Sheets("a").Cells(N+8, 3)), 1, False)
If IsEmpty(myString) Then
Sheets("b").Cells(1+i, 3) = ""
Else
Sheets("b").Cells(1+i, 3) = myString
End if
Next i
我得到 'Run-time 1004: Application-defined or object-defined error'。任何帮助表示赞赏。
将您的代码更改为以下内容。
If IsEmpty(myString)
是错误的做法。在 #N/A
的情况下,该语句仍然为真。
这是你正在尝试的吗?
Sub Sample()
Dim lookupVal As String
Dim myString As Variant
Dim rng As Range
'~~> Change this whatever you want
n = 5
For i = 1 To n
lookupVal = Sheets("b").Cells(1 + i, 2)
Set rng = Sheets("a").Range(Sheets("a").Cells(9, 3), Sheets("a").Cells(n + 8, 3))
myString = Application.Evaluate("=VLOOKUP(" & lookupVal & "," & "a!" & rng.Address & ",1,0)")
Select Case CVErr(myString)
Case CVErr(xlErrName), CVErr(xlErrNA), CVErr(xlErrRef), CVErr(xlErrValue)
Case Else: Sheets("b").Cells(1 + i, 3) = myString
End Select
Next i
End Sub
你的代码有两个问题:
对于worksheetfunction.vlookup
搜索范围不能变化。
所以解决这个问题的方法是使用额外的变量来永久
如果worksheetfunction.vlookup
找不到搜索值,则会出现错误,在这种情况下,您需要使用额外的操作和错误处理
lookupVal
必须声明 as Range
由于单元格的格式(查找范围和查找值)可能不同,但在您的代码单元格中,值始终会被转换转成string类型,转成string
就找不到数字了
由于与“3”中所述相同的原因,myString
也需要声明 as Variant
。例如,单元格的类型可以是 double
,但您的代码会将其转换为 string
所以,您的更新代码如下,工作正常
Sub test()
Dim lookupVal As Range, myString As Variant, Rng$, n&
n = Sheets("b").[B:B].Cells.Find("*", , , , xlByRows, xlPrevious).Row
On Error Resume Next
For i = 1 To n
Set lookupVal = Sheets("b").Cells(1 + i, 2)
Rng = Range(Cells(9, 3), Cells(n + 8, 3)).Address
myString = WorksheetFunction.VLookup(lookupVal, Sheets("a").Range(Rng), 1, False)
If Err.Number > 0 Then
Sheets("b").Cells(1 + i, 3) = ""
Err.Clear
Else
Sheets("b").Cells(1 + i, 3) = myString
End If
Next i
End Sub
下面的替代方法
Sub test()
Dim cl As Range, Dic As Object
Set Dic = CreateObject("Scripting.Dictionary"): Dic.Comparemode = vbTextCompare
With Sheets("a")
For Each cl In .Range("C9:C" & .Cells(Rows.Count, "C").End(xlUp).Row)
If Not Dic.exists(cl.Value) Then Dic.Add cl.Value, cl.Row
Next cl
End With
With Sheets("b")
For Each cl In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row)
If Dic.exists(cl.Value) Then cl.Offset(, 1).Value = cl.Value
Next cl
End With
Set Dic = Nothing
End Sub
所以我有两个列表,每个列表都在不同的 sheet 中。我正在检查 Sheet B 中的值也在 Sheet A 中。我正在为此使用 VLookup,问题似乎与范围语句有关,因为此范围似乎是 'empty'.
我的 VBA 尝试是这样的,
Dim lookupVal As String
Dim myString As String
For i = 1 to N
lookupVal = Sheets("b").Cells(1 + i, 2)
myString = Application.WorksheetFunction.VLookup(lookupVal, Sheets("a").range(Sheets("a").Cells(9,3), Sheets("a").Cells(N+8, 3)), 1, False)
If IsEmpty(myString) Then
Sheets("b").Cells(1+i, 3) = ""
Else
Sheets("b").Cells(1+i, 3) = myString
End if
Next i
我得到 'Run-time 1004: Application-defined or object-defined error'。任何帮助表示赞赏。
将您的代码更改为以下内容。
If IsEmpty(myString)
是错误的做法。在 #N/A
的情况下,该语句仍然为真。
这是你正在尝试的吗?
Sub Sample()
Dim lookupVal As String
Dim myString As Variant
Dim rng As Range
'~~> Change this whatever you want
n = 5
For i = 1 To n
lookupVal = Sheets("b").Cells(1 + i, 2)
Set rng = Sheets("a").Range(Sheets("a").Cells(9, 3), Sheets("a").Cells(n + 8, 3))
myString = Application.Evaluate("=VLOOKUP(" & lookupVal & "," & "a!" & rng.Address & ",1,0)")
Select Case CVErr(myString)
Case CVErr(xlErrName), CVErr(xlErrNA), CVErr(xlErrRef), CVErr(xlErrValue)
Case Else: Sheets("b").Cells(1 + i, 3) = myString
End Select
Next i
End Sub
你的代码有两个问题:
对于
worksheetfunction.vlookup
搜索范围不能变化。 所以解决这个问题的方法是使用额外的变量来永久如果
worksheetfunction.vlookup
找不到搜索值,则会出现错误,在这种情况下,您需要使用额外的操作和错误处理lookupVal
必须声明as Range
由于单元格的格式(查找范围和查找值)可能不同,但在您的代码单元格中,值始终会被转换转成string类型,转成string 就找不到数字了
由于与“3”中所述相同的原因,myString
也需要声明as Variant
。例如,单元格的类型可以是double
,但您的代码会将其转换为string
所以,您的更新代码如下,工作正常
Sub test()
Dim lookupVal As Range, myString As Variant, Rng$, n&
n = Sheets("b").[B:B].Cells.Find("*", , , , xlByRows, xlPrevious).Row
On Error Resume Next
For i = 1 To n
Set lookupVal = Sheets("b").Cells(1 + i, 2)
Rng = Range(Cells(9, 3), Cells(n + 8, 3)).Address
myString = WorksheetFunction.VLookup(lookupVal, Sheets("a").Range(Rng), 1, False)
If Err.Number > 0 Then
Sheets("b").Cells(1 + i, 3) = ""
Err.Clear
Else
Sheets("b").Cells(1 + i, 3) = myString
End If
Next i
End Sub
下面的替代方法
Sub test()
Dim cl As Range, Dic As Object
Set Dic = CreateObject("Scripting.Dictionary"): Dic.Comparemode = vbTextCompare
With Sheets("a")
For Each cl In .Range("C9:C" & .Cells(Rows.Count, "C").End(xlUp).Row)
If Not Dic.exists(cl.Value) Then Dic.Add cl.Value, cl.Row
Next cl
End With
With Sheets("b")
For Each cl In .Range("B2:B" & .Cells(Rows.Count, "B").End(xlUp).Row)
If Dic.exists(cl.Value) Then cl.Offset(, 1).Value = cl.Value
Next cl
End With
Set Dic = Nothing
End Sub