Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

saving an excel file with a serially increnmenting number

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rd - 29 Sep 2007 22:05 GMT
Hi,

Can you please direct me to code that allows me to save my excel files in a
serially incrementing default name, such as book201, book203, book204, etc.
as you are aware, excel always go back to book1 whenever you start a new
cession, and it doesn't know that the last book was, say book204, and that
therefore, it should suggest the next higher number, namely book205.

i hope my request is clear.

Thanks in advance.

RD
OssieMac - 29 Sep 2007 23:02 GMT
Hi,

Here is a macro that I have posted a couple of time here and I think that it
will do what you want. Fee free to get back to me if it need a little
tweeking.

Sub Save_As_NewName()
Dim strInitName As String
Dim strNewName As String
Dim intAppend As Integer
Dim myFile As String
Dim strPath

'Set the next line to required file path
strPath = Application.DefaultFilePath
'Note next two variables start with same value
strInitName = strPath & "\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
   If intAppend > 3 Then
       MsgBox "Already " & intAppend & _
           " saves of this file" & Chr(13) & _
           "Processing terminated"
           'Insert your code here to handle the
           'excessive saves.
           End
   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
rd - 30 Sep 2007 13:44 GMT
Many thanks and best regards.

RD

> Hi,
>
[quoted text clipped - 65 lines]
>
> OssieMac
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.