vba找到第一个非空行

时间:2016-09-01 21:15:55

标签: vba excel-vba row excel

我是VBA的新手并且正在努力解决这段代码。

我需要找到同时满足条件的第一个非空行。在col B和C中必须有文本,在col D和G中必须有数字(必须满足所有4个条件)。

我非常感谢你的帮助 小号

2 个答案:

答案 0 :(得分:3)

使用以下代码编写如下&if;&并注意:lastrow是列值的结尾。

for i = 1 to lastrow
 if cells(i,"b")<>"" and cells(i,"c")<>"" and isnumber(cells(i,"d"))= true and isnumber(cells(i,"g"))= true then
 'do something
 end if
next i

答案 1 :(得分:0)

您可能希望嵌套SpecialCells()方法,如下所示:

Sub main()

    With Worksheets("Conditions") '<--| change "Conditions" to your actual worksheet name
        With .Range("B1", .Cells(.Rows.Count, "B").End(xlUp)) '<-- refer to column "B" cells down to last non empty one
            With .SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues) '<-- refer to its "text" cells only
                With .Offset(, 1).SpecialCells(XlCellType.xlCellTypeConstants, xlTextValues) '<-- refer to adjacent column "text" cells only
                    With .Offset(, 1).SpecialCells(XlCellType.xlCellTypeConstants, xlNumbers) '<-- refer to adjacent column "number" cells only
                        With .Offset(, 1).SpecialCells(XlCellType.xlCellTypeConstants, xlNumbers) '<-- refer to adjacent column "number" cells only
                            MsgBox .Cells(1, 1).row '<--| get first "multifiltered" cells row
                        End With
                    End With
                End With
            End With
        End With
    End With

End Sub

您可能需要在每个SpecialCells()之前添加测试以检查&#34;当前&#34;列实际上有一些文本/数字值,使用Count()CountA()方法的混合