Find Last Row Column in Data with Blanks using VBA

How to find the last row and last column in data with blanks using VBA. Look at the image below.

Find last row column in data with blanks using VBA

Find last row column in data with blanks using VBA

Now which is the last column used for rows 1 to 3? Which is the last column for row 4? Which is the last row and last column for the complete data? We may need this information for accurate data entry. Also which formula should we apply to get the correct last row and column used? If you don’t have blanks in the data then the solution is easy. If you have blanks in your data then you need to be careful which formula to use to capture the last row and last column correctly.

Watch the video below which finds a solution to these questions quickly and easily using VBA.

 

Here’s the complete VBA code:

Sub lastRowColumn()
Dim lastRow As Long, lastColumn As Long
lastRow = Cells.Find(What:=”*”, After:=Range(“A1”), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox “The last row used in data is ” & lastRow
lastColumn = Cells.Find(What:=”*”, After:=Range(“A1”), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
MsgBox “The last column used in data is ” & lastColumn
End Sub

Sub lastRowSpecificColumn()
Dim lastRowSpCol As Long, lastColumnSpRow As Long
‘Last row of data for column C.
lastRowSpCol = Cells(Rows.Count, 3).End(xlUp).Row
MsgBox “The last row of data for column C ” & lastRowSpCol
‘Last column of data for row 2.
lastColumnSpRow = Cells(2, Cells.Columns.Count).End(xlToLeft).Column
MsgBox “The last column of data for row 2 is ” & lastColumnSpRow

End Sub

Sub lastRowColMultiSp()
Dim lastRowMultiColumns As Long, lastColumnMultiRows As Long
‘Last row of data for columns D and E
lastRowMultiColumns = Range(“D:E”).Find(What:=”*”, After:=Range(“D1”), _
SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
MsgBox “Last row of data for columns D and E is ” & lastRowMultiColumns

lastColumnMultiRows = Rows(“1:3″).Find(What:=”*”, _
After:=Cells(1, Cells.Columns.Count), _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column

MsgBox “The last column for rows 1 to 3 is ” & lastColumnMultiRows
End Sub

Further reading:

Range.Find Method (Excel)

Find Next Empty Row for Data Entry using Excel VBA