Need File Name

Montez659

Well-known Member
Joined
May 4, 2005
Messages
918
I am trying to open a spreadsheet and read from that sheet except I am encountering a "subscript out of range" error on the
Code:
Set whatBook = Workbooks(myFile)
portion of what you see below. I figure that is because I am trying to reference the full path and not the file name, so how do I get the file name exculding path? Or am I off base?

Code:
myFile = Application.GetOpenFilename("Microsoft Excel Files (*.xls*), *.xls*")
Workbooks.Open (myFile)
skippedLines = False
'On Error GoTo skipIt
Set whatBook = Workbooks(myFile)

With whatBook.Sheets("Sheet1")...
 

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
Try like this

Code:
myFile = Application.GetOpenFilename("Microsoft Excel Files (*.xls*), *.xls*")
Set whatbook = Workbooks.Open(myFile)
skippedLines = False
 
Upvote 0
A workbook object variable refers to a workbook in memory, not a workbook on disk, so you omit the path and just use the name.

The Open method returns the opened workbook, so you can set the variable when you do the Open:
Code:
Set whatBook = Workbooks.Open(myFile)
 
Upvote 0
Boom! Thanks gentlemen. You wouldn't believe all the posts I searched through and I still couldn't find that.
 
Upvote 0

Forum statistics

Threads
1,217,451
Messages
6,136,714
Members
450,025
Latest member
Beginner52

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