VBA: Loop calling another sub routine

pujo

Well-known Member
Joined
Feb 19, 2009
Messages
708
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
My goal is to calculate a range every 30 seconds
Command button starts and stops the timer to calculate the range and I am getting my desired result however...
I want to paste the range values to next available column to the right after each update.

With there being a loop statement, I am not sure how to accomplish this.
Attempts have failed with continuous copy paste in all columns. lol

Hope someone can assist.

My AutoUpdate routine
VBA Code:
Sub AutoUpdate()
    Dim RunningTime As Double
    Dim TimerStart  As Double

    TimerStart = Timer

    Do Until Sheets("Sheet1").Range("A1").Value = False
        RunningTime = (Timer - TimerStart) / 24 / 60 / 60
        If (Format(RunningTime, "ss") Mod 30) = 0 Then Sheets("Sheet1").Range("Data").Calculate
        Sheets("Sheet1").Range("TimeStamp").Calculate
        DoEvents
    Loop
End Sub

My copy/paste routine
VBA Code:
Sub CopyPasteData()
    Dim Data        As Range
    Dim lastCol     As Long
    Set Data = Worksheets("Sheet1").Range("Data")

    lastCol = Cells(2, Columns.Count).End(xlToLeft).Column

    Range("Data").Copy
    Cells(2, lastCol + 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    Application.CutCopyMode = False
End Sub
 

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"
Check if the following is what you need:

VBA Code:
Sub AutoUpdate()
  Dim sh As Worksheet
  Dim RunningTime As Double
  Dim TimerStart  As Double
  Dim lastCol     As Long
  
  Set sh = Sheets("Sheet1")
  TimerStart = Timer
  
  Do Until sh.Range("A1").Value = False
    RunningTime = (Timer - TimerStart) / 24 / 60 / 60
    If (Format(RunningTime, "ss") Mod 30) = 0 Then
      lastCol = sh.Cells(2, Columns.Count).End(xlToLeft).Column + 1
      sh.Range("Data").Calculate
      sh.Range("Data").Copy
      sh.Cells(2, lastCol).PasteSpecial xlPasteValues
      Application.CutCopyMode = False
    End If
    sh.Range("TimeStamp").Calculate
    DoEvents
  Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,927
Messages
6,122,309
Members
449,080
Latest member
jmsotelo

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