我有以下代码:
然而,当我运行下面的脚本时,它停止工作:
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
答案 0 :(得分:4)
您有几个问题
Range("A:A")
而不是"A:A"
VLOOKUP
而不是LOOKUP
,那么您需要引用四列范围,Range("A:D")
以下示例代码供您调整
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