Stop macro with loop then resume

rowbro

New Member
Joined
Dec 16, 2010
Messages
43
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
 

Some videos you may like

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

JB2020

Board Regular
Joined
Jul 29, 2020
Messages
75
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
You could try using DoEvents after changing the date?
 

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,358
Try this. I'm not sure if it would work.

VBA Code:
WaitTime = Now + TimeValue("0:00:05")
Do Until Now > WaitTime
    DoEvents
Loop
 

JB2020

Board Regular
Joined
Jul 29, 2020
Messages
75
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
Can I ask what you are using to import the data, i.e. power query?
 

Watch MrExcel Video

Forum statistics

Threads
1,118,812
Messages
5,574,479
Members
412,596
Latest member
nickthebizz
Top