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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.

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,379
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,122,589
Messages
5,597,053
Members
414,118
Latest member
moversnpackers

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
Top