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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,216,084
Messages
6,128,721
Members
449,465
Latest member
TAKLAM

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