How to Assign a Macro to a Cell in Excel

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

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.

Advertisement

Step 1

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

Video of the Day

Step 2

Click "View Code."

Step 3

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

Advertisement

Step 4

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.

Advertisement

Video of the Day

Advertisement

Advertisement

references & resources

Report an Issue

screenshot of the current page

Screenshot loading...