Loop macro X number of times and restart.

DButler1

New Member
Joined
Apr 19, 2020
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I have a macro that is capturing data once a minute. I would like the macro to run for 180 times, then start over for another 180 times. This is what I have so far, just need it to loop the 180 times... i think.
VBA Code:
RunTimer = Now + TimeValue("00:00:10")
'=====================================
' Updates PI links


ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks


Application.OnTime RunTimer, "CopyData"
'=====================================
'Copy and move Data

' 1A

    Range("D10:D188").Select
    Selection.Copy
    Range("D11").Select
    ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
        IconFileName:=False
    Range("D4").Select
    Selection.Copy
    Range("D10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
' 1B

    Range("E10:E188").Select
    Selection.Copy
    Range("E11").Select
    ActiveSheet.PasteSpecial Format:=3, Link:=1, DisplayAsIcon:=False, _
        IconFileName:=False
    Range("D5").Select
    Selection.Copy
    Range("E10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Is this what you want ?
When SetTimer is run, CopyData is run 180 times and then run again 180 times

Rich (BB code):
Option Explicit
Private Count180 As Long, Run2 As Boolean

Sub SetTimer()
    Dim RunTimer
    If Count180 < 180 Then
        RunTimer = Now + TimeValue("00:00:10")
        ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, Type:=xlExcelLinks
        Application.OnTime RunTimer, "CopyData"
        Count180 = Count180 + 1
    Else
        If Run2 Then
            Run2 = False
            Exit Sub
        Else
            Count180 = 0
            Run2 = True
            SetTimer
        End If
    End If
End Sub

Private Sub CopyData()
'copy the data
    PLACE CODE TO COPY DATA HERE
'start the timer again
    Call SetTimer
       
End Sub
 
Upvote 0
EXCELLENT!!! thank you.

Now, at the end of the 180 macro runs, I need to select the range D10:E189, and clear it so the new values can start populating for the 3 hours. Where would I need to insert this clearcontents line in the SetTimer code?
 
Upvote 0
Rich (BB code):
    Else
        Range("D10:E189").ClearContents
        If Run2 Then
 
Upvote 0
Thank you, I will insert it in the morning and give it a whirl!
 
Upvote 0
MONEY!!!!! Works perfectly!

Thank you so much for your help. Much appreciated!!!
 
Upvote 0
Thanks for your feedback
Glad it worked :)
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,939
Members
449,094
Latest member
teemeren

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