ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Activating Workbooks (https://www.excelbanter.com/excel-discussion-misc-queries/232908-activating-workbooks.html)

Sungibungi

Activating Workbooks
 
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.

Jim Thomlinson

Activating Workbooks
 
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



--
HTH...

Jim Thomlinson


"Sungibungi" wrote:

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.


Sungibungi

Activating Workbooks
 
Works like a charm. Thanks for the help.

"Jim Thomlinson" wrote:

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



--
HTH...

Jim Thomlinson


"Sungibungi" wrote:

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.



All times are GMT +1. The time now is 08:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com