Continue working while loop does batches

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,
I am unable to figure out how to solve the following problem.
  • I have a working loop that runs down a list of names in column B (Account) and retrieves data from individual account workbooks on our network (quite a few) and puts them on the main sheet like below.
  • My problem is that some of the external workbooks get updated at different times given who makes them (Singapore vs. Cleveland for example).
  • I would like to adjust the loop so that it considers the accounts in batches and pauses a certain amount of time before continuing to retreive.
  • If possible, I would like to be able to continue working with the workbook while it is waiting for the next time stamp to get the next batch of data.
I've tried a few things out there that pause after each request, but that kind of defeats the purpose as I would like to be able to work "in between" updates in this and other excel files.

How could I for example send just the first 5 accounts through the loop to the Call, Pause for 1 hour (while still using excel?) and then process 5 more, pause for an hour, until the list is complete?
Thanks!

VBA Code:
Option Explicit
Public Const firstAcctRow As Integer =3

SubSnippet()

Dim lastRow As Integer
Dim Acct As String
Dim AcctName As String
Dim firstAcctRow As Integer

lastRow = ActiveSheet.Cells(Rows.Count, "b").End(xlUp).Row

For Acct = firstAcctRow To lastRow
AcctName = Worksheets("Sheet1").Range("$b$" & Acct)
Call ExternalDataUpdate(AcctName)

Next

End Sub




Book4
ABCDEFGHIJKLMNOPQRS
2#AccountDateABCDEFGHIJKLMNO
31A6/6/2022401135810612810760788676531481308422
42B6/5/2022103591332541696136514910725547814
53C6/5/20229890845990881001271166186132908779
64D6/5/2022101387231143115118193112616809211698
75E6/5/20221221021445291108102124921081177413311869
86F6/5/2022381693194714530363715046113132093
97G6/5/202210958129010515641435214242120184939
108H6/5/2022137103171597148681501595126681937146
119I6/5/20224316134181413867150861362282548768
1210J6/5/202248481813499538612039126961718118124
1311K6/5/20222828833914011986725768204710980125
1412L6/5/202214384109107169312870423194673356110
1513M6/5/20221087610742298552193855581226112765
1614N6/5/2022661723771041255669126107611198128134
1715O6/5/202232341355681385910741109705611814132
Sheet1
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Perhaps like

VBA Code:
For Acct = firstAcctRow To lastRow
   For i = 1 To 5
      AcctName = Worksheets("Sheet1").Range("$b$" & Acct)
      Call ExternalDataUpdate(AcctName)
   Next
'Call to your pause code goes here - it is in separate sub, yes?
Next

In the pause sub try Do Events, possibly as the first line to be executed before the timer portion. Can't see it so can't suggest anything specific. Also, I don't know if Do Events will free up the workbook or not, especially since what happens after the call to anywhere else is unknown.
 
Upvote 0
Perhaps like

VBA Code:
For Acct = firstAcctRow To lastRow
   For i = 1 To 5
      AcctName = Worksheets("Sheet1").Range("$b$" & Acct)
      Call ExternalDataUpdate(AcctName)
   Next
'Call to your pause code goes here - it is in separate sub, yes?
Next

In the pause sub try Do Events, possibly as the first line to be executed before the timer portion. Can't see it so can't suggest anything specific. Also, I don't know if Do Events will free up the workbook or not, especially since what happens after the call to anywhe

Have been trying to get your code to work, but it looks like once the For loop gets done with the 5th "i" (Acct E), it starts back at the first Acct (A) again instead of doing A to E in one batch, pausing/waiting then resuming with Acct F.

This is the pause loop that I am using that was posted by Logit in 2020. I like that it shows the time left and lets me continue working in the worksheet at the same time. Seems to work fine.

VBA Code:
Sub Delay()
Dim i As Integer
Dim s As Long

    Sheet1.Range("D2").Value = i
        For i = 30 To 0 Step -1
             Range("D2") = i
             s = Timer + 1
             Do While Timer < s
                DoEvents
            Loop
        Next

End Sub
 
Upvote 0
That's often the outcome with untested code but in this case I'd say it's a large oversight on my part. A variable would have to be declared at the module level:
VBA Code:
Option Explicit
Dim x As Integer

The following example will loop inside 5 times so I guess that's where you put the code that is for your updated files. The outer loop is for going over the sheet rows. I used 4 and 5 just to test that it works as I expected. So this is just how it might be constructed but you have to use your references and variable values of course.

VBA Code:
Private Sub Snippet()
Dim i As Integer, n As Integer
Dim LastRow As Integer

LastRow = 4
x = 1: n = 1
For n = x To LastRow
     For i = 1 To 5
          'do stuff here 5x
          x = x + 1
     Next
Delay
Next
End Sub

I prefer a timer sub that takes parameters so it can be used for anything else. Yours is fixed. This one will accept fractional seconds.
VBA Code:
Sub pause(sngSecs As Single)
Dim endTime As Single

endTime = Timer
Do Until Timer > endTime + sngSecs
   'Debug.Print Timer
Loop

End Sub
 
Upvote 0
Solution
That's often the outcome with untested code but in this case I'd say it's a large oversight on my part. A variable would have to be declared at the module level:
VBA Code:
Option Explicit
Dim x As Integer

The following example will loop inside 5 times so I guess that's where you put the code that is for your updated files. The outer loop is for going over the sheet rows. I used 4 and 5 just to test that it works as I expected. So this is just how it might be constructed but you have to use your references and variable values of course.

VBA Code:
Private Sub Snippet()
Dim i As Integer, n As Integer
Dim LastRow As Integer

LastRow = 4
x = 1: n = 1
For n = x To LastRow
     For i = 1 To 5
          'do stuff here 5x
          x = x + 1
     Next
Delay
Next
End Sub

I prefer a timer sub that takes parameters so it can be used for anything else. Yours is fixed. This one will accept fractional seconds.
VBA Code:
Sub pause(sngSecs As Single)
Dim endTime As Single

endTime = Timer
Do Until Timer > endTime + sngSecs
   'Debug.Print Timer
Loop

End Sub
This works nicely, thank you Micron!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
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