2016-10-14 84 views
0

我需要查找包含特定单词等的行以及某个(可变)行号之后。excel宏查找行

最新的行数公式如下:

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 

必须有最简单的方法更好的方法,而不是一个环......

+3

听起来好像你只是想用'after'选项设置为'Son1 + 1'行来做另一个'.Find'? – arcadeprecinct

+0

@arcadeprecinct我想找到最新的一行,比如行785之前的一个选项...(每个条件的行号更改) –

+0

您想查找特定行之前的最后一行或第一行之后的行某一排?你的问题和评论相互矛盾。你能更详细地描述你想找到的东西吗?也许只是一个小例子。 (编辑它到您的问题) – arcadeprecinct

回答

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

+0

感谢您的帮助。我用xlNext从头开始...... ''' –

+0

设置searchRange =范围(KirilimKolonu&SubB3& “:” &KirilimKolonu&SubB4) 设置foundCell = searchRange.Find(什么:= “4”, searchdirection:= xlNext) 如果不foundCell是Nothing然后 Son4 = foundCell.Row 如果Son5> SubB5然后 范围(Kolon2&SubB5).Formula = “= SUBTOTAL(9,” &Kolon2&SubB5 + 1 &“:”&Kolon2&Son5&“)” Else Range(Kolon2&SubB5).Formula =“= SUBTOTAL(9,”&Kolon2&SubB5 + 1&“:”&Kolo n2&LastRow&“)” End If End If ' –

+0

发布较长的代码部分从来就不是一个好主意。你有另外一个问题吗?我的代码对你有帮助吗?如果不是为什么不呢? – arcadeprecinct