Application.V查看格式
在VBA中的Application.VlookUp有问题。
我正在运行下面的代码来尝试获得等价
Dim hh As Variant Dim hi As Variant hh = Range("A" & hd) hi = Range("B" & hd) If hi = Application.VLookup(hh, Worksheets("Equivalency Table").Range("A1:D20"), 2, False) Then If Not IsError(hi) Then Range("A" & hd & ":B" & hd).Select Selection.Style = "Good" hf = hf + 1 Else Range("A" & hd & ":B" & hd).Select Selection.Style = "Bad" End If
高清是一个增量variables
当我运行它时,程序应该检查等价表,如果在列A中find值“hh”,那么它应该返回列D中的值。然后程序检查“hi”中的值是否等于由VLookUp返回的值。
但是,当我运行该程序时,出现以下错误:
运行时错误:13types不匹配
我试图将“hi”的DIM改为String,Long和Integer,但没有运气。 “hh”的值可以是字母,数字或组合,而“hi”将始终是数字。
编辑:
在更多地处理文件之后,错误现在似乎只是在VLookUp在等同表的A列中找不到“hh”的值
如果VLOOKUP
找不到该值并返回错误,则该命令:
hi = Application.VLookup(hh, Worksheets("Equivalency Table").Range("A1:D20"), 2, False)
试图比较原始值与错误值,这会导致运行时错误13:types不匹配 。
为了避免这个问题,我build议你引入另一个variablesVLookupResult
。 首先将VLOOKUP
函数的结果分配给这个variables,并在比较之前检查它是否没有错误。
以下是这些更改的代码:
Sub x() Dim hh As Variant Dim hi As Variant Dim VLookupResult As Variant hh = Range("A" & hd) hi = Range("B" & hd) VLookupResult = Application.VLookup(hh, Worksheets("Equivalency Table").Range("A1:D20"), 2, False) If IsError(h1) Then Range("A" & hd & ":B" & hd).Select Selection.Style = "Bad" ElseIf hi = VLookupResult Then Range("A" & hd & ":B" & hd).Select Selection.Style = "Good" hf = hf + 1 End If End Sub