How to Assign a Macro to a Cell in Excel

Updated February 21, 2017

A macro is a piece of Visual Basic for Applications (VBA) executable code that you can use to tailor Microsoft Excel to suit your needs. For example, you can write a macro to format a spreadsheet in a particular way. Macros can be executed using the F5 key in the Visual Basic Editor (VBE), they can be attached to a function key or you can run a macro with a cell click.

Right-click the sheet tab you want the change to happen in. The sheet tabs are at the very bottom of the worksheet.

Click "View Code."

Cut and post the following code into the worksheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Address = "$A$1" Then

Range("A10") = "Your text here"

End If

End Sub

Change the code to suit your needs. The above code writes "Your text here" into cell "A10" when cell "A1" is clicked. Change the "A1" in "Target.Address = "$A$1"" to the cell you want to click, then change Range("A10") = "Your text here" to indicate the cell you want the event to happen in and the text you want to appear.


You can also access the worksheet code by pressing the "Alt" and "F11" keys together. If you don't want text to appear, you could perform a function or formula instead.

Things You'll Need

  • Excel 2007 or Excel 2010
Cite this Article A tool to create a citation to reference this article Cite this Article

About the Author

Stephanie Ellen teaches mathematics and statistics at the university and college level. She coauthored a statistics textbook published by Houghton-Mifflin. She has been writing professionally since 2008. Ellen holds a Bachelor of Science in health science from State University New York, a master's degree in math education from Jacksonville University and a Master of Arts in creative writing from National University.