+ Reply to Thread
Results 1 to 4 of 4

Thread: Run macro with value based on button clicked

  1. #1
    Registered User
    Join Date
    09-29-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    90

    Run macro with value based on button clicked

    Hi All,



    I have like three different buttons which perform the same functions. but the values they deal with are from three different pivot tables.



    For eg, When button 1 is clicked, it takes values from Pivot table 1 and executes the functionalities of the macro. When button 2 is clicked, values from Pivot Table 2 are taken and the SAME macro is executed.



    So essentially, the macro is the same, but it has to retrieve information from respective pivot table based on button clicked. I would like to know as to how to get this done.



    Also, if there is a way that I can pass values from one macro to another, I guess I can get this done. My approach was to provide an table number to each of the pivot table and pass that number to the Macro to choose that pivot table and execute from there.



    Let me know if there is a better way to do.

  2. #2
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,640

    Re: Run macro with value based on button clicked

    Hard to comment without seeing the code
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  3. #3
    Registered User
    Join Date
    09-29-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    90

    Re: Run macro with value based on button clicked

    Provided is the code which copies the pivot table and pastes it in another sheet. If you look at the name of the pivot table, it says "Pivot_yearly"&i - which means 'i' is the integer specifying the pivot table number.

    The number is to be allocated based on the button pressed. If button 1 is pressed, value of i is 1, if button 2 is pressed, value of i is 2 and so on.

    Hope its clear.

    On Error GoTo Reset
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        Application.EnableEvents = False
        Dim my_range As Range
        Worksheets("Pivot").Cells.Delete
        Worksheets("PivotTables").Activate
        Worksheets("PivotTables").PivotTables("Pivot_Yearly_" & i).PivotSelect "", xlDataOnly, True
        With Selection
            Set my_range = .Offset(, -1).Resize(.Rows.count - 1, .Columns.count + 1)
        End With
        my_range.Copy
        Worksheets("Pivot").Range("A1").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        
    Reset:
        Application.Calculation = xlCalculationAutomatic
        Application.EnableEvents = True
        Application.ScreenUpdating = True

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

    Re: Run macro with value based on button clicked

    something like ...........

    Sub BtnClickedWotsit()
        ' assign this macro to the buttons
        Dim i As Integer
        
        ' name buttons with names as below or change to suit
        Select Case Application.Caller
            Case "Button_1_Name"
                PivotTableWotsit (1)
            Case "Button_2_Name"
                PivotTableWotsit (2)
            Case "Button_3_Name"
                PivotTableWotsit (3)
            Case Else
                MsgBox "I wasn't expecting a call from this button"
                Exit Sub
        End Select
        
    End Sub
    
    Sub PivotTableWotsit(i As Integer)
        Dim my_range As Range
        
        On Error GoTo Reset
        With Application
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .EnableEvents = False
        End With
    
        Worksheets("Pivot").Cells.Delete
        With Worksheets("PivotTables")
            .Activate
            .PivotTables("Pivot_Yearly_" & i).PivotSelect "", xlDataOnly, True
        End With
        With Selection
            Set my_range = .Offset(, -1).Resize(.Rows.Count - 1, .Columns.Count + 1)
        End With
        my_range.Copy
        Worksheets("Pivot").Range("A1").PasteSpecial Paste:=xlPasteValues, _
            Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
        
    Reset:
        Set my_range = Nothing
        With Application
            .Calculation = xlCalculationAutomatic
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        
    End Sub

+ 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.2.0