Waiting for Refresh

Abelethan

New Member
Joined
Nov 16, 2011
Messages
7
I am trying to get a macro to run when a cell is selected. After a parameter is entered into the cell above it for a Query. My problem is the On_Refresh routine runs before the refresh is finished..I have turned off the "Enable backgorund refresh" in the properties of the query. I have tried adding Application.Wait times to allow the query to run first. That hasn't worked either. The query is fast when it runs by itself <1sec but can't seem to get it to update the table before the macro is taking the table values and plugging them into the other worksheets cells. Any help would be appreciated.

Thanks,
Ryan

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)    BackgroundQuery = False
    If Target.Address = "$G$11" Then
    Call On_Refresh
    End If
End Sub


Code:
Sub On_Refresh()

BackgroundQuery = False
Worksheets("QMS-IQA-F-7-4-005 C").Select
Worksheets("AutoPrint").Select
Application.Wait (Now + TimeValue("00:00:01"))
ThisWorkbook.Worksheets("QMS-IQA-F-7-4-005 C").Range("C4").Value = Date
ThisWorkbook.Worksheets("QMS-IQA-F-7-4-005 C").Range("C5").Value = ThisWorkbook.Worksheets("AutoPrint").Range("G5").Value
ThisWorkbook.Worksheets("QMS-IQA-F-7-4-005 C").Range("C6").Value = ThisWorkbook.Worksheets("AutoPrint").Range("F21").Value
ThisWorkbook.Worksheets("QMS-IQA-F-7-4-005 C").Range("C7").Value = ThisWorkbook.Worksheets("AutoPrint").Range("H21").Value
ThisWorkbook.Worksheets("QMS-IQA-F-7-4-005 C").Range("D7").Value = ThisWorkbook.Worksheets("AutoPrint").Range("I21").Value
ThisWorkbook.Worksheets("QMS-IQA-F-7-4-005 C").Range("C8").Value = ThisWorkbook.Worksheets("AutoPrint").Range("G21").Value
ThisWorkbook.Worksheets("QMS-IQA-F-7-4-005 C").Range("C9").Value = ThisWorkbook.Worksheets("AutoPrint").Range("J21").Value


Worksheets("QMS-IQA-F-7-4-005 C").Select
ActiveSheet.PrintOut
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
this may or may not work, i'm guessing on this

i assume that the query you mentioned is actually a pivot table update

get rid of the "call on_refresh" in Worksheet_SelectionChange


put your on_refresh code into Worksheet_PivotTableUpdate

once the pivot table finishes updating, the code will run

optionally check which pivot table updated (myWatchedPivotTable is pseudo code, correct value needs to be substituted)

Code:
Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
      
      if Target <> myWatchedPivotTable then exit sub      ' exit if this is not the pivot table that i wait for
      
      if Target.Name <> "myPivotTable" then exit sub      ' maybe this?

      '  original code here

      :
      :
      :
 
Last edited:
Upvote 0
Code:
set a global variable

dim sqlRunning as boolean

sqlRunning = false

------------------------------------------
your sql code ... something like this


sqlRunning = true

run sql

sql done

sqlRunning = false

---------------------------------------------

your macro ... add this

while sqlRunning      ' keeps looping until sql code is done
    doEvents
loop

------------------------------------------------

how is the sql code running at the same time as your macro?

is one of them event driven?
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,916
Members
449,093
Latest member
dbomb1414

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