LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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

 
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 10:54 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"