简体   繁体   中英

Excel VBA .Find() error

I have the following piece VBA code, however when I run it I get Infinite MsgBoxes displaying me the same address 4 rows above the Address of ID. I have to stop execution with Ctrl+Break.

I've been trying to get the code to work and it seems to me the error comes from the Find() method. In other versions of my code I get "Method 'Find' of object 'Range' failed. Any ideas on how to fix this? Thank you.

Function CheckFinancials(ID As Integer) As Double
    Dim MyArray(1 To 4) As Integer
    Dim Guide As Range
    Set Guide = Worksheets("Financials").Range(Cells(1, 1),Cells(Worksheets("Financials").UsedRange.Rows.Count, 1))
    Set Guide = Guide.Find(ID)

    MsgBox Guide.Address

Your find miss a lot of parameters (try to record it, you'll see), test this :

Function CheckFinancials(ID As Integer) As Double
    Dim MyArray(1 To 4) As Integer
    Dim Guide As Range
    Set Guide = Worksheets("Financials").Range(Cells(1, 1), Cells(Worksheets("Financials").UsedRange.Rows.Count, 1))
    Set Guide = Guide.Find(What:=ID, LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

    MsgBox Guide.Address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM