+ Reply to Thread
Results 1 to 14 of 14

Pivot Table not refreshing properly

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Pivot Table not refreshing properly

    I have some data downloaded by means of a query created in VBA.
    This data is then modified by adding columns of formulae.
    A pivot table is then generated from the data and is further modified.
    This is all done by procedures I have used many times before.
    This time however when the pivot table is refreshed the two data columns are left out. This is unexpected and I cant see what I may have done differently to cause the problem.
    I have prepared the attached workbook using the same code as the original in the hope that it would show what happens, but it works. (Click an option button and press "Choose a Month"
    on UserForm2 to fire the code.)
    I would guess that there will be code that would give more control over the pivot refresh than my Sub (DoPivot). Would that be a way forward?
    John
    Attached Files Attached Files

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Pivot Table not refreshing properly

    You are trying to refresh PivotTable1's pivotcache...

    But your pivot table name is actually PivotTable3.

    I'd change your DoPivot code to something like below.
    Please Login or Register  to view this content.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Pivot Table not refreshing properly

    CK76
    Thanks for your help(Again!). Using PivotTables(1) makes no difference. The two value fields are still missing. If I rebuild the table and then do a manual refresh it works. If I manually refresh the partial table it refreshes partialy. I have made a new version on a new sheet and the same fault persists. I have changed PivotTable1 for PivotTable3 in DoPivot and was unable to find PivotTable3. (I had changed the name manually earlier).
    Is it possible to list the names of the pivottables on a worksheet? I can see that it is better to use PivotTables(1) as long as I am looking for the first.
    John

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Pivot Table not refreshing properly

    If you need to list all pivot tables in a worksheet.

    Please Login or Register  to view this content.
    If you need to list them, populate collection or array with the names, and retrieve as needed (or directly load from loop to ListBox or ComboBox).

    The two value fields are still missing.
    What value fields do you mean? CODE2 & CODE3? Or some other field?

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Pivot Table not refreshing properly

    Sorry, I should have been clearer. The two fields that go missing are Sum Of Prior_Yr_Mth7 and Sum of Balance_Mth7. Those are the fields that are selected from the database according to the month chosen by the selected option button on UserForm2.
    The value affects SQLStr.
    John

  6. #6
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Pivot Table not refreshing properly

    I have noticed that when I refresh the pivot either manually or through the program I get a warning "There is already data in PivotTable do you want to replace it"?
    I don't recall seeing this message before. Perhaps it provides a clue as to what is going on.
    I have also noticed that when the fields go missing they have been unchecked in the field list.

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Pivot Table not refreshing properly

    Could my problem be caused by Pivottable option setting?

  8. #8
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Pivot Table not refreshing properly

    That error means, that you have existing values/formula in the range where pivot table is going to occupy.

    That's causing the issue here.

    As for fields going missing. I can't really test it. I can't run your Userform as it requires DSN file to connect to DB.

    If you need to add value field back to Pivot Table when it becomes unselected...

    You'd use PivotTable.AddDataField method. See link for details. For sum, you'd use xlSum for third argument.
    https://docs.microsoft.com/en-us/off...e.adddatafield

  9. #9
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Pivot Table not refreshing properly

    That makes sense. When the PivotTable changes it looses the two fields that I want and replaces them with spaces for all the fields of the table I am "pivoting". Those extra columns cover part of the range where I am making calculations.
    I still have no idea why the simple refresh code isn't working, and I think I may have to redo the PivotTables each time. I hope your link may help there.
    John

  10. #10
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Pivot Table not refreshing properly

    looses the two fields that I want and replaces them with spaces for all the fields of the table I am "pivoting"
    My guess, is that Column/field name changed from original, and it caused these fields to be dropped (become unselected).

  11. #11
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Pivot Table not refreshing properly

    Bingo! You have got it. The databases I query have fields for every month of this year and every month of last year. My query asks for the results for month n for this year and last year.
    If n changes from the time the query was set up the two fields will be missing.
    Is there a way of keeping a constant field name for the pivottable even though the results come from different database fields?

    John

  12. #12
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Pivot Table not refreshing properly

    No, the field name must match source name.
    However, in your SQL string, you can change name of the column and keep it constant.

    Ex:
    Please Login or Register  to view this content.
    You can then change Caption of the field to display desired string in the pivot table header (underlying name remains unchanged).

  13. #13
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Manchester England
    MS-Off Ver
    Excel 2010
    Posts
    992

    Re: Pivot Table not refreshing properly

    CK76
    I think the last posts crossed each other. in case my last did not arrive, thanks once again for your help. It solved a problem that had stopped me for days.
    I told you that I had worked round the problem by copying the data from the query into a second table with static headings. I will now follow your much more elegant solution.
    John

  14. #14
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,887

    Re: Pivot Table not refreshing properly

    You are welcome

+ 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. Help with pivot table not refreshing.
    By holykimura in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 06-12-2019, 10:01 PM
  2. Pivot Table not refreshing - HELP
    By milo1984 in forum Excel General
    Replies: 0
    Last Post: 09-12-2018, 07:03 AM
  3. [SOLVED] Pivot Table Data Not Refreshing from Named Table
    By Seth_ in forum Excel Charting & Pivots
    Replies: 5
    Last Post: 05-04-2016, 11:10 AM
  4. Pivot table not refreshing
    By Zealotwraith in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 11-18-2013, 12:02 PM
  5. Refreshing Pivot table data
    By yael in forum Excel General
    Replies: 12
    Last Post: 01-09-2012, 07:51 AM
  6. Refreshing Pivot Table
    By yawnzzzz in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-30-2009, 04:59 PM
  7. Refreshing a pivot table
    By Louise in forum Excel General
    Replies: 2
    Last Post: 05-23-2005, 09:06 AM

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