PDA

View Full Version : Solved: Disable subs depending on which button is pressed



rob0923
08-22-2009, 08:53 AM
Hi,

This is just a general question.. If you were to have several buttons required to do similar tasks (Macros) but didn't want to create 4 seperate subs for each one. Is it possible to disable called subs depending on what button is pushed?

So you would have one main sub with all you Called subs but somehow get them to by pass the called sub.

Thanks in advance,

Bob Phillips
08-22-2009, 09:16 AM
Dim mCaller As String

mCaller = Application.Caller

If mCaller <> "Button 1" Then Call macro1
If mCaller <> "Button 2" Then Call macro2
If mCaller <> "Button 3" Then Call macro3
If mCaller <> "Button 4" Then Call macro4

rbrhodes
08-22-2009, 03:00 PM
Hi rob,

To do 4 in one, change each 'Button # name' to the name of the Butttons:


Option Explicit
Sub FourInOne()

Dim mCaller As String

mCaller = Application.Caller

Select Case mCaller
Case Is = "Button 1 name"
'..do the code here and nothing else...
MsgBox ("Running " & mCaller & " code")

Case Is = "Button 2 name"
'..do the code here and nothing else...
MsgBox ("Running " & mCaller & " code")

Case Is = "Button 3 name"
'..do the code here and nothing else...
MsgBox ("Running " & mCaller & " code")

Case Is = "Button 4 name"
'..do the code here and nothing else...
MsgBox ("Running " & mCaller & " code")

End Select

End Sub





To get the names of the Buttons if you don't know them:


Sub bName()
Dim i As Long
Dim shp As Shape
Dim ButtonName As String

'//Change row number to suit
'Row number
i = 1
'//End

'Do all
For Each shp In ActiveSheet.Shapes
'Get name
ButtonName = shp.Name

'//Change Column to suit
'Put name
Range("A" & i) = ButtonName
'//End

'Next row
i = i + 1
Next shp

'Cleanup
Set shp = Nothing
End Sub

rob0923
08-22-2009, 05:05 PM
I'll give this a shot. Gonna take some tweaking since I haven't done this before.

Thanks for your time!

rbrhodes
08-22-2009, 05:39 PM
let me know...

rob0923
08-31-2009, 01:38 PM
Hi,

I have one more comment that is similar to this subject.

I have two files that need to be processed from excel to world with the same macro code, but I would like them to be able to run seperatly. Do I have copy and paste twice, or is there another way to tell which one to run while only using the sub once?

Thanks!

mdmackillop
09-01-2009, 09:07 AM
You could save your code in Personal.xls or create a Add-In (xla file) to hold your code. It can then be called from any workbook/worksheet.

rob0923
09-02-2009, 07:32 AM
I have a bit of a dead line on this now. Not sure if I have the time to figure that one out, but i'll look into that!

Thanks for the reponse!