VBA - Refresh All Twice

ExcelKnut

Board Regular
Joined
Apr 18, 2013
Messages
144
Every day I open a bunch of workbooks and manually Refresh All each two times. I click Refresh All and when it's complete it's necessary for me to do Refresh All again--each refresh takes about 5 seconds. I'm trying to automate this. In VBA, when I have ActiveWorkbook.RefreshAll listed twice (as indicated below) it seems to only refresh once. And, I've tried putting Application.Wait (Now() + TimeValue("00:00:20")) in between the RefreshAll statements but that doesn't seem to help. Any assistance is greatly appreciated!!!!


VBA Code:
[QUOTE]
ActiveWorkbook.RefreshAll

ActiveWorkbook.RefreshAll
[/QUOTE]
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Since I'm still a newbie to VBA, I'm making a number of different Sub processes and then a Master Sub that does all of the calling in the order in which I am looking for.

So, can you do

VBA Code:
Sub RefreshAll_First()

  ActiveWorkbook.RefreshAll

End Sub

VBA Code:
Sub RefreshAll_Second()

  ActiveWorkbook.RefreshAll

End Sub

VBA Code:
Sub CallAll()

  Call RefreshAll_First
  Call RefreshAll_Second

End Sub
 
Upvote 0
Is the RefreshAll for refreshing querytable(s) or pivottables ?

If it is for refreshing a querytable, you could write some custom Method like REPT_RefreshAll to allow for repeated refreshes.

1- Place this code in the ThisWorkbook Module:
VBA Code:
Option Explicit

Private WithEvents Q As Excel.QueryTable
Private oWb As Workbook
Private lRefreshesCount As Long

Private Sub Q_AfterRefresh(ByVal Success As Boolean)

    Static lCurrentRefresh As Long

    If Success And lRefreshesCount > 0 Then
        lRefreshesCount = lRefreshesCount - 1
        oWb.RefreshAll
        Debug.Print "Refresh number :", lCurrentRefresh + 1
        lCurrentRefresh = IIf(lRefreshesCount = 0, 0, lCurrentRefresh + 1)
    End If
End Sub

Public Sub REPT_RefreshAll(ByVal QT As QueryTable, ByVal RefreshesCount As Long)
    Set Q = QT
    Set oWb = QT.Parent.Parent
    lRefreshesCount = RefreshesCount
    oWb.RefreshAll
End Sub


2- Usage Example:
VBA Code:
Sub example()
    'refresh querytable twice.
    Call ThisWorkbook.REPT_RefreshAll(QT:=Sheet1.QueryTables(1), RefreshesCount:=2)
End Sub


If you are trying to refresh pivottables, I am not familiar with them but I think you should be able do what you want using the corresponding pivottable event handlers.
 
Last edited:
Upvote 0
Jaafar,

Thanks for taking the time to provide that information. I'll definitely give that a shot.

Here's what I don't understand. When I step through the code, both "Refresh All" statements work perfectly but when I run the code they don't. Why is that?
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,686
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top