Vlookup,excel,错误438“对象不支持此属性或方法”

时间:2012-02-12 00:01:29

标签: excel excel-vba vba

我有以下代码:

  • 它应该进入名为" Flash"并获取第i个2位数字值并检索Activecell右侧第4列中的值。
  • 然后交换到名为" Sheet1"在同一工作簿上,使用垂直查找功能查找检索到的值,并返回该单元格右侧的值4列。

然而,当我运行下面的脚本时,它停止工作:

MsgBox (ActiveSheet.VLookup(LookFor, "A:A", 4, True))

和VBA抛出error 438 object doesn't support this property or method

有谁知道为什么有例外?

' Begin lookup :
Dim i As Integer, designator As String, LookFor As String
Flash.Activate
ActiveSheet.Range("C3").Select
For i = 3 To lastUsedCellInRow("C")
    designator = "C" + CStr(i)
    Dim cellVal As String
    cellVal = ActiveSheet.Range(designator).Value()
    If (Len(cellVal) <= 2 And IsNumeric(cellVal)) Then
        LookFor = ActiveSheet.Range(designator).Offset(0, 4).Value()
        RawData.Activate
        MsgBox (ActiveSheet.VLookup(LookFor, "A:A", 4, True))
    End If
Next i

1 个答案:

答案 0 :(得分:4)

您有几个问题

  1. 您需要将范围称为Range("A:A")而不是"A:A"
  2. 如果您使用的是VLOOKUP而不是LOOKUP,那么您需要引用四列范围,Range("A:D")
  3. 您需要处理在A
  4. 中找不到的测试值

    以下示例代码供您调整

    Dim strLookfor as String
    Dim strOut
    strLookfor = "test"
    strOut = Application.VLookup(strLookfor, Range("A:D"), 4, True)
    If IsError(strOut) Then
        MsgBox "value not found"
    Else
    'return column D value as string
        MsgBox CStr(strOut)
    End If
    

    <强>后续

    是的,您可以使用

    `strOut = Application.VLookup(strLookfor & "*", Range("A:D"), 4, True)`
    

    这场比赛

    我认为Find更清洁,即

    Dim strLookfor As String
    strLookfor = "F71"
    Dim rng1 As Range
    Set rng1 = Sheets("Sheet1").Columns("A").Find(strfolder & "*", , xlValues, xlPart)
    If Not rng1 Is Nothing Then
        MsgBox "Match in " & rng1.Offset(0, 3)
    Else
        MsgBox strfolder & "*" & vbNewLine & "not found"
    End If