Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default How to Activate unknown file name

Hi,
I am creating a macro where the user will choose the xls
file to open. I then want to activate that file, but am
having some trouble figuring out how to write code to
specify which workbook to activate since the user will
choose the workbook and therefore I don't know it
beforehand.
Here is the code so far:

Dim fileToOpen
Dim fileName

fileToOpen = Application _
.GetOpenFilename("Microsoft Excel Files (*.xls),
*.xls")
Workbooks.Open fileName:=fileToOpen
'Workbooks("*****").Activate


I am not sure what to put where the asteriks are in the
last line of code.
I was thinking of finding the filename in the file path by
using the InStrRev to find where the \ is and take the
name between the \ and .xls, such as
InStrRev(fileToOpen, "\")
However, I thought there might be a better way that I
don't know about.

Any help would be much appreciated!
Thanks!
Ashley
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default How to Activate unknown file name

Hi Ashley,

If you want to be able to refer to the new workbook you've opened, you can
use an object variable to do this:

Dim wb As Workbook

Set wb = Workbooks.Open(Filename:=fileToOpen)

wb.Activate

'/ other stuff here

wb.Close SaveChanges:=False
Set wb = Nothing

That said, you shouldn't need to activate the workbook, as it is activated
when you open it. Also, activating and selecting workbooks, worksheets, and
ranges is typically unnecessary - most things can be done without modifying
the selection.

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Ashley wrote:
Hi,
I am creating a macro where the user will choose the xls
file to open. I then want to activate that file, but am
having some trouble figuring out how to write code to
specify which workbook to activate since the user will
choose the workbook and therefore I don't know it
beforehand.
Here is the code so far:

Dim fileToOpen
Dim fileName

fileToOpen = Application _
.GetOpenFilename("Microsoft Excel Files (*.xls),
*.xls")
Workbooks.Open fileName:=fileToOpen
'Workbooks("*****").Activate


I am not sure what to put where the asteriks are in the
last line of code.
I was thinking of finding the filename in the file path by
using the InStrRev to find where the \ is and take the
name between the \ and .xls, such as
InStrRev(fileToOpen, "\")
However, I thought there might be a better way that I
don't know about.

Any help would be much appreciated!
Thanks!
Ashley


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to activate the permission option in the file tab in excel? trying Setting up and Configuration of Excel 0 March 20th 06 02:49 PM
How to auto activate macro when file open ? bonzio Excel Worksheet Functions 1 December 16th 05 02:45 PM
Activate new workbook with unknown name rt10516 Excel Discussion (Misc queries) 3 August 24th 05 05:40 AM
activate workbook w/unknown name Jim Cottrell Excel Discussion (Misc queries) 2 July 30th 05 12:31 AM
Macro to activate a Batch file ! Tarek[_2_] Excel Programming 3 October 13th 03 08:02 PM


All times are GMT +1. The time now is 11:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"