我需要知道A级轿车的长度是否在适当的长度范围内。我希望通过VBA对VLookup进行表,该表在每个类中具有最大和最小长度值。我的代码,其中第23列=长度,第16列=类,第101列=返回它是否在正确的范围内。
Dim ClassCodeTable As Range
Set ClassCodeTable = Worksheets("Class Code Tables").Range("A:G")
Range("A1").Select
Lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To Lastrow
If Cells(i, 23).Value > Application.WorksheetFunction.VLookup(Cells(i, 16), ClassCodeTable, 3, False) And Cells(i, 23).Value < Application.WorksheetFunction.VLookup(Cells(i, 16), ClassCodeTable, 4, False) Then
Cells(i, 101).Value = ""
Else
Cells(i, 101).Value = "Length Range Error"
End If
Next
此代码返回1004错误,无法获取WorksheetFunction类的VLookup属性。但这种方法适用于我在VLookup中是否具有特征C 的A类汽车等于 ClassCodeTable中Class A的预期分类值。在我看来,返回的VLookup值不会被存储为数字,即使查找表中的所有值都是数字。
有没有办法将VLookup值转换为可以根据不等式检查的数字?
答案 0 :(得分:1)
这是另一种做同样事情的方法,不需要循环。我根据您描述的设置对其进行了测试,并为我成功运行:
Sub tgr()
With Range("CW2:CW" & Cells(Rows.Count, "P").End(xlUp).Row)
.Formula = "=IF(COUNTIF('Class Code Tables'!A:A,P2)=0,""Class not found""," & _
"IF(AND(W2>=VLOOKUP(P2,'Class Code Tables'!A:G,3,FALSE)," & _
"W2<=VLOOKUP(P2,'Class Code Tables'!A:G,4,FALSE))," & _
""""",""Length Range Error""))"
.Value = .Value
End With
End Sub
或者,如果您希望将公式全部放在一行:
.Formula = "=IF(COUNTIF('Class Code Tables'!A:A,P2)=0,""Class not found"",IF(AND(W2>=VLOOKUP(P2,'Class Code Tables'!A:G,3,FALSE),W2<=VLOOKUP(P2,'Class Code Tables'!A:G,4,FALSE)),"""",""Length Range Error""))"