How to Assign a Macro to a Cell in Excel
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.
Things You'll Need
- Excel 2007 or Excel 2010
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" ThenRange("A10") = "Your text here"End IfEnd 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.
Tips & 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.