Help with macro please

  • Thread starter Thread starter Victor Delta
  • Start date Start date
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
 
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
 
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.)
 
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
 
Back
Top