【问题标题】:vba unable to get VLookup propertyvba 无法获取 VLookup 属性
【发布时间】:2014-07-07 19:38:06
【问题描述】:

我是 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的,但是接着报错:unable to get VLookup property... after I ty

任何帮助表示赞赏。

【问题讨论】:

标签: excel vlookup vba


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    Sub GetPrice()
        Dim PartNum As Variant
        Dim Price As Variant
        PartNum = InputBox("Enter the Part Number")
        Price = Application.VLookup(PartNum, Sheets("Sheet2").Range("A2:C20"), 2, False)
        If Not IsError(Price) Then
            MsgBox PartNum & " costs " & Price
        Else
            MsgBox "Part Number '" & PartNum & "' was not found!"
        End If
    End Sub
    

    【讨论】:

      【解决方案3】:

      我个人总是在 Excel 宏中使用 Cells([rowIndex], [columnIndex])。这是与数据交互所需的最基本(也是大多数情况下唯一)的功能。如果您尝试仅获取值而不是位置,则 VLookUp 很好。使用 Cells() 你有更多的权力。

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

      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
      

      【讨论】:

        【解决方案4】:

        谢谢大家!

        我想我找出了问题所在。我不能将“PartNum”定义为 Variant(我仍然没有得到它?我认为 Variant 可以替换任何潜在的数据类型)。

        在我将“PartNum”定义为 Integer 或 Double 之后,我可以很好地运行这个 vlookup 函数,因为我尝试查找的值实际上是数字!

        【讨论】:

        • 是的,PartNum = InputBox("Enter the Part Number") 会将 PartNum 视为字符串,因为 InputBox 返回一个字符串,该字符串表示 InputBox 表单中 TextBox 的 Text 属性。 VLOOKUP 非常注重不查找带有文本的数字字段,反之亦然。您必须要么不定义为变体(就像你所做的那样),要么像 CInt(InputBox(...)) 那样强制转换。你觉得 Variant 为什么会失败吗?
        • 当然!谢谢马克!
        猜你喜欢
        • 2014-05-13
        • 1970-01-01
        • 2023-03-19
        • 2013-10-17
        • 2016-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多