VBA Copy, Paste to Next Column, Repeat

JonesyUK

Board Regular
Joined
Oct 11, 2005
Messages
190
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

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
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:
Upvote 0
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" ?
 
Upvote 0
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
 
Upvote 0
Solution

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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