New Laptop - VBA Script will not run using VBS file manually or via Task Scheduler

jaihawk8

Board Regular
Joined
Mar 23, 2018
Messages
69
Office Version
  1. 2016
Platform
  1. Windows
I just got a new work laptop, moving from Excel 2016 to Office 365). I had created a dashboard file on my old laptop that had 2 macros in it.

Refresh - this macro refreshed the two Power Query Tables
UpdateQuarter - this macro refreshed the pivot tables in the file based on the quarter

I had created a .vbs file that would Open the Excel file and run the two macros, then save and close the file. I used Tas Scheduler to have the .vbs file run every hour.

On my old laptop, this worked perfectly and with no issues.

On my new laptop, the Task Scheduler fails on this. I tried double clicking on the .vbs file to just run the script manually. When I do, it launches the Excel file, but gives me the following error message:

Run-time error '91':
Object variable With block variable not set.
When I click on Debug, it highlights this:

Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False

I can go into the Excel file myself and manually run the macros and they run just fine. I am running everything under myself. I have even tried copying the file and just moving it to my C Drive and updating the file path to that and still get the message.

Here is the .vbs file:

VBA Code:
'Create Excel App Instance & Open Xlsm File

Set objExcelApp = CreateObject("Excel.Application")

objExcelApp.Visible = True

objExcelApp.DisplayAlerts = False

'Define Macro File & Path

sFilePathXlsm = "C:\Users\jbohl\OneDrive - Quadient\Central District\2024 Payroll Files\Test Files\Dashboard.xlsm"

Set iWb =
[URL='https://objexcelapp.workbooks.open/']objExcelApp.Workbooks.Open[/URL](sFilePathXlsm)

'1. Run 1st Macro in another Excel

sMacroToRun = "'" & sFilePathXlsm & "'!Refresh"

[URL='https://objexcelapp.run/']objExcelApp.Run[/URL] sMacroToRun

'2. Run 2nd Macro in same file

sMacroToRun = "'" & sFilePathXlsm & "'!UpdateQuarter"

[URL='https://objexcelapp.run/']objExcelApp.Run[/URL] sMacroToRun

'Save & Close file

[URL='https://iwb.save/']iWb.Save[/URL]

iWb.Close

objExcelApp.DisplayAlerts = True

objExcelApp.Quit
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.

Forum statistics

Threads
1,224,885
Messages
6,181,577
Members
453,055
Latest member
cope7895

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