Stop macro with loop then resume

rowbro

Board Regular
Joined
Dec 16, 2010
Messages
50
Hi all,

I have a macro that I am running to pull a series of data into excel from an internet based API, which is then dumped into my database for further use. The challenge I am facing is that pulling the data takes a few seconds, and I need to stop the macro between changing the date and saving the file, to allow for this. The "application.wait" function seems to stop the API, so I think the best is to stop and resume the macro. The challenge here is that the macro is a loop, that loops through dates set in the sheet, in cells C8:C9.

Can someone please assist me to stop the macro for 5 seconds, before resuming it, while allowing the API pull to still function?

Thanks!

VBA Code:
Sub LoopThroughRange()
Dim rng As Range, c As Range, fPath As String, fName As String
fPath = "\\111.11.1.1\STAGING_AREA\"
Set rng = Range(Range("C8").Value & ":" & Range("C9").Value)
    For Each c In rng
        If c <> "" Then
        c.Copy
        
        Range("C3").PasteSpecial xlPasteValues
        
[I][B]        Application.Wait (Now + TimeValue("0:00:05"))[/B][/I]
        
        Sheets("Upload").Cells.Copy
        Sheets("ULVal").Cells.PasteSpecial Paste:=xlPasteValues
        Application.CutCopyMode = False
                
        fName = "NSX_ALL_" & Format(Range("C3").Value, "yyyymmdd")
        
        Sheets("ULVal").Copy
        ActiveWorkbook.SaveAs fPath & fName & ".csv", FileFormat:=6, CreateBackup:=True, local:=True
        ActiveWorkbook.Close
        End If
    Next
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You could try using DoEvents after changing the date?
 
Upvote 0
Try this. I'm not sure if it would work.

VBA Code:
WaitTime = Now + TimeValue("0:00:05")
Do Until Now > WaitTime
    DoEvents
Loop
 
Upvote 0
Can I ask what you are using to import the data, i.e. power query?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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