Umut K
Umut K

Reputation: 1388

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...

Upvotes: 0

Views: 82

Answers (1)

arcadeprecinct
arcadeprecinct

Reputation: 3777

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

Upvotes: 1

Related Questions