find the row number of the first empty cell in the first column in excel via vbs

1.4k views Asked by At

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

There are 4 answers

0
Ron Rosenfeld On BEST ANSWER

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.

7
Dominique On

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

1
Shemesh On

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

0
Foxfire And Burns And Burns On

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.