2016-01-08 93 views
0

Set finder = .Find(clientName, LookIn:=xlValue, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext)行接收到運行時錯誤9下標超出範圍,我嘗試了一堆東西來修復它。我錯過了一些至關重要的東西,我不確定它是什麼。通讀這裏的所有其他帖子,沒有一個似乎幫助/關聯過於密切。錯誤9下標超出範圍嘗試了所有內容

注意事項: 1)變量CLIENTNAME是一個標準的字符串時,傳遞到該函數被正確重視。 2)我在查找行之前添加contactsMaster.Activate行來測試工作表對象,並且它正確地激活到表單,這導致我相信它與表單或工作簿的名稱無關(我使用getBook = Activeworkbook.Name主要是爲了避免與用戶名稱變化相關的問題。3)我已將範圍更改爲contactsMaster.Range("A:C"),但這也沒有改變任何內容。 4)數據存儲在列A到C中。每個單元一個客戶端名稱。此函數旨在通過客戶端名稱來標準化命名約定方法,以便我可以使用相同的約定從任何宏中查找文件。 5)最初我認爲在錯誤行上使用.Address是由於某種原因拋出一個錯誤,但它似乎不是這種情況,因爲我已經刪除它並仍然收到相同的錯誤。代碼如下:

Function GetClientName(clientName As String) As String 

'Gets specific client name to save excel file in a standardized format to be found easily 

    'Sets Objects/vars 
    Dim finder As Range 
    Dim location As Long 
    Dim contactsMaster As Worksheet 

    Set contactsMaster = Workbooks("EEB MACRO DEVELOPMENT BOOK").Sheets("Contacts Master") 

    With contactsMaster.Range("A1:C1000") 
     Set finder = .Find(clientName, LookIn:=xlValue, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext) 
    End With 

    'Prevents error for not finding client in contacts master 
    If finder Is Nothing Then 
     GoTo endFunc 
    End If 

    location = finder.Row 

    GetClientName = contactsMaster.Cells(location, 1) 

    Exit Function 

endFunc: 

    GetClientName = clientName 
End Function 

回答

1

你只需要使它LookIn:=xlValues,請注意在最後的「S」。此外,您正在使用Lookin:=LookAt:=,所以只爲持續性,我建議把What:=太在一開始,讓你獲得:

Set finder = .Find(What:=clientName, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext)

+1

的感謝!知道這是愚蠢和小事。我很感激 – StormsEdge

+0

@StormsEdge - 不用擔心!由於*看起來正確,我認爲這肯定是一些拼寫錯誤或濫用這些限定詞。很高興很簡單! – BruceWayne

相關問題