Compile error "Subscript out of range" in simple set statement

ibud00

New Member
Joined
Aug 15, 2015
Messages
4
I'm obviously missing a fundamental concept. I have reduced my problem down to a few lines of code, and still can't solve it.

Code:
Sub tstSbScrptRng()


    Dim wkBk As Workbook
    Dim sPath As String
    Dim sFile As String
    
    sPath = "C:\Users\ibud0\Documents\"
    sFile = "Garbage.xlsx"
    
    Set wkBk = Workbooks(sPath & sFile)
'   Last statement gets "Subscript out of range" compile error message
    
End Sub

I'm drowning - please help!
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
If the workbook is open, you should be using:

Code:
Set wkBk = Workbooks(sFile)

If not, you need to open it:

Code:
Set wkBk = Workbooks.Open(sPath & sFile)
 
Upvote 0
If the workbook isn't already open then you need:

Code:
Set wkBk = Workbooks.Open(sPath & sFile)

If the workbook is already open then you need:

Code:
Set wkBk = Workbooks(sFile)

WBD
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,930
Members
449,195
Latest member
Stevenciu

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