gpt4 book ai didi

excel - 错误 "Unable to get the VLookup property of the WorksheetFunction class"背后的原因可能是什么?

转载 作者:行者123 更新时间:2023-12-04 21:49:46 24 4
gpt4 key购买 nike

mySValue = InputBox("Enter the no. of rows to be considered")
Set myrange = Worksheets("UniqueSupNames").Range("A:B")

For counterSVar = 3 To mySValue
SupName = Range("A" & counterSVar)
SupId = Application.WorksheetFunction.VLookup(SupName, Range("myrange"), 2, False)
Range("K" & counterSVar).Value = SupId
Next counterSVar

以上是我的代码。请提出解决方案。
仅供引用 - 我创建了一个名为“UniqueSupNames”的工作表(在同一个工作簿中),其中包含两列(A 和 B),分别包含供应商名称和供应商 ID。

最佳答案

Application.WorksheetFunction.VLookup ,当没有匹配时你会得到一个中断错误。因此,当错误未得到处理时,如您的情况,您将收到错误 Unable to get the VLookup property of the WorksheetFunction class .但是,如果您使用 Application.VLookup相反,你会得到一个不间断的错误。此外,由于您已将查找范围分配给 myrange , Range("myrange")应替换为 myrange ,正如@ARL 已经提到的那样。所以你应该有以下...

SupID = Application.VLookup(SupName, myRange, 2, False)

此外,您可以使用 IsError 测试错误。 ...
Dim SupID As Variant

SupID = Application.VLookup(SupName, myRange, 2, False)

If IsError(SupID) Then
MsgBox "No match found!", vbExclamation
Else
MsgBox "SupID: " & SupID, vbInformation
End If

此外,如果您使用 Application.InputBox而不是 InputBox功能,可以强制用户输入数字而不是文字等,可以测试用户是否取消。例如...
Dim mySVAlue As Variant

'prompt the user to enter a number
mySVAlue = Application.InputBox(Prompt:="Enter the no. of rows to be considered", Type:=1)

'if user cancels, exit the sub
If mySVAlue = False Then Exit Sub

所以你的宏可以重写如下......
'Force the explicit declaration of variables
Option Explicit

Sub LookupValues()

'declare the variables
Dim mySVAlue As Variant
Dim myRange As Range
Dim SupName As String
Dim SupID As Variant
Dim counterSVar As Long

'prompt the user to enter a number
mySVAlue = Application.InputBox(Prompt:="Enter the no. of rows to be considered", Type:=1)

'if user cancels, exit the sub
If mySVAlue = False Then Exit Sub

'set the range for the lookup table
Set myRange = Worksheets("UniqueSupNames").Range("A:B")

'lookup the values for the user selected rows
For counterSVar = 3 To mySVAlue
SupName = Range("A" & counterSVar).Value
SupID = Application.VLookup(SupName, myRange, 2, False)
Range("K" & counterSVar).Value = SupID
Next counterSVar

End Sub

关于excel - 错误 "Unable to get the VLookup property of the WorksheetFunction class"背后的原因可能是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56915038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com