[英]How to use Vlookup in VBa?
這是我編寫的在Vba中使用Vlookup的代碼,但我不斷
運行時錯誤1004無法獲取工作表函數類的Vlookup屬性
If WorksheetFunction.IsNA(Application.WorksheetFunction.VLookup(ListBox1.Selected(0), Range("B4:C7"), 2, False)) = True Then
'Create row
Range("EndlineFM").Select
Selection.Insert Shift:=xlDown
'Initialise Detail and montant of new row
Range("TotalF").Offset(-1, 0) = FraisM.ListBox1.Selected(0)
Range("TotalF").Offset(-1, 1) = CSng(FraisM.Chiffremontant)
我該如何解決? 謝謝!
如果找不到匹配項, Application.WorksheetFunction.VLookup
將始終引發運行時錯誤-您無法使用IsNA()
處理該IsNA()
您可以在沒有WorksheetFunction
情況下這樣做:
Dim m
m = Application.VLookup(ListBox1.Selected(0), Range("B4:C7"), 2, False))
If IsError(m) Then
'Create row
Range("EndlineFM").Insert Shift:=xlDown
'Initialise Detail and montant of new row
Range("TotalF").Offset(-1, 0) = FraisM.ListBox1.Selected(0)
Range("TotalF").Offset(-1, 1) = CSng(FraisM.Chiffremontant)
'etc
像這樣工作
Dim Txt As Variant
On Error Resume Next
Txt = Application.WorksheetFunction.VLookup(FraisM.ListBox1.List(FraisM.ListBox1.ListIndex), Range("B4:C7"), 2, False)
If Err.Number <> 0 Then
MsgBox " Not found" ''optional, no need to do anything
Err.Clear
Exit Sub ' Or may use Goto label
End If
On Error GoTo 0
'Create row
Range("EndlineFM").Select
Selection.Insert Shift:=xlDown
'Initialise Detail and montant of new row
Range("TotalF").Offset(-1, 0) = Txt
我已經將FraisM
用作用戶名,並假設代碼是從Module運行的。 如果您在工作表上使用Msform.ListBox
,則可以嘗試
Dim MyLB As Object
Set MyLB = Sheet1.Shapes("List Box 1")
Txt = Application.WorksheetFunction.VLookup(MyLB.ControlFormat.List(MyLB.ControlFormat.ListIndex), Range("B4:C7"), 2, False)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.