?
Solved

Excel VBA pause code for all Pivot Tables to refresh

Posted on 2012-09-04
5
Medium Priority
?
2,227 Views
Last Modified: 2012-09-26
I have created a spreadsheet that has Pivot Tables based on data in Access queries.  
From Excel VBA,
- I open the database,
- run all the append queries,
- close Access,
- do a RefreshAll command for the Pivot Tables,
- and then for a 3rd party app (BeGraphic) I have to use SendKeys to refresh the charts.  - Finally, I copy all the relevant Excel files from the main folder to a backup folder with a prefix of the previous month/years.  

For some reason, the RefreshAll for the pivot tables runs after a Wait command, a msgbox, another macro for the SendKeys and the copying of files.  I want the code to stop until the Pivot Tables are refreshed.  That's why I threw in the msgbox and wait code.

    'Run all the Append queries
    For CounterB = 1 To Range("AppendQueryNames").Rows.Count
    oapp.DoCmd.OpenQuery (ActiveCell.Offset(CounterB - 1, 1).Text), acViewNormal, acEdit
    Next

    oapp.Quit
    
    'Refresh the Pivot Tables
    ActiveWorkbook.RefreshAll
    Application.Wait Time + TimeSerial(0, 0, 20)
    
    'Update BeGraphic
    MsgBox ("Waiting")
    bgtest

    fn = Dir(myDir & "*.xls*")
    Datebu = Format(Now() - 27, "mmm") & "_" & Year(Now()) & "_"
    Do While fn <> ""
        FileCopy myDir & fn, newFolder & Datebu & fn
        fn = Dir
    Loop

Open in new window

    'Run all the Append queries
    For CounterB = 1 To Range("AppendQueryNames").Rows.Count
    oapp.DoCmd.OpenQuery (ActiveCell.Offset(CounterB - 1, 1).Text), acViewNormal, acEdit
    Next

    oapp.Quit
    
    'Refresh the Pivot Tables
    ActiveWorkbook.RefreshAll
    Application.Wait Time + TimeSerial(0, 0, 20)
    
    'Update BeGraphic
    MsgBox ("Waiting")
    bgtest

Open in new window

0
Comment
Question by:pmpatane
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:tdlewis
ID: 38366729
Try replacing the MsgBox with
DoEvents

Open in new window

0
 
LVL 10

Expert Comment

by:tdlewis
ID: 38366736
If that doesn't work then you can try putting your macro to sleep:
Private Declare Sub Sleep Lib "kernel32" _
    (ByVal dwMilliseconds As Long)

Sub WaitASecond()
    Sleep (1000)
End Sub

Open in new window

0
 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 38366873
Instead of using Refreshall I would refresh each cache individually

   Dim PC                As PivotCache
   For Each PC In ActiveWorkbook.PivotCaches
      PC.Refresh
   Next PC

Open in new window

0
 

Author Comment

by:pmpatane
ID: 38379630
Hi all,
I will be trying the different solutions here Monday morning when we get August data input into the database.  My apologies for the delayed response, and my appreciation for all the suggestions!
0
 

Author Closing Comment

by:pmpatane
ID: 38437429
This took care of it!  Sorry for the late reply...I work at a law firm and priorities change at light-speed here and I have been working on other projects.  I am now back with this one
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question