PDA

Click to See Complete Forum and Search --> : In Excel - Can you assign a CommandButton with a procedure that has arguments?


odedyer
Oct 21st, 2004, 03:23 AM
I have several CommandButtons on the worksheet and I want each one to do the same action, but on a different range of cells. Is there a way to assign each CommandButton with the same procedure, so that every time you press it the procedure will do whatever it does but on the right range of cells?
Maybe assign it with a procedure that has arguments?
Maybe you can do it if the procedure "knows" (maybe gets a handler or something) which button called it and then from the button's name or something it can decide which range to work on...?
Any other ideas (except for selecting the range and let the procedure work on "ActiveSheet.selection")?

BrianB
Oct 21st, 2004, 03:48 AM
This is the usual method. You set the variable in the button click sub then call the subroutine containing the common code :-

'- declaration top of module --------------------
Dim MyRange As Range
'------------------------------------------------
Sub Button1_Click()
Set MyRange = ActiveSheet.Range("A1:A100")
Mainsub
End Sub
'------------------------------------------------
Sub Button2_Click()
Set MyRange = ActiveSheet.Range("B1:B100")
Mainsub
End Sub
'---------------------------------------------
Sub Mainsub()
'- common code
MsgBox (MyRange.Address)
End Sub
'-- eop --------------------------------------