简体   繁体   English

在Excel VBA中的可变长度范围上使用FindNext

[英]Use FindNext on a Range of Variable Length in Excel VBA

I'm fairly next to excel VBA and am trying to use the FindNext function to find the next non-zero value in a column on the following data set. 我相当接近excel VBA,并且正在尝试使用FindNext函数在以下数据集的列中查找下一个非零值。
在此处输入图片说明
When I first tried to do this I used the Columns function saying: Columns("J").FindNext().Row but returned the value of 3 rather than 11. Then I tried tried to use the range object, declaring it as Range("J2", Cells(Cells(Rows.Count, "H").End(xlUp).Row, "J")) where Column H has data till the length of Column J. doing this returned the error "Compile Error: Wrong number of Arguments or invalid property assignment" I'm not entirely sure why 1) the compiler wont accept how I declared the Range and 2) why the FindNext function is not returning the row of the next non-zero value. 当我第一次尝试执行此操作时,我使用Columns函数说: Columns("J").FindNext().Row但返回值3而不是11。然后尝试使用range对象,将其声明为Range("J2", Cells(Cells(Rows.Count, "H").End(xlUp).Row, "J"))其中,列H具有数据,直到列J的长度为止。这样做会返回错误“编译错误:错误的参数数量或无效的属性分配”,我不完全确定为什么1)编译器不会接受我如何声明Range以及2)为什么FindNext函数未返回下一个非零值的行。 Thank you in advanced. 在此先感谢您。

You do not need either Find() or FindNext() to locate the rows containing non-empties: 您不需要Find()FindNext()来查找包含非空的行:

Sub FindingAnything()
    Dim rng As Range
    Dim s As String

    Set rng = Range("J:J").Cells.SpecialCells(xlCellTypeConstants)

    For Each r In rng
        s = s & r.Row & vbCrLf
    Next r

    MsgBox s
End Sub

在此处输入图片说明

If the column contained some cells with formulas, you would change the SpecialCells() argument. 如果该列包含一些带有公式的单元格,则可以更改SpecialCells()参数。 If the column was filled with formulas, some of which return non-Nulls, you would have an interesting problem ! 如果列中填充了公式,其中一些返回非Null,那么您将遇到一个有趣的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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