Results 1 to 2 of 2

Thread: In Excel - Can you assign a CommandButton with a procedure that has arguments?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2004
    Posts
    28

    In Excel - Can you assign a CommandButton with a procedure that has arguments?

    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")?

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Location
    Luton, UK
    Posts
    178
    This is the usual method. You set the variable in the button click sub then call the subroutine containing the common code :-
    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 --------------------------------------
    Regards
    BrianB
    -------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width