VBA: Query refresh always runs first regardless of where it is in the code.

joeyslaptop

New Member
Joined
Aug 26, 2011
Messages
27
I'm trying to create a macro that does the following:

On theButton was clicked
set theButton style =msoShapeStylePreset12
Run the query
Query refresh completes
set theButton style = msoShapeStylePreset14
end

But, the button doesn't change until after the query has refreshed. I eve tried putting it in a loop with a condition:

Code:
Sub RefreshQueries()
'
' RefreshQueries Macro
' This macro triggers when the button is pushed.  It refreshes the queries that produce the datasets.

Dim Looper As Integer
    Looper = 1
Do
'change the button to yellow.
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset12
    Looper = 2
    If Looper = 2 Then
'Do query refresh.
    ActiveSheet.Shapes.Range(Array("Rounded Rectangle 1")).Select
    Sheets("Mbrs to Renew").Range("H2").ListObject.QueryTable.Refresh BackgroundQuery:=False
    Looper = 3
    End If
Loop Until Looper = 3
'change the button back to green.
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset14
End Sub
 
Last edited:

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
Dim Looper As Integer
    Looper = 1
Do
'change the button to yellow.
    If Looper = 1 Then
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset12
    Looper = 2
    End If
    If Looper = 2 Then
'Do query refresh.
    ActiveSheet.Shapes.Range(Array("Rounded Rectangle 1")).Select
    Sheets("Mbrs to Renew").Range("H2").ListObject.QueryTable.Refresh BackgroundQuery:=False
    Looper = 3
    End If
    If Looper = 3 Then
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset14
    Looper = 4
    End If
Loop Until Looper = 4
'change the button back to green.
End Sub
 
Upvote 0
Add the line in blue as shown
Code:
Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset12
[COLOR=#0000ff]DoEvents[/COLOR]
 
Upvote 0
It still doesn't work. I tried commenting out the part where it changes it back to msoShapeStylePreset14. The query refreshes, and then the button turns yellow.
 
Upvote 0
What if you just use
Code:
Sub joey()
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset12
    DoEvents
    Sheets("Mbrs to Renew").Range("H2").ListObject.QueryTable.Refresh BackgroundQuery:=False
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset14
End Sub
How long does it take for the query to refresh?
 
Upvote 0
Same results. The button doesn't change to msoShapeStylePreset12 until after the query refresh completes. And then it switches to mso...14 so quickly - like it won't update the window until after the query refreshes. Is there a way to ensure that a window update/refresh takes place before moving to the next event in the macro?
 
Upvote 0
Could you please answer my question?
 
Upvote 0
Thanks for your help. Here's what worked:


Code:
Sub RefreshQueries()
[LEFT][COLOR=#222222][FONT=Verdana]    Application.ScreenUpdating = False[/FONT][/COLOR]
[/LEFT]
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset12
    Application.ScreenUpdating = True 
    Sheets("Mbrs to Renew").Range("H2").ListObject.QueryTable.Refresh BackgroundQuery:=False
    Sheets("Stats").Shapes("Rounded Rectangle 1").ShapeStyle = msoShapeStylePreset14
End Sub

Setting the ScreenUpdate to False and then back to true caused the screen update to activate and update the screen before proceeding to the next step.
 
Last edited:
Upvote 0
Glad you got it sorted & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,984
Members
449,058
Latest member
oculus

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