+ Reply to Thread
Results 1 to 7 of 7

I need macro/vba help: Code to refresh pivot table!!

Hybrid View

  1. #1
    Registered User
    Join Date
    06-20-2014
    Location
    west midlands
    MS-Off Ver
    2010
    Posts
    5

    I need macro/vba help: Code to refresh pivot table!!

    I'm relatively new to vba and require some help constructing a code .

    objective: i have a huge set of data 200k+ rows which is updated monthly, each time a new report is ran there is a template sheet where i drop the data and have to manually update three pivot tables linked to it that then do a find remove on one column to get rid of a set list of text. The second stage is to update and refresh the individual pivot tables and then copy this data into a summary.

    All I want to do is to be able to put all that into a code so each month i don't have to do the repetitive task of manually removing the same text and refreshing the table etc

    Anyone know how?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,104

    Re: I need macro/vba help: Code to refresh pivot table!!

    Suggest you switch on the macro recorder while you do the task(s) manually. That will give you the basic code. It will probably need tweaking and refining to make it generic and more efficient.

    At that point, come back with a sample workbook and the embryo code and someone will help you to make it do what you want ... with a little luck

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    06-20-2014
    Location
    west midlands
    MS-Off Ver
    2010
    Posts
    5

    Re: I need macro/vba help: Code to refresh pivot table!!

    a common sense move , thanks TMS

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,104

    Re: I need macro/vba help: Code to refresh pivot table!!

    You're welcome.

  5. #5
    Registered User
    Join Date
    06-20-2014
    Location
    west midlands
    MS-Off Ver
    2010
    Posts
    5

    Re: I need macro/vba help: Code to refresh pivot table!!

    Apologies if the below is asking too much as I am a novice to VBA and trying to catch up quickly.
    The below is the ‘embryo code’ generated from a recorded macro. Numerous attempts to edit this myself has caused various compile errors so I’m at my wits end.

    Please help!!
    The objective I want to achieve is to have a button that the user presses once they have entered data into columns l to V the data quantity is upwards of 300k rows each time so filters have been applied from columns A-K.

    Once data is entered I want the code to find and replace specific references in column k which removes part of the text. In this example _yes, _no, _maybe. It’s safe to assume these variables will always be at the end of the text. I then want to make sheet two which is the pivot table which will be hidden to appear and refresh .

    Then copy and paste special values all that data into a new sheet change ‘Key2’ values to be in a specific order and then copy and paste the data values and key amounts only (without headers) into a set template.
    The stages up to this point are quite repetitive hence the need for the coding
    There are various pivot tables that will need refreshing and data copied into different sheets relevant to each pivot table so any instructions you can add to the code to help me identify what i can modify would help.

    The user will then have instructions of how to manually manipulate the set template only.

    Sub Embryo_code()
    '
    ' Embryo_code Macro
    '
    ' Keyboard Shortcut: Ctrl+r
    '
        Range("P8").Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Range("K8").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Selection.Replace What:="_yes", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Selection.Replace What:="_no", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Selection.Replace What:="_maybe", Replacement:="", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Range("A7").Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Application.CutCopyMode = False
        sheets.Add
        ActiveWorkbook.Worksheets("Sheet6").PivotTables("PivotTable2").PivotCache. _
            CreatePivotTable TableDestination:="Sheet2!R3C1", TableName:="PivotTable1" _
            , DefaultVersion:=xlPivotTableVersion14
        sheets("Sheet2").Select
        Cells(3, 1).Select
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("PK Tariff")
            .Orientation = xlRowField
            .Position = 1
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("PK Fuel")
            .Orientation = xlRowField
            .Position = 2
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("RC Prod Type")
            .Orientation = xlRowField
            .Position = 3
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("RC Charge")
            .Orientation = xlRowField
            .Position = 4
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Key4")
            .Orientation = xlRowField
            .Position = 5
        End With
        ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
            "PivotTable1").PivotFields("Key11"), "Count of Key11", xlCount
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Key2")
            .Orientation = xlColumnField
            .Position = 1
        End With
        With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Key11")
            .Caption = "Sum of Key11"
            .Function = xlSum
        End With
        ActiveWindow.SmallScroll Down:=9
        Range("A19").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Key4").Subtotals = Array( _
            False, False, False, False, False, False, False, False, False, False, False, False)
        Range("B33").Select
        With ActiveSheet.PivotTables("PivotTable1")
            .InGridDropZones = True
            .RowAxisLayout xlTabularRow
        End With
        Range("B21").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("PK Fuel").Subtotals = Array _
            (False, False, False, False, False, False, False, False, False, False, False, False)
        Range("A25").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("PK Tariff").Subtotals = _
            Array(False, False, False, False, False, False, False, False, False, False, False, False)
        Range("C24").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("RC Prod Type").Subtotals = _
            Array(False, False, False, False, False, False, False, False, False, False, False, False)
        Range("D21").Select
        ActiveSheet.PivotTables("PivotTable1").PivotFields("RC Charge").Subtotals = _
            Array(False, False, False, False, False, False, False, False, False, False, False, False)
        Range("I25").Select
        ActiveWindow.SmallScroll Down:=-36
        Cells.Select
        Selection.Copy
        sheets.Add After:=sheets(sheets.Count)
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Range("G5").Select
        Columns("E:E").EntireColumn.AutoFit
        sheets("Sheet3").Select
    End Sub
    Attached Files Attached Files

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: I need macro/vba help: Code to refresh pivot table!!

    Hi,

    Does the attached help. It uses the following procedures

    Sub ReplaceYesNoMaybe()
        Range("data").Columns("P").Replace what:="_*", replacement:=""
        Sheet1.PivotTables("PivotTable1").PivotCache.Refresh
        Call Module1.CopyPT
    
    End Sub
    Sub CopyPT()
        Sheet3.Range("A3").CurrentRegion.Clear
        Sheet2.Activate
        Sheet2.PivotTables("PivotTable1").PivotSelect "", xlDataAndLabel, True
        Selection.Copy
        Sheet3.Range("A3").PasteSpecial (xlPasteValues)
    
    End Sub
    I also added the dynamic range name "Data" to cover your data set and used that name in the PT. I also added a one line macro in the PT sheet Activate event that automatically refreshes the PT every time you select it. It's also refreshed when you use the first of the procedures above.
    Attached Files Attached Files
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  7. #7
    Registered User
    Join Date
    06-20-2014
    Location
    west midlands
    MS-Off Ver
    2010
    Posts
    5

    Re: I need macro/vba help: Code to refresh pivot table!!

    Thanks Richard Buttrey - this works perfectly

+ 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. [SOLVED] VB Code to Refresh Pivot Table Automatically every 5 mins - For Dashboard
    By MarkyP18 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-06-2015, 04:38 PM
  2. Problems with code to refresh pivot table
    By Masun in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-19-2015, 09:04 AM
  3. VBA code to auto refresh Pivot table
    By GEMINI528 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-29-2014, 07:01 PM
  4. Pivot table refresh - VBA Code
    By Raviprasad.k in forum Excel General
    Replies: 3
    Last Post: 12-14-2012, 08:19 AM
  5. Refresh Multipe Pivot Table Macro
    By DaFonz01 in forum Excel General
    Replies: 1
    Last Post: 03-18-2010, 03:56 AM
  6. pivot table refresh code failed
    By dreamz in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-24-2005, 03:22 PM
  7. [SOLVED] Code to Refresh Pivot Table
    By S Jackson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-31-2005, 01:05 PM

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