Macro to Pause/Wait

Shahzadt

Board Regular
Joined
Aug 25, 2009
Messages
72
Is there a way to pause the macro while excel runs a query? i am using a macro to run a query on a large file. once the data is returned to excel i am copy/pasting it to different sheet. the problem i am currently running into at this time is that the query takes over few secounds to return data and macro moves to the next line of code. due to which i am getting an error. i have tried to add the code
Code:
        Do While IsEmpty(Cells(1, 2))
            .Wait Now + TimeSerial(0, 0, 2)
        Loop
and this is causing excel to get stuck in a loop with no results. please help.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
If the query is running in the background, try adding the DoEvents in your loop. Loops are always dangerous this way. It doesn't help not seeing your entire code either.
 
Upvote 0
If the query is running in the background, try adding the DoEvents in your loop. Loops are always dangerous this way. It doesn't help not seeing your entire code either.
Another approach to stall your code that might work (untested by me) is something like this:
Code:
Sub test()
Dim T As Variant
T = Now + TimeSerial(0, 0, 2)
Do Until Now >= T
Application.EnableEvents = False
Loop
MsgBox "HELLO"
Application.EnableEvents = True
End Sub
 
Upvote 0
But what happens when the time runs out and the process(es) still aren't finished? Better to base it off the actual events and when they complete.
 
Upvote 0
Code:
       With .Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
        .Sheets.Add
        With .ActiveSheet
            With .ListObjects.Add(SourceType:=0, Source:=Array(Array("ODBC;DSN=Excel Files;DBQ=" & ActiveWorkbook.FullName & _
                ";DefaultDir=" & ActiveWorkbook.Path), Array(ActiveWorkbook.Path & _
                ";DriverId=1046;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range("$A$1")).QueryTable
                .CommandText = Array( _
                    "SELECT" _
                        , "`My_Sheet$`.Invoice_Number, `My_Sheet$`.Invoice_Date," _
                        , "Sum(`My_Sheet$`.Quantity) AS Quantity, Sum(`My_Sheet$`.Amount) AS Amount, `My_Sheet$`.Billing_Code1" _
                        & Chr(13) & "" & Chr(10) & _
                    "FROM" _
                        , "`My_Sheet$` `My_Sheet$`" _
                        & Chr(13) & "" & Chr(10) & _
                    "GROUP BY" _
                        , "`My_Sheet$`.Invoice_Number, `My_Sheet$`.Invoice_Date, `My_Sheet$`.Billing_Code1")
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .BackgroundQuery = True
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .PreserveColumnInfo = True
                .ListObject.DisplayName = "Table_Query_from_Excel_Files"
                .Refresh BackgroundQuery:=True
            End With
        End With
 
'------------------------------------------------------------------------------------------------------------------------
        Do While IsEmpty(Cells(1, 2))
            .Wait Now + TimeSerial(0, 0, 2)
        Loop
        .Sheets.Add
        With .ActiveSheet
            Sheets(2).Range("A1", Range("A1").SpecialCells(xlLastCell)).Copy
            .Range("A1").PasteSpecial (xlPasteValuesAndNumberFormats)
here is the code. it is a part of a more detail sub. i am trying to use to do loop after the query and thats when i think it goes into an infinate loop. i am thinking i would need a line of code which would alow the query to finish while the macro pauses. I am not familiar with DoEvent loop. Please help.
 
Upvote 0
Does Application.Wait allow a query to run in the background? Maybe I am looking for something different. is it possible to stop the macro in the middle of sub while a external data query finishs refreshing in the back ground?
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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