我在excel VBA中面临一个问题。上面说是unable to get the vlookup property of the WorksheetFunction class
Private Sub cmb_Product_Change()
Dim sh As Worksheet
Set sh = ThisWorkbook.Sheets("Product_Master")
If Me.cmb_Product.Value = "" Or Me.cmb_Type.Value = "" Then Me.txt_Rate.Value = ""
emphasized text[enter image description here][1]
If Me.cmb_Type.Value = "Sale" Then
Me.txt_Rate.Value = Application.WorksheetFunction.VLookup(Me.cmb_Product, sh.Range("B:D"), 2, 0)
ElseIf Me.cmb_Type.Value = "Purchase" Then
Me.txt_Rate.Value = Application.WorksheetFunction.VLookup(Me.cmb_Product, sh.Range("B:D"), 3, 0)
End If
End Sub
发布于 2021-02-16 11:12:14
如果找不到查找值,这就是WorksheetFunction.VLookup
的正常行为。
你有两种可能性:
(a)忽略错误
Dim rate As Double ' Or String, not sure what your data is.
On Error Resume Next
rate = WorksheetFunction.VLookup(Me.cmb_Product, sh.Range("B:D"), 2, false)
On Error Goto 0
Me.txt_Rate.Value = rate
如果找不到您的产品,费率将为空。
(b)改用Application.VLookup
。这将不会引发运行时错误,但如果没有找到匹配,它将返回一个错误作为结果。请注意,您需要将结果声明为变量,否则将得到一个Type不匹配错误(错误13)。
Dim rate As Variant
rate = Application.VLookup(Me.cmb_Product, sh.Range("B:D"), 2, false)
if not isError(rate) then Me.txt_Rate.Value = rate
更新:有关详细信息,请参阅本文:https://rubberduckvba.wordpress.com/2021/02/15/worksheetfunction-and-errors/
https://stackoverflow.com/questions/66223069
复制相似问题