Activating Workbooks

S

Sungibungi

I have opened up a file using the following code:

Dim fName As String
fName = Application.GetOpenFilename()
Workbooks.Open Filename:=fName

Then I switch to a different workbook to, tranferring data. When done, I
need to go back to this "fName" worksheet.

The logical code (at least to me...) Worksheets(fName).activate obviously
doesn't seem to be working. How do I make this happen?

Thanks so much in advance.
 
J

Jim Thomlinson

fname is the complete path and file name. The code you have to activate
expects only the file name. So you can either parse out the file name with
instrev looking for the \ or easier still set a workbook object with makes
referncing easy...

Dim wbk As Workbook
Dim fName As String

fName = Application.GetOpenFilename()
on error resume next
Set wbk = Workbooks.Open(Filename:=fName)
on error goto 0
if wbk is nothing then
msgbox "No file opened"
exit sub
end if
ThisWorkbook.Activate
wbk.Activate
 
S

Sungibungi

Works like a charm. Thanks for the help.

Jim Thomlinson said:
fname is the complete path and file name. The code you have to activate
expects only the file name. So you can either parse out the file name with
instrev looking for the \ or easier still set a workbook object with makes
referncing easy...

Dim wbk As Workbook
Dim fName As String

fName = Application.GetOpenFilename()
on error resume next
Set wbk = Workbooks.Open(Filename:=fName)
on error goto 0
if wbk is nothing then
msgbox "No file opened"
exit sub
end if
ThisWorkbook.Activate
wbk.Activate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Open files from List 9
VBA Formula Help 6
Worksheet.activate 1
Listbox in VBA 1
Sub out of range 1
5941 The requested member of the collection does not exist...Please help! 0
Excel activate in excel vba 0
Copy Cells 2

Top