简体   繁体   中英

excel macro find the row

I need to find a row that contains a specific word etc. AND after a certain (variable) row number.

The latest row number formula is as follows:

Son1 = Range(KirilimKolonu & ":" & KirilimKolonu) _
           .Find(what:="1", _
                 after:=Range(KirilimKolonu & "1"), _
                 searchdirection:=xlPrevious).Row

something like

x =1

do while x < 785

if range(KirilimKolonu & x).value = 1 then exit loop else

x = x + 1

Loop

there must be a better way instead of a loop...

The easiest way would be to modify the range to search and search backwards. If you specify the cell to start the search (the After option) somewhere in the middle, the search will wrap around and your hit might actually be after the starting cell (rather than no hit at all)

Dim foundCell As Range
Dim searchRange As Range
Dim Son1 As Long

Set searchRange = Range("A1:A" & 785) 'your variable goes here

Set foundCell = searchRange.Find(what:="1", searchdirection:=xlPrevious) 'specify other options if you must

'you need to check if something was found or .Row will cause an error.
If Not foundCell Is Nothing Then
    Son1 = foundCell.Row
Else
    'do what you need to if there is no match
End If

More options

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