VBA code for opening 2 files in different file paths

blueeagles003

New Member
Joined
Feb 27, 2019
Messages
1
Hi All,

I am new to VBA and I am trying to have one sub open 2 files that are stored in different file paths. The below is my current code:

Sub ExceptionReportSCV()
'
' ExceptionReportSCV Macro
'
' Keyboard Shortcut: Ctrl+Shift+T
'
Workbooks.Open Filename:=Sheets("Execute").Range("F1")
Workbooks.Open Filename:=Sheets("Execute").Range("F2")



And the below is what is being referenced in sheet "Execute" within cells F1 & F2:

cell F1 is: S:\Institutional Trading\Automated Reports\Model Exception Reports\Small Cap Value\SCV_Model_Exc_Report_02272019.xlsx

cell F2 is: S:\Institutional Trading\Daily Sign Offs\Exception Reports\SCV\SCV_Model_Exc_Report_02262019.xlsx

When running my macro I get a Run-time error '9': Subscript out of range. The first workbook referencing cell F1 opens successfully but the second command calling cell F2 fails to open. Curiously, when I try to have the macro just call the file path in F1 or F2 they both work successfully, but when I have the two commands under the same Sub, the second command fails. Does anyone have any insight to how I can rework this code to have both files open through one sub? Thank you!
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
The problem is because when you open the first book, in that book the "exeute" sheet does not exist, that's why it sends you the error.

Try:

Code:
    Set sh = Sheets("Execute")
    Workbooks.Open Filename:=sh.Range("F1")
    Workbooks.Open Filename:=sh.Range("F2")

Or :

Code:
    Workbooks.Open Filename:=ThisWorkbook.Sheets("Execute").Range("F1")
    Workbooks.Open Filename:=ThisWorkbook.Sheets("Execute").Range("F2")
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

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