+ Reply to Thread
Results 1 to 8 of 8

Run Macro when Slicer Changes

  1. #1
    Registered User
    Join Date
    06-10-2015
    Location
    Vancouver, WA
    MS-Off Ver
    2013
    Posts
    4

    Cool Run Macro when Slicer Changes

    Hello,
    Thank you for teaching me what I know. Ha...It's not much but I've read threads and went through http://www.homeandlearn.org/. I am brand new and hooked.

    I was able to create a very simple code and have to push a Macro button to run it. Instead, I would like to have it automatically run when I make a different selection on a Slicer.

    Here is my code:


    -----
    Private Sub Worksheet_PivotTableUpdate()

    Dim TotalLines As Integer
    TotalLines = Worksheets("Sheet1").Range("E:E").Cells.SpecialCells(xlCellTypeConstants).Count - 1
    Dim NextLine As Integer
    NextLine = 8

    Range("F8:F1048576").Value = "" 'Clear Old Values
    If TotalLines > 0 Then 'End If Nothing Returned
    For X = 1 To TotalLines
    Range("F" & NextLine).Value = Range("E" & NextLine) / Range("D" & NextLine)
    NextLine = NextLine + 1
    Next X
    End If

    End Sub
    -----

    Of course any programming tips for what I've made would be GREAT! But what I really want is to have this run automatically. Instead, I get an error that says "Compile error: Procedure declaration does not match description of event or procedure having the same name".

  2. #2
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Run Macro when Slicer Changes

    A slicer updates the pivot table, and the PivotTableUpdate event will run after that. Does it not do that for you?

  3. #3
    Registered User
    Join Date
    06-10-2015
    Location
    Vancouver, WA
    MS-Off Ver
    2013
    Posts
    4

    Re: Run Macro when Slicer Changes

    Thank You! Yes, it does run but I get the error.

  4. #4
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Run Macro when Slicer Changes

    If your code changes part of the Pivot Table, then the PivotTableUpdate event will be in a loop. You need to disable events before you manipulate the pivot table.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-10-2015
    Location
    Vancouver, WA
    MS-Off Ver
    2013
    Posts
    4

    Re: Run Macro when Slicer Changes

    So... Because the Slicer only changes one field of my pivot table, the PivotTableUpdate goes into a loop? Then your code needs to be run before the Slicer is changed? How do I do that?

  6. #6
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Run Macro when Slicer Changes

    It's hard to say without knowing the extent of the pivot table. All I can seed from your code is that you clear a very big range of cells and I assumed that that might interfere with the pivot table. I may be wrong.

    To take the guesswork out of the equation, post a sample file with non-sensitive data that shows the behaviour you describe. To post a file, click Go Advanced below, and then the paper clip icon.

  7. #7
    Registered User
    Join Date
    06-10-2015
    Location
    Vancouver, WA
    MS-Off Ver
    2013
    Posts
    4

    Re: Run Macro when Slicer Changes

    The Pivot Table is linked to a Data Warehouse so uploading it won't work unless you had access to the warehouse. Instead, I took a screenshot and created a JPEG. It's only 108KB but when I try to attach it here, I get the spinning "working" symbol but it won't actually upload. Might be a security setting on my PC that I need to change. Can I email it to you? My email is [email protected].

    Also, I've modified the Data Clearing portion of my code...does this work better?

    'Remove Old Data
    Range("F:F").Value = ""
    Range("F7").Value = "Average Purchase Price"

  8. #8
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,372

    Re: Run Macro when Slicer Changes

    The forum can be slow at times. Try the screenshot again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Run a Macro using Slicer
    By ManUBlueJay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-08-2014, 05:21 PM
  2. Make one slicer mirror filters of another slicer
    By IbenezPrez in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-28-2014, 02:00 PM
  3. Macro on slicer is slow
    By surveyman1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-11-2013, 01:20 PM
  4. Slicer Macro - unfilter another slicer on selection
    By chadheins in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-24-2012, 10:41 AM
  5. [SOLVED] Change Chart Title Depending on a Slicer - slicer advice
    By JungleJme in forum Excel General
    Replies: 8
    Last Post: 08-17-2012, 07:59 AM

Tags for this Thread

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