Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4
Default Workbook name in VBA doesn't match filename.xls

I have a workbook named ABC.xls, in that workbook are several macros that
reference the currently named workbook ... ABC.xls.
Now, my users rename the workbook to 123.xls.
When they go to run the macro in the newly named 123.xls workbook the macro
fails because there are references to the old workbook name of ABC.xls.
Is there a way to avoid this .. one note, having the user NOT rename the
workbook is not an option.
Is there a way to use a variable to reference the workbook no matter what it
is named, pehaps ?
Thanks in advance,
Joe
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2,489
Default Workbook name in VBA doesn't match filename.xls

Hi,

The object Thisworkbook will refer to the workbook containing the code.
So you can use this to modify your code and the way it references the
workbook.

If you need further assistance it would help if you posted an example of
your code in which you use the reference to a specific workbook.

Cheers
Andy

--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
"JoeAdidas21" wrote in message
...
I have a workbook named ABC.xls, in that workbook are several macros that
reference the currently named workbook ... ABC.xls.
Now, my users rename the workbook to 123.xls.
When they go to run the macro in the newly named 123.xls workbook the
macro
fails because there are references to the old workbook name of ABC.xls.
Is there a way to avoid this .. one note, having the user NOT rename the
workbook is not an option.
Is there a way to use a variable to reference the workbook no matter what
it
is named, pehaps ?
Thanks in advance,
Joe


  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 11,058
Default Workbook name in VBA doesn't match filename.xls

Sub routine()
MsgBox (ThisWorkbook.Name)
End Sub

--
Gary''s Student - gsnu200756


"JoeAdidas21" wrote:

I have a workbook named ABC.xls, in that workbook are several macros that
reference the currently named workbook ... ABC.xls.
Now, my users rename the workbook to 123.xls.
When they go to run the macro in the newly named 123.xls workbook the macro
fails because there are references to the old workbook name of ABC.xls.
Is there a way to avoid this .. one note, having the user NOT rename the
workbook is not an option.
Is there a way to use a variable to reference the workbook no matter what it
is named, pehaps ?
Thanks in advance,
Joe

  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 4
Default Workbook name in VBA doesn't match filename.xls

Hi Andy,
Thanks for the quick reply... here is a sample of the VBA code.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 11/15/2007 by jc
'

' This macro resides in a workbook named ABC.xls.. and will eventually
jump/goto another workbook
' named DEF.xls... then the macro goes back to the workbook named ABC.xls.
' However, if I rename ABC.xls to a filename 123.xls.... this macro will
run but break
' on the line that reads "Windows ("ABC.xls").Activate because the
workbook is now called 123.xls

Range("A1").Select
ActiveCell.FormulaR1C1 = "This is the ABC workbook."
Range("A2").Select
Windows("DEF.xls").Activate
Range("A1").Select
ActiveCell.FormulaR1C1 = _
"This is the DEF Workbook€¦ now jumping back to the ABC workbook."
Range("A2").Select

' macro breaks here IF the workbook is not called ABC.xls
Windows("ABC.xls").Activate
ActiveCell.FormulaR1C1 = "Back to ABC workbook."
Range("A3").Select
End Sub




"Andy Pope" wrote:

Hi,

The object Thisworkbook will refer to the workbook containing the code.
So you can use this to modify your code and the way it references the
workbook.

If you need further assistance it would help if you posted an example of
your code in which you use the reference to a specific workbook.

Cheers
Andy

--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
"JoeAdidas21" wrote in message
...
I have a workbook named ABC.xls, in that workbook are several macros that
reference the currently named workbook ... ABC.xls.
Now, my users rename the workbook to 123.xls.
When they go to run the macro in the newly named 123.xls workbook the
macro
fails because there are references to the old workbook name of ABC.xls.
Is there a way to avoid this .. one note, having the user NOT rename the
workbook is not an option.
Is there a way to use a variable to reference the workbook no matter what
it
is named, pehaps ?
Thanks in advance,
Joe


  #5   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Workbook name in VBA doesn't match filename.xls

You can use:
Thisworkbook.activate
instead of:
Windows("ABC.xls").Activate

But better would be to stop the selects and activates.

'....

Activesheet.range("a1").value = "This is the ABC workbook."

Workbooks("Def.xls").worksheets("somesheetname").r ange("A1").value _
= "This is the DEF Workbook€¦ now jumping back to the ABC workbook."

Activesheet.range("A2").value = "Back to ABC workbook."

End Sub


JoeAdidas21 wrote:

Hi Andy,
Thanks for the quick reply... here is a sample of the VBA code.

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 11/15/2007 by jc
'

' This macro resides in a workbook named ABC.xls.. and will eventually
jump/goto another workbook
' named DEF.xls... then the macro goes back to the workbook named ABC.xls.
' However, if I rename ABC.xls to a filename 123.xls.... this macro will
run but break
' on the line that reads "Windows ("ABC.xls").Activate because the
workbook is now called 123.xls

Range("A1").Select
ActiveCell.FormulaR1C1 = "This is the ABC workbook."
Range("A2").Select
Windows("DEF.xls").Activate
Range("A1").Select
ActiveCell.FormulaR1C1 = _
"This is the DEF Workbook€¦ now jumping back to the ABC workbook."
Range("A2").Select

' macro breaks here IF the workbook is not called ABC.xls
Windows("ABC.xls").Activate
ActiveCell.FormulaR1C1 = "Back to ABC workbook."
Range("A3").Select
End Sub

"Andy Pope" wrote:

Hi,

The object Thisworkbook will refer to the workbook containing the code.
So you can use this to modify your code and the way it references the
workbook.

If you need further assistance it would help if you posted an example of
your code in which you use the reference to a specific workbook.

Cheers
Andy

--

Andy Pope, Microsoft MVP - Excel
http://www.andypope.info
"JoeAdidas21" wrote in message
...
I have a workbook named ABC.xls, in that workbook are several macros that
reference the currently named workbook ... ABC.xls.
Now, my users rename the workbook to 123.xls.
When they go to run the macro in the newly named 123.xls workbook the
macro
fails because there are references to the old workbook name of ABC.xls.
Is there a way to avoid this .. one note, having the user NOT rename the
workbook is not an option.
Is there a way to use a variable to reference the workbook no matter what
it
is named, pehaps ?
Thanks in advance,
Joe



--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 229
Default Workbook name in VBA doesn't match filename.xls

In the general case, it is best not to refer to anything with a string
literal. ThisWorkbook.Name is a good method, as people have pointed
out. If you must refer to multiple files, use string constants, or in
your master workbook, set up a sheet where you have named ranges
containing the strings, then initialize them at application startup -
Workbook_Open() or whatever your entry point is.


On Nov 15, 11:48 am, JoeAdidas21
wrote:
I have a workbook named ABC.xls, in that workbook are several macros that
reference the currently named workbook ... ABC.xls.
Now, my users rename the workbook to 123.xls.
When they go to run the macro in the newly named 123.xls workbook the macro
fails because there are references to the old workbook name of ABC.xls.
Is there a way to avoid this .. one note, having the user NOT rename the
workbook is not an option.
Is there a way to use a variable to reference the workbook no matter what it
is named, pehaps ?
Thanks in advance,
Joe


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
Saving XLS workbook with set filename levertonfamily Excel Discussion (Misc queries) 0 November 16th 06 05:52 PM
link to other workbook with changing filename DN Excel Worksheet Functions 6 August 30th 06 06:44 PM
set filename to <filename-date on open bob engler Excel Worksheet Functions 2 July 13th 06 05:11 AM
Filename in footer automatically in new workbook. cmorton89 Excel Discussion (Misc queries) 1 March 9th 06 12:52 PM
How do I remove multiple windows of 1 workbook (filename.xls:2,:3 Stacey D. Excel Discussion (Misc queries) 2 March 1st 06 09:57 PM


All times are GMT +1. The time now is 02:01 AM.

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"