Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

invoking excel macros

Forum home » Delegate support and help forum » Microsoft Excel Training and help » Invoking Excel Macros

Invoking Excel Macros

ResolvedVersion 2003

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

Edited on Mon 9 Mar 2009, 14:23

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

Tue 17 Mar 2009: Automatically marked as resolved.

Excel tip:

Move or Highlight Cells

Use any of your movement keys, cursor, Home, End, PgUp or PgDn to highlight cells rows or columns by holding down the Shift key as you move.

Use in combination with the Ctrl key for quicker movements.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.