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.

Skill level:
Moderately Challenging

Other People Are Reading

Things you need

  • Excel 2007 or Excel 2010

Show MoreHide


  1. 1

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

  2. 2

    Click "View Code."

  3. 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

  4. 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.

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.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

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