Author Archives: Dinesh Kumar Takyar

About Dinesh Kumar Takyar

Welcome to exceltrainingvideos.com! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: http://youtube.com/familycomputerclub For a structured Excel VBA training course online you can visit: https://www.youtube.com/excelvbaonline

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