2012-02-12 264 views
4

我有以下代碼:VLOOKUP,EXCEL,錯誤438「對象不支持此屬性或方法」

  • 它應該進入一個名爲「閃電俠」片,並獲得第i個2位數字值,並檢索活動單元右側偏移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

'activesheet.worksheetfunction.vlookup' – 2012-02-12 00:04:45

+0

@TomIngram嗯...這聽起來像它可以工作,但是當我代替我的方法與它仍返回'438'錯誤... – franklin 2012-02-12 00:20:39

+0

嘗試'Application.WorksheetFunction .V Lookup' – Cutter 2012-02-12 01:10:54

回答

4

你有幾個問題

  1. 您需要參考範圍爲Range("A:A")"A:A"
  2. 如果使用VLOOKUP,而不是LOOKUP然後根據說明你需要提及的四列範圍,Range("A:D")
  3. 您需要處理您的測試值不在A中找到
0123下面

示例代碼爲您適應

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)` 

這場比賽

BU我覺得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 
+0

就像後續一樣,Vlookup是否會將搜索「F71」與包含字符串「F71 - 更多無意義追隨」的數據點進行精確匹配? – franklin 2012-02-12 03:25:35

+1

@franklinchou查看更新評論 – brettdj 2012-02-12 03:35:53

+0

最後只是使用for循環Mid()和Len()函數來查找我正在查找的術語。將嘗試這種方法。非常感謝@brettdj – franklin 2012-02-15 21:00:14

相關問題