Graham has attended:
Excel Intermediate course
Excel Advanced course
Invoking Excel Macros
I want to run a macro when the value in a cell changes. e.g. if the cell contains the letter "A" macro 1 is run, if it contains the letter "B2 macro 2 is run, and so on. Can this be done?
Many thanks in advance.
Graham
RE: Invoking Excel Macros
Hi Graham
What you require needs a bit of VBA code.
1. Open the Visual Basic Editor
2. Double click the relevant sheet in the Project area.
The code below needs to be pasted into the code area for this sheet.
Dim MySelection As String
Dim Indicator As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MyValue As String
If Indicator = True Then
MyValue = Range("A1").Value
Select Case MyValue
Case "A"
Call M1
Case "B"
Call M2
Case "C"
Call M3
End Select
Indicator = False
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MySelection = ActiveCell.Address
If MySelection = "$A$1" Then
Indicator = True
End If
End Sub
Replace the M1, M2 etc with the names of the relevant macros. If more letters are used enter the code immediately after Case "C" following the above pattern.
For a better understanding of the code I suggest you attend our Excel VBA Introduction course.
Hope this helps
Carlos