Excel vba无法获取VLookup属性

Excel vba无法获取VLookup属性,excel,vlookup,vba,Excel,Vlookup,Vba,我是vba编程新手。谁能帮我解决这个问题 Sub GetPrice() Dim PartNum As Variant Dim Price As Variant PartNum = InputBox("Enter the Part Number") Sheets("Sheet2").Activate Price = WorksheetFunction._ VLookup(PartNum, Range("A2:C20"), 2, False)

我是vba编程新手。谁能帮我解决这个问题

Sub GetPrice()
    Dim PartNum As Variant
    Dim Price As Variant
    PartNum = InputBox("Enter the Part Number")
    Sheets("Sheet2").Activate
    Price = WorksheetFunction._
    VLookup(PartNum, Range("A2:C20"), 2, False)
    MsgBox PartNum & "costs" & Price
End Sub
每次我尝试运行此代码时,输入框都是ok的,但随后出现错误:无法获取VLookup属性。。。我死后


非常感谢您的帮助。

如果查找表中没有查找值,您将得到一个不明确的错误。尝试将其包装在一些错误更正中,以查看是否有帮助:

Sub GetPrice()
    Dim PartNum As Variant
    Dim Price As Variant
    PartNum = InputBox("Enter the Part Number")
    Sheets("Sheet2").Activate
    On Error GoTo out
    Price = WorksheetFunction.VLookup(PartNum, Sheet2.Range("A2:C20"), 2, False)
    MsgBox PartNum & "costs" & Price
    Exit Sub

out:
MsgBox "Part not found"
End Sub

我个人总是在Excel宏中使用单元格[rowIndex]、[columnIndex]。这是与数据交互所需的最基本且大多数情况下唯一的功能。如果您试图只获取值,而不是位置,那么VLookUp是好的。使用电池你有更多的能量

这是一个在工作表的已定义部分中搜索特定字符串的实现:

Sub searchPartNumber()
Dim ret() As Integer
'initialize the cell space to search in.
Dim fromCell(1) As Integer
Dim toCell(1) As Integer
'index 0: row
'index 1: column

'(A,2):(C,20) looks like this:
fromCell(0) = 1
fromCell(1) = 2
toCell(0) = 3
toCell(1) = 20

PartNum = InputBox("Enter the Part Number")
ret = searchTable(PartNum, fromCell, toCell)
If ret(0) = 1 Then
    MsgBox ("The value searched is in Cell " & ret(1) & ", " & ret(2) & " and the value searched for is " & Cells(ret(1), 2))
Else
    MsgBox "This part number could not be found."
End If
End Sub

Function searchTable(ByVal searchValue As String, ByRef fromCell() As Integer, ByRef toCell() As Integer) As Integer()
Dim ret(2) As Integer
'index 0: 1 = found, 0 = not found
'index 1/2: row and column of found element
ret(0) = 0
Dim i As Integer, j As Integer
For i = fromCell(0) To toCell(0)
    For j = fromCell(1) To toCell(1)
        If (CStr(Cells(i, j)) = searchValue) Then
            'Item found
            ret(0) = 1
            ret(1) = i
            ret(2) = j
            searchTable = ret
            Exit Function
        End If
    Next
Next
End Function
谢谢大家!

我想我找到了问题所在。我不能将“PartNum”定义为我仍然没有得到的变量?我认为该变体可以替代任何潜在类型的数据


在我将“PartNum”定义为整数或双精度后,我可以很好地运行这个vlookup函数,因为我试图查找的值实际上是数字

试着把VLookup放在同一行上。可能重复的肯定重复的。您需要在此代码中进行错误处理,以说明搜索值不存在的可能性。Doug的答案尤其有用:yes PartNum=InputBoxEnter零件号将PartNum视为字符串,因为InputBox返回一个字符串,该字符串表示InputBox表单中TextBox的文本属性。VLOOKUP非常特别,不使用文本查找数字字段,反之亦然。您必须要么不像以前那样定义为variant,要么像CIntInputBox一样强制转换。。。。这对你解释为什么变体失败有意义吗?当然!谢谢你,马克!
Sub searchPartNumber()
Dim ret() As Integer
'initialize the cell space to search in.
Dim fromCell(1) As Integer
Dim toCell(1) As Integer
'index 0: row
'index 1: column

'(A,2):(C,20) looks like this:
fromCell(0) = 1
fromCell(1) = 2
toCell(0) = 3
toCell(1) = 20

PartNum = InputBox("Enter the Part Number")
ret = searchTable(PartNum, fromCell, toCell)
If ret(0) = 1 Then
    MsgBox ("The value searched is in Cell " & ret(1) & ", " & ret(2) & " and the value searched for is " & Cells(ret(1), 2))
Else
    MsgBox "This part number could not be found."
End If
End Sub

Function searchTable(ByVal searchValue As String, ByRef fromCell() As Integer, ByRef toCell() As Integer) As Integer()
Dim ret(2) As Integer
'index 0: 1 = found, 0 = not found
'index 1/2: row and column of found element
ret(0) = 0
Dim i As Integer, j As Integer
For i = fromCell(0) To toCell(0)
    For j = fromCell(1) To toCell(1)
        If (CStr(Cells(i, j)) = searchValue) Then
            'Item found
            ret(0) = 1
            ret(1) = i
            ret(2) = j
            searchTable = ret
            Exit Function
        End If
    Next
Next
End Function