Find the last row of a column and the last column of a row using a macro in an excel report

This macro shows a basic concept of how to find the last row/column. In this form, it is not very useful, at least for me, but customizing it, it is totally useful. In the other hand, in this other topic Know the last column letter of a row using a macro in an excel report, I explain how to find the letter of the last column of a row.

formula excel

 

When I use the macro ?

For instance, I need to find the last column then the last cell of this same column because all cells in this range are formulas so it will copy and paste this range to the next column.

 

How to create the macro ?

Read How to create, edit, hide and select a macro in an excel report

 

How to create the button to associate it with the macro ?

Read How to create a button and associated it to a macro in an excel report

 

How is the macro ?

Copy the code below and paste it into your macro. You will see my comments in green if exist so follow the help to adapt to your need.

Sub test()
Dim lastRow
Dim lastCol
' searching the last row of the column A represented by the number 1
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
' searching the last column of the row 1
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
' a popup message will display to tell you the results
MsgBox "Last Row: " &lastRow & vbNewLine & "Last Column: " & lastCol
End Sub

Example of how to use it, copy and paste:

Sub test()
Dim lastRow
Dim lastCol
' searching the last row of the column A represented by the number 1
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
' searching the last column of the row 1
lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
' copy cell A2 and paste to sheet2 on cell A1
' change Sheet2 by your sheet
Range("A" & lastRow).Copy Worksheets("Sheet2").Range("A1")
' copy cell A1:D2 and paste to sheet3 from cell A1
' change Sheet3 by your sheet
Range("A1:D" & lastRow).Copy Worksheets("Sheet3").Range("A1")
End Sub

Another example, display the content:

Sub test()
Dim lastRow
Dim lastCol
' searching the last row of the column A represented by the number 1
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
lastCol = Cells(lastRow, "A").Value
MsgBox lastCol
End Sub

If you want to know the number of columns, read my other article Count the number of rows and columns using a macro in an excel report.

Interesting Management