+ Reply to Thread
Results 1 to 5 of 5

Need macro's behavior to depend on button pushed

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-25-2007
    Posts
    166

    Need macro's behavior to depend on button pushed

    Hi All,

    I have a spreadsheet with several different sections of data. I'd like to have a button within each section to run a macro (what the macro does is less important), but I'd like to just create one single macro which will work on all of the sections.

    In short, I'm wondering if there's a way to create a single macro, have multiple buttons linked to that one macro, but have the behavior of the macro vary depending on which button is pushed.

    For instance, the macro called myMacro will shade the top left cell of section X green. Three different sections - A, B, and C - each have a button linking to myMacro. If the button in section C is pushed, the top left cell of section C will be shaded green; if the button in section A is pushed, the top left cell of section A will be shaded green.

    Is there a way for a macro to detect which button was pushed, and modify behavior accordingly? I'd like to avoid having to create a separate macro for each section if at all possible. I've done some forum and google searches to try to find answers on this but I've come up short. If anyone has any helpful resources on this topic I'd greatly appreciate it.

    Many thanks in advance,
    Jeff
    Last edited by JChandler22; 03-29-2010 at 09:57 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Need macro's behavior to depend on button pushed

    You can create a Public Variable that is set by each button & used to determine the action
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: Need macro's behavior to depend on button pushed

    Using Button from "Forms Toolbar" name the buttons as required

     Sub MyUniversalMacro()
     ' Run button specfic procedure
    
        Dim Choice As String
    
        Select Case Application.Caller
            Case "Button 3", "Button 4"
                Choice = "running macro for button """
                ' add button specific code here for buttons 3 or 4 .............
            Case "Button 5"
                Choice = "running macro for button """
                ' add button specific code here .............
            Case Else
                Choice = "No secific procedure for button """
                ' add code here for any other button .............
        End Select
    
        MsgBox Choice & Application.Caller & """", , "The Choosen One"
        
    End Sub

  4. #4
    Forum Contributor
    Join Date
    06-25-2007
    Posts
    166

    Re: Need macro's behavior to depend on button pushed

    These are great suggestions. Thank you both!

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Need macro's behavior to depend on button pushed

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1