Help with macro please

V

Victor Delta

In an Excel 2002 spreadsheet, I have three different macros (say a, b and c)
which do similar, but slightly different, things on 3 different worksheets
(say 1, 2 and 3).

I would like to combine the macros into one macro which senses the worksheet
name and then invokes the appropriate code. Is there a simple way to do this
please?

Thanks,

V
 
J

James Ravenswood

Here is a typical example. If you run MASTER, it detects the name of the active sheet and call the proper subrouitne:

Sub mac1x()
MsgBox 1
End Sub

Sub mac2x()
MsgBox 2
End Sub

Sub mac3x()
MsgBox 3
End Sub

Sub MASTER()
Dim s As String
s = ActiveSheet.Name
If s = "Sheet1" Then Call mac1x
If s = "Sheet2" Then Call mac2x
If s = "Sheet3" Then Call mac3x
End Sub
 
S

Stan Brown

In an Excel 2002 spreadsheet, I have three different macros (say a, b and c)
which do similar, but slightly different, things on 3 different worksheets
(say 1, 2 and 3).

I would like to combine the macros into one macro which senses the worksheet
name and then invokes the appropriate code. Is there a simple way to do this
please?


In VBA, so much of coding is just figuring out what things are
called.

ActiveSheet.Name contains the name of the currently active worksheet.
You can do IF tests on ActiveSheet.Name for the effect you intend. (I
do exactly this in some of my workbooks.)
 
V

Victor Delta

Stan Brown said:
In VBA, so much of coding is just figuring out what things are
called.

ActiveSheet.Name contains the name of the currently active worksheet.
You can do IF tests on ActiveSheet.Name for the effect you intend. (I
do exactly this in some of my workbooks.)

Many thanks to both of you for your responses. I used James's code and it
works brilliantly. Problem solved!

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top