How to Assign a Macro to a Cell in Excel

Written by stephanie ellen
  • Share
  • Tweet
  • Share
  • Email

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.

Tip

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.

Tips and warnings

  • 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.
Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.