In some VBA macros, it is needed to get the selected cell (active cell) automatically. This may be required when developing simple as well as advanced macros. In a simple macro you might need to let the user select a cell before running the macro. Then macro will perform different tasks depending on the cell selected by the user. Also if you are developing advanced macros such as Games in Excel, then you may need to identify the active cell to make the game functional.
So now let’s see how to get the selected cell using VBA. I have selected cell B3 on my Excel sheet.
We can easily get the address of the selected cell like this.
MsgBox ActiveCell.Address
End Sub
Address of the Active cell will be shown in the message box if we run the above macro.
Also you can use the Split function to extract the column letter and the row number from the above result.
Learn how to use Split Function
However there is another way to get the row number of the active cell easily.
MsgBox ActiveCell.Row
End Sub
You can use a similar way to get the column. But it will return the column number rather than the letter.
MsgBox ActiveCell.Column
End Sub
Also see
Get Selected Rows Using VBA MacroHow to find the name of an active chart using VBA
Save a Workbook as a Single PDF Using VBA