How to Get the Address of a Selection in VBA

Save

When programming in VBA, you may find it necessary to retrieve the address of the cell selection for the code. For instance, if you need to change the font color, the selection address of the cell is necessary. Selection a cell address in VBA is performed using only a few steps.

  • Activate the sheet. Since an Excel workbook has several spreadsheets, so first activate it. The following code syntax activates the worksheet:
    Worksheets("mySheet").Activate

  • Assign a variable for the active cell address. The following code retrieves the active cell column and row pair:
    myAddress = ActiveCell.Address(False, False)

  • Set a value for the active cell. The following code sets the column header "Customer" into the active cell:
    ActiveCell.Value = "Customer"

  • Display the active cell and its value. The following code displays "Customer" and the address:
    MsgBox "Cell address is: " & myAddress & " and the value is: " & ActiveCell.Value

References

Promoted By Zergnet

Comments

You May Also Like

Read Article

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!