2014-04-07 45 views
0

嗨我想找到一個單元格包含變量「AI」,然後選擇該行。搜索可能是空的,如果是這樣的話,它應該寫一個文本。錯誤9由於.find

這是到目前爲止我的代碼

Private Sub Worksheet_Change(ByVal Target As Range) 
Dim KeyCells As Range 
Dim mf As Object 
Dim RZ As String 
Dim AI As String 


' The variable KeyCells contains the cells that will 
' cause an alert when they are changed. 
Set KeyCells = Range("B3") 

If Not Application.Intersect(KeyCells, Range(Target.Address)) _ 
     Is Nothing Then 

    ' Place your code here. 

AI = Range("B3").Value 
Sheets("DATA").Select 
Sheets("DATA").Columns("C:C").Select 

Set mf = Sheets("DATA").Columns("C:C").Find(What:=AI, LookAt:=Whole, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, SearchFormat:=False) 
    If Not mf Is Nothing Then 

    RFound = mf.Row 
    Sheets("DATA").Cells(RFound, 1).Select 
    RZ = Sheets("DATA").Cells(RFound, 2).Value2 
    Sheets("Búsqueda").Select 
    Cells(3, 6).Value = RZ 
    Else 
    Cells(3, 6).Value = "RAZON SOCIAL" 

    End If 
End If 
End Sub 

問題是

Set mf = Sheets("DATA").Columns("C:C").Find(What:=AI, LookAt:=Whole,  SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, SearchFormat:=False) 

回報 「的錯誤9超出範圍」

+3

'LookAt:= Whole' - >'LookAt:= xlWhole' –

+0

這確實奏效!我改變了它,因爲xlWhole沒有發現整個單詞,但現在它似乎工作。謝謝! – Stephanie

回答

0
LookAt:=Whole 

...應該是:

LookAt:=xlWhole 

信用應該去@simoco,因爲他提供了答案。

我不知道正確的禮儀(即使在檢查Meta後)。

  • 理想情況下,simoco應該將他的評論轉換爲正式答案(並且應該被接受)。他當然應該信任。
  • 與此同時,問題應該會得到答案,所以我正在填寫答案。如果我的答覆被接受,那麼通過澄清解決方案可以幫助社區。

我可以投票結束,但這似乎不是單純的錯字。這似乎是一個高質量的問題。