0

I'm trying to find the number of the first empty row in my sheet in the excel via vbscript. Can some one help me out how to do that? that's what I did:

With objSheet
    iRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With
msgbox iRow

It's not working well if the sheet is empty. Thanks!

4

4 回答 4

0

You can check if, by doing xlDown, if you arrive at row 1048576 (that's the last row on the sheet).

于 2018-10-08T11:47:33.430 回答
0

Your now modified (with xlup) script will return the last NON-empty row in the column; unless the column is empty in which case it will return a 1. So if you want the first empty row, you need to add 1.

unless the script returns a 1. In that case, you need to check A1 to see if it is empty or not, and only add 1 if it is.

于 2018-10-08T12:02:52.703 回答
0

the answer as u said its:

With objSheet
    If objSheet.Cells(1,1).value = "" Then
        iRow = 1
    else
        iRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
    End If
End With

thanks guys

于 2018-10-08T12:13:36.937 回答
0

To get the FIRST empty cell in a column, you can use:

With objSheet
    If .Range("A1") = "" Then
        iRow = 1
    ElseIf .Range("A2") = "" Then
        iRow = 2
    Else
        iRow = .Range("A1").End(xlDown).Offset(1, 0).Row
    End If
End With

You must first check if first row or second row are empty, because End(xlDown) behaves different if row 1 is non-empty and second row is empty, it can skip it and returns the wrong value.

于 2018-10-08T12:16:51.517 回答