There are two ways you can tackle this. In fact this answer will cover both and in detail so please be patient with me. I am going to go at lengths with this answer as I am sure this will definitely help future visitors.
WAY 01: VB Script + Windows Task Scheduler
SETTING UP VB Script
- Open Notepad
- Paste this code
Code:
Dim ExcelApp, ExcelWB
Set ExcelApp = CreateObject("Excel.Application")
'~~> Change this to the relevant Excel File
Set ExcelWB = ExcelApp.Workbooks.Open("C:\Users\routs\Desktop\Sample.xlsm")
ExcelApp.Run "Refresh"
ExcelWB.Close True
ExcelApp.Quit
Set ExcelApp = Nothing
- Save the file as
MyTask.Vbs
![enter image description here]()
SETTING UP TASK SCHEDULER (Win 10)
Start the Task Scheduler in Windows. If you do not know how, then type Task Scheduler in Windows search.
![enter image description here]()
Click on Create Task under Actions and then fill up basic details in General Tab
![enter image description here]()
Set the relevant settings in the Trigger Tab
![enter image description here]()
Next in Action tab, create a new action and choose relevant details
![enter image description here]()
Similarly check out other tabs and see if you need to set up anything else
SETTING UP EXCEL MACRO
Paste this code in a module
Option Explicit
Sub Refresh()
Dim wsCopyFrom As Worksheet
Dim wsCopyTo As Worksheet
Dim lastCol As Long
ThisWorkbook.RefreshAll
Set wsCopyFrom = ThisWorkbook.Sheets("Collection")
Set wsCopyTo = ThisWorkbook.Sheets("Chart")
'~~> Find the next empty column where data will be pasted
lastCol = wsCopyTo.Cells(1, wsCopyTo.Columns.Count).End(xlToLeft).Column + 1
wsCopyFrom.Range("D10,J10").Copy
DoEvents
wsCopyTo.Cells(2, lastCol).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
DoEvents
End Sub
And we are done.
WAY 02: HANDLING EVERYTHING FROM EXCEL
Paste this code in a module in Excel (Untested)
Sub Refresh()
Dim wsCopyFrom As Worksheet
Dim wsCopyTo As Worksheet
Dim lastCol As Long
ThisWorkbook.RefreshAll
Set wsCopyFrom = ThisWorkbook.Sheets("Collection")
Set wsCopyTo = ThisWorkbook.Sheets("Chart")
'~~> Find the next empty column where data will be pasted
lastCol = wsCopyTo.Cells(1, wsCopyTo.Columns.Count).End(xlToLeft).Column + 1
wsCopyFrom.Range("D10,J10").Copy
DoEvents
wsCopyTo.Cells(2, lastCol).PasteSpecial _
Paste:=xlPasteValues, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
DoEvents
'~~> Do not do anything after 5 PM
If Now < Date + TimeValue("17:00:00") Then
Application.OnTime Now + TimeValue("00:15:00"), "Refresh"
End If
End Sub
At 9 AM just run the procedure once.
INTERESTING READ: How to avoid using Select in Excel VBA