View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bill Lunney Bill Lunney is offline
external usenet poster
 
Posts: 68
Default Variable Filename/Worksheet/Workbook

Try

my2ndfilename = Application.GetOpenFilename("All Files (*.*), *.*")
If my2ndfilename < "" Then
Workbooks.Open my2ndfilename
Workbooks(Workbooks.Count).Activate
End If

The newly loaded workbook is always going to be last in the list so by using
workbooks.count we're always going to activate the newly opened book.

You are refering to workbooks here not worksheets. A workbook is a
collection of worksheets (and other things). Trouble with getting the
filename is as you've found it is a fully qualified name with a path and
filename extension etc. It's just the filename iteself (without extension)
which the name given to the workbook.

Remember to also check to see if the user didn't hit cancel. That's what
the < "" check is for.


--

Regards,


Bill Lunney
www.billlunney.com

"bw" wrote in message
...
I'm using the following code to get the filename I want to open:

my2ndFileName = Application.GetOpenFilename("All Files (*.*), *.*")
where the actual filename is "C:\Downloads\LAS6014"

and I use the following to open the file:
Workbooks.OpenText Filename:=my2ndFileName,...

and because I don't now how to use the variable filename in the following,

I hard code it
as follows:
Windows("LAS6014").Activate

So the question is: how do I activate the worksheet (or is it a workbook)

for the variable
filename?

Thanks,
Bernie