Variables in workbook naming procedures

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello All,
I am using the following to create a workbook. The string derived from Now() will be used as part of the name of the workbook
Code:
Workbooks.Add
With ActiveWorkbook.Worksheets("Sheet1").Range("E1")
   .FormulaR1C1 = "=NOW()"
   .NumberFormat = "0.000"
End With
Dim nowCell As Range
Set nowCell = ActiveWorkbook.Worksheets("Sheet1").Range("B1")
nowCell.Value = Range("E1").Value
nowCell.Offset(0, 1).Formula = "=LEFT(RC[-1],5)"
nowCell.Offset(0, 2).Formula = "=Right(Left(RC[-2],9),4)"
leftStr = nowCell.Offset(0,1).Value
rightStr = nowCell.Offset(0,2).Value
ActiveWorkbook.SaveAs thisBook.Path & "/" & leftStr & rightStr
 & ".xls"
Thus I never get two workbooks with the same name.
Q1: Sometimes Right(Left(RC[-2],9),4) replaces a zero with a decimal. It works fine in the environments where I have used it, but I thought a decimal was an illegal character for naming a workbook... I get something like: "406538.3.xls" should I take steps to eliminate this?
Q2. Does the variable nowCell survive after I name the workbook in the same procedure?
Thank you.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hello David,

That is a very unusual method of naming files. How do you retrieve the correct file you want later? :eeek:

Please see my thoughts below...

Q1: Sometimes Right(Left(RC[-2],9),4) replaces a zero with a decimal. It works fine in the environments where I have used it, but I thought a decimal was an illegal character for naming a workbook... I get something like: "406538.3.xls" should I take steps to eliminate this?

For Windows OS decimals are allowed in the filename. Since you are starting with the numeric value Now(), you might want to avoid using string manipulation functions to build a filename. That introduces unintended variables of the number format of cell B1 and perhaps the effect of trailing zeros.

You might consider deriving your filename directly within VBA using Numeric functions instead of using the worksheet cells to build the filename.

Q2. Does the variable nowCell survive after I name the workbook in the same procedure?

Yes, the variables would retain their values within the procedure after you name your new workbook including the Range variable nowCell. Those values will be lost when the procedure ends unless the variables are defined to have scope outside of the procedure.
 
Upvote 0
Thanks Jerry, for your comments,
Good to know that the variables survive the renaming of the book until we depart the module.
I will look at using VBA to do get the Now() values instead of the spreadsheet because I am sure it is much more efficient...FYI I am not too concerned about retrieving the files because I am using them to report errors and they will go to an archive folder where they will only be accessed manually.
Regards,
David
 
Upvote 0
Glad that helped, David.

Technically the variables survive until you depart the Procedure (Sub or Function) instead of the Module. But that's probably what you meant. ;)

Good to know that the variables survive the renaming of the book until we depart the module.
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,337
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top