excel宏找到行

时间:2016-10-14 08:41:55

标签: excel vba excel-vba

我需要在某个(可变的)行号之后找到包含特定单词等的行。

最新的行号公式如下:

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

类似

x =1

do while x < 785

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

x = x + 1

Loop

必须有更好的方式而不是循环......

1 个答案:

答案 0 :(得分:1)

最简单的方法是修改搜索范围并向后搜索。如果您指定要在中间某处开始搜索的单元格(After选项),搜索将会回绕并且您的命中可能实际上位于起始单元格之后(而不是根本没有命中)

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