Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Rename and save without overwriting the exisiting file

I have a macro (given below) that saves a new workbook with the name
Book1. Next time when the macro is executed, how the code should be
tweaked such that the new workbook is saved in a different name? I
tried using "i" loop but that doesnt merge well in my case as this
part is a part of a huge code. I

In short, I would like to have a macro for the following algorithmn
for this one:
If the filename is already found, then add any character (number,
alphabet) to save it as a different file name.

For example, initial save gives Book1.xls.
Next save finds Book1.xls, the filename should be renamed as
Book11.xls or Book1a.
Next save finds Book1 and Book11/Book1a. So, the filename should be
renamed as Book111 or Book11a or....
So on....

Can someone please help me with the macro?

ActiveWorkbook.SaveAs Filename:= _
"C:\Book1.xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:= _
False, CreateBackup:=False

Thanks!
Thulasiram

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Rename and save without overwriting the exisiting file

Hi,

This should do the trick for you. Note that it is not necessary to supply
the .xls extension in the save part. Excel looks after that and it makes it
easier to create the new filename without contending with the file extension.

Sub Save_As_NewName()

Dim strInitName As String
Dim strNewName As String
Dim intAppend As Integer
Dim myFile As String

strInitName = "C:\Book1"
strNewName = strInitName

Do
myFile = Dir(strNewName & ".xls")
If myFile < "" Then
intAppend = intAppend + 1
strNewName = strInitName & intAppend
End If
Loop While myFile < ""

ActiveWorkbook.SaveAs Filename:= _
strNewName, FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:= _
False, CreateBackup:=False

End Sub

Regards,

OssieMac

"Thulasiram" wrote:

I have a macro (given below) that saves a new workbook with the name
Book1. Next time when the macro is executed, how the code should be
tweaked such that the new workbook is saved in a different name? I
tried using "i" loop but that doesnt merge well in my case as this
part is a part of a huge code. I

In short, I would like to have a macro for the following algorithmn
for this one:
If the filename is already found, then add any character (number,
alphabet) to save it as a different file name.

For example, initial save gives Book1.xls.
Next save finds Book1.xls, the filename should be renamed as
Book11.xls or Book1a.
Next save finds Book1 and Book11/Book1a. So, the filename should be
renamed as Book111 or Book11a or....
So on....

Can someone please help me with the macro?

ActiveWorkbook.SaveAs Filename:= _
"C:\Book1.xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:= _
False, CreateBackup:=False

Thanks!
Thulasiram


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Rename and save without overwriting the exisiting file

OssieMac!!!!!!!!

You rock!! This code definitely does the trick for me... Thanx a
zillionfor the quick response and helping me out! Can you please
explain what goin on the Do loop..

On Sep 6, 10:52 pm, OssieMac
wrote:
Hi,

This should do the trick for you. Note that it is not necessary to supply
the .xls extension in the save part. Excel looks after that and it makes it
easier to create the new filename without contending with the file extension.

Sub Save_As_NewName()

Dim strInitName As String
Dim strNewName As String
Dim intAppend As Integer
Dim myFile As String

strInitName = "C:\Book1"
strNewName = strInitName

Do
myFile = Dir(strNewName & ".xls")
If myFile < "" Then
intAppend = intAppend + 1
strNewName = strInitName & intAppend
End If
Loop While myFile < ""

ActiveWorkbook.SaveAs Filename:= _
strNewName, FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:= _
False, CreateBackup:=False

End Sub

Regards,

OssieMac

"Thulasiram" wrote:
I have a macro (given below) that saves a new workbook with the name
Book1. Next time when the macro is executed, how the code should be
tweaked such that the new workbook is saved in a different name? I
tried using "i" loop but that doesnt merge well in my case as this
part is a part of a huge code. I


In short, I would like to have a macro for the following algorithmn
for this one:
If the filename is already found, then add any character (number,
alphabet) to save it as a different file name.


For example, initial save gives Book1.xls.
Next save finds Book1.xls, the filename should be renamed as
Book11.xls or Book1a.
Next save finds Book1 and Book11/Book1a. So, the filename should be
renamed as Book111 or Book11a or....
So on....


Can someone please help me with the macro?


ActiveWorkbook.SaveAs Filename:= _
"C:\Book1.xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:= _
False, CreateBackup:=False


Thanks!
Thulasiram



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Rename and save without overwriting the exisiting file

To fit my query, I've added Workbooks.Add between the Do loop and the
ActiveWorkbook.SaveAs Filename:= _

Thanks OssieMac!

On Sep 6, 10:52 pm, OssieMac
wrote:
Hi,

This should do the trick for you. Note that it is not necessary to supply
the .xls extension in the save part. Excel looks after that and it makes it
easier to create the new filename without contending with the file extension.

Sub Save_As_NewName()

Dim strInitName As String
Dim strNewName As String
Dim intAppend As Integer
Dim myFile As String

strInitName = "C:\Book1"
strNewName = strInitName

Do
myFile = Dir(strNewName & ".xls")
If myFile < "" Then
intAppend = intAppend + 1
strNewName = strInitName & intAppend
End If
Loop While myFile < ""

ActiveWorkbook.SaveAs Filename:= _
strNewName, FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:= _
False, CreateBackup:=False

End Sub

Regards,

OssieMac

"Thulasiram" wrote:
I have a macro (given below) that saves a new workbook with the name
Book1. Next time when the macro is executed, how the code should be
tweaked such that the new workbook is saved in a different name? I
tried using "i" loop but that doesnt merge well in my case as this
part is a part of a huge code. I


In short, I would like to have a macro for the following algorithmn
for this one:
If the filename is already found, then add any character (number,
alphabet) to save it as a different file name.


For example, initial save gives Book1.xls.
Next save finds Book1.xls, the filename should be renamed as
Book11.xls or Book1a.
Next save finds Book1 and Book11/Book1a. So, the filename should be
renamed as Book111 or Book11a or....
So on....


Can someone please help me with the macro?


ActiveWorkbook.SaveAs Filename:= _
"C:\Book1.xls", FileFormat _
:=xlNormal, Password:="", WriteResPassword:="",
ReadOnlyRecommended:= _
False, CreateBackup:=False


Thanks!
Thulasiram



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default Rename and save without overwriting the exisiting file

Hi again,

Hope the comments in the code answers you question as to what is happening
in the loop.

Sub Save_As_NewName()

Dim strInitName As String
Dim strNewName As String
Dim intAppend As Integer
Dim myFile As String

'Note next two variables start with same value
strInitName = "C:\Book1"
strNewName = strInitName


Do
'Dir function searches for the file description
'which has been concatenated from strNewName
'plus the file extension (First search is for
'"C:\Book1.xls")
'if file found then myFile = filename so
'therefore it exists. If not found then myFile
'is empty or = ""

myFile = Dir(strNewName & ".xls")

'If myFile is not empty then increment intAppend
'and concatenate it with strInitName and save the
'concatenated string to strNewName so on the first
'loop strNewName would become "C:\Book11".
'Note that strInitName remains unchanged.
'On the next loop if "C:\Book11" exists then
'strNewName would become "C:\Book12".
If myFile < "" Then
intAppend = intAppend + 1
strNewName = strInitName & intAppend
End If

'Next line will cause the code to loop if myfile
'is not empty. If myFile is empty then it has not
'found the most recent strNewName therefore it does
'not exist and can be used as the new file name.
Loop While myFile < ""

ActiveWorkbook.SaveAs Filename:= _
strNewName, FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:= _
False, CreateBackup:=False

End Sub

Regards,

OssieMac




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 106
Default Rename and save without overwriting the exisiting file

On Sep 7, 12:22 am, OssieMac
wrote:
Hi again,

Hope the comments in the code answers you question as to what is happening
in the loop.

Sub Save_As_NewName()

Dim strInitName As String
Dim strNewName As String
Dim intAppend As Integer
Dim myFile As String

'Note next two variables start with same value
strInitName = "C:\Book1"
strNewName = strInitName

Do
'Dir function searches for the file description
'which has been concatenated from strNewName
'plus the file extension (First search is for
'"C:\Book1.xls")
'if file found then myFile = filename so
'therefore it exists. If not found then myFile
'is empty or = ""

myFile = Dir(strNewName & ".xls")

'If myFile is not empty then increment intAppend
'and concatenate it with strInitName and save the
'concatenated string to strNewName so on the first
'loop strNewName would become "C:\Book11".
'Note that strInitName remains unchanged.
'On the next loop if "C:\Book11" exists then
'strNewName would become "C:\Book12".
If myFile < "" Then
intAppend = intAppend + 1
strNewName = strInitName & intAppend
End If

'Next line will cause the code to loop if myfile
'is not empty. If myFile is empty then it has not
'found the most recent strNewName therefore it does
'not exist and can be used as the new file name.
Loop While myFile < ""

ActiveWorkbook.SaveAs Filename:= _
strNewName, FileFormat _
:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:= _
False, CreateBackup:=False

End Sub

Regards,

OssieMac


Perfect reply! Thanks a lot for clarifying the code.

Regards,
-T

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
rename current file save and delete original valve79 Excel Programming 0 January 13th 06 09:17 PM
How to set SAVE AS file name to equal A1 contents when rename file E Excel Discussion (Misc queries) 0 October 19th 05 08:36 PM
overwriting a file Jordan Shoderu Excel Programming 4 August 6th 04 11:46 AM
Macros in Excel - Save as and rename file Tom Ogilvy Excel Programming 0 August 13th 03 12:13 PM
Macros in Excel - Save as and rename file Bob Phillips[_5_] Excel Programming 0 August 13th 03 09:28 AM


All times are GMT +1. The time now is 12:39 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"