VBA Copy, Paste to Next Column, Repeat

JonesyUK

Board Regular
Joined
Oct 11, 2005
Messages
183
Office Version
  1. 365
Platform
  1. Windows
Hi All,

Please can you help?

I have copied the following Refresh1 macro 65 times... (Refresh1 to Refresh 65)

It updates info from the internet, then copies the data every 15 minutes from "NewData" to a new column (starting at "D4:D24" and ending at column "BP4:BP24". i.e. it runs all day from 8am to 11pm.

Please can you suggest a better way of doing this than having 65 macros?? the only change in each macro is Range("D4:D24").Select

Thank You!

Sub Refresh1()
ThisWorkbook.RefreshAll
Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 5) [small delay to update numbers before copy]
Worksheets("Start").Activate
Range("NewData").Select
Selection.Copy
Range("D4:D24").Select [next macro uses "E4:E24", then "F4:F24" etc... ]
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Worksheets("Analysis").Activate
ActiveSheet.Range("H1").Select
Application.Wait TimeSerial(Hour(Now), Minute(Now) + 15, Second(Now))
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

fadee2

Active Member
Joined
Nov 7, 2020
Messages
367
Office Version
  1. 2019
Platform
  1. Windows
try it on a copy of your workbook

VBA Code:
Sub Refresh1()
For i = 4 To 69
    ThisWorkbook.RefreshAll
    Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 5) '[small delay to update numbers before copy]
    Worksheets("Start").Activate
    Range("NewData").Select
    Selection.Copy
    Range(Cells(4, 4), Cells(24, i)).Select '[next macro uses "E4:E24", then "F4:F24" etc... ]
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False
    Worksheets("Analysis").Activate
    ActiveSheet.Range("H1").Select
    Application.Wait TimeSerial(Hour(Now), Minute(Now) + 15, Second(Now))
Next i
End Sub
 
Last edited:

JonesyUK

Board Regular
Joined
Oct 11, 2005
Messages
183
Office Version
  1. 365
Platform
  1. Windows
try it on a copy of your workbook

VBA Code:
Sub Refresh1()
For i = 4 To 69
    ThisWorkbook.RefreshAll
    Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 5) '[small delay to update numbers before copy]
    Worksheets("Start").Activate
    Range("NewData").Select
    Selection.Copy
    Range(Cells(4, 4), Cells(24, i)).Select '[next macro uses "E4:E24", then "F4:F24" etc... ]
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
    Application.CutCopyMode = False
    Worksheets("Analysis").Activate
    ActiveSheet.Range("H1").Select
    Application.Wait TimeSerial(Hour(Now), Minute(Now) + 15, Second(Now))
Next i
End Sub
Thanks, it's giving error, variable not defined... is it "Dim i as long" ?
 

Osvaldo Palmeiro

Well-known Member
Joined
Feb 24, 2009
Messages
690
Office Version
  1. 365
Platform
  1. Windows
Another approach:
VBA Code:
Sub Refresh1()
  ThisWorkbook.RefreshAll
  Application.Wait TimeSerial(Hour(Now), Minute(Now), Second(Now) + 5) '[small delay to update numbers before copy]
   Sheets("Start").Range("NewData").Copy
   If Sheets("Start").[D4] = "" Then
    Sheets("Start").[D4].PasteSpecial xlValues
   Else: Sheets("Start").Cells(4, Columns.Count).End(1)(1, 2).PasteSpecial xlValues '[next macro uses "E4:E24", then "F4:F24" etc... ]
   End If
  Application.CutCopyMode = False
  Application.Wait TimeSerial(Hour(Now), Minute(Now) + 15, Second(Now))
End Sub
 
Solution

Forum statistics

Threads
1,141,126
Messages
5,704,441
Members
421,349
Latest member
Santhosh3188

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