Activate worksheet with variable name

Herbiec09

Active Member
Joined
Aug 29, 2006
Messages
250
Hi All,

Can anyone assist with the last line of the below code, not quite sure what's wrong with the statement, but it comes out highlighted in yellow when i try to run it:

Dim fName As Variant
Dim wb As Workbook
Dim file_name As Variant
fName = Application.GetOpenFilename(FileFilter:="Excel Files (*.XLSX), *.XLSX", Title:="Select File To Be Opened")
If fName = False Then Exit Sub
Workbooks.Open Filename:=fName
Set wb = Workbooks(fName & ".xlsx")

Thanks
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Once the book is open, you no longer need the Path in the workbooks command.
fName contains the path given during teh GetOpeFielname process..

You can set the variable at the same time you open the book.

Try changing

Workbooks.Open Filename:=fName
Set wb = Workbooks(fName & ".xlsx")

to

Set wb = Workbooks.Open(fName)
 
Upvote 0
Once the book is open, you no longer need the Path in the workbooks command.
fName contains the path given during teh GetOpeFielname process..

You can set the variable at the same time you open the book.

Try changing

Workbooks.Open Filename:=fName
Set wb = Workbooks(fName & ".xlsx")

to

Set wb = Workbooks.Open(fName)

Hi Jonmo1

Thanks for the above, but it produces a Compile error Expected: End Statement
 
Upvote 0
You may have used the first code I posted (thought I edited it fast enough)
Try it again with the new code after I edited it..


Set wb = Workbooks.Open(fName)
 
Upvote 0

Forum statistics

Threads
1,203,075
Messages
6,053,392
Members
444,661
Latest member
liamoohay

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