ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Need help - Filesaving - need to increment filename (https://www.excelbanter.com/excel-programming/328593-need-help-filesaving-need-increment-filename.html)

Guillaume[_2_]

Need help - Filesaving - need to increment filename
 

Greetings, i need to modify this code to be able to increment the file
name each time i save the file.

for example when i save it to silo001.txt
i want the next one to be silo002.txt , etc..

Here is the code right now ;
----
Sub SaveCmde()

ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\guicha\bureau\cano\silo001.txt",
FileFormat:=xlTextMSDOS, CreateBackup:=False
Range("A21").Select

End Sub
----
I know you guys rock and you will help me out,
im more into hardware so thanks for your help !

Guillaume.




*** Sent via Developersdex http://www.developersdex.com ***

Bob Phillips[_6_]

Need help - Filesaving - need to increment filename
 
One way

Sub SaveCmde()
Const sFilebase = "C:\Documents and Settings\guicha\bureau\cano\silo"
Dim sFile As String
Dim iNum As Long
Dim fExists As Boolean

Do
sFile = sFilebase & Format(iNum, "000") & ".txt"
fExists = Len(Dir(sFile))
If Not fExists Then
ActiveWorkbook.SaveAs Filename:=sFile, _
FileFormat:=xlTextMSDOS, _
CreateBackup:=False
Range("A21").Select
End If
Loop Until Not fExists

End Sub



--

HTH

RP
(remove nothere from the email address if mailing direct)


"Guillaume" wrote in message
...

Greetings, i need to modify this code to be able to increment the file
name each time i save the file.

for example when i save it to silo001.txt
i want the next one to be silo002.txt , etc..

Here is the code right now ;
----
Sub SaveCmde()

ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\guicha\bureau\cano\silo001.txt",
FileFormat:=xlTextMSDOS, CreateBackup:=False
Range("A21").Select

End Sub
----
I know you guys rock and you will help me out,
im more into hardware so thanks for your help !

Guillaume.




*** Sent via Developersdex http://www.developersdex.com ***




Stevie_mac

Need help - Filesaving - need to increment filename
 
Sub SaveCmde()
Dim sFN As String
Static i As Integer
i = i + 1
sFN = "C:\Documents and Settings\guicha\bureau\cano\silo" & _
Format(i, "000.txt")
ActiveWorkbook.SaveAs sFN, xlTextMSDOS
Range("A21").Select
End Sub

NOTE: this will reset to 001 when you close & re-open the book.

If this is a problem, you could use a registry entry instead...

Sub SaveCmde()
Dim sFN As String
Dim i As Integer
i = GetSetting("SiloExport", "Settings", "LastSave", 0)
i = i + 1
sFN = "C:\Documents and Settings\guicha\bureau\cano\silo" & _
Format(i, "000.txt")
ActiveWorkbook.SaveAs sFN, xlTextMSDOS
SaveSetting "SiloExport", "Settings", "LastSave", i
Range("A21").Select
End Sub

Finally, neither of these will check the directory exists - or check to see if the file exists - or indeed, wether
silo002.txt is the next on in the sequence. To do that will take a bit more time & effort.

Regards - Steve.

"Guillaume" wrote in message ...

Greetings, i need to modify this code to be able to increment the file
name each time i save the file.

for example when i save it to silo001.txt
i want the next one to be silo002.txt , etc..

Here is the code right now ;
----
Sub SaveCmde()

ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\guicha\bureau\cano\silo001.txt",
FileFormat:=xlTextMSDOS, CreateBackup:=False
Range("A21").Select

End Sub
----
I know you guys rock and you will help me out,
im more into hardware so thanks for your help !

Guillaume.




*** Sent via Developersdex http://www.developersdex.com ***




Guillaume[_2_]

Need help - Filesaving - need to increment filename
 
You guys Rock !!

Works like a charm, i used the registry option, fits what i needed to
do.

i had to modify this line also for your info ;

sFN = "C:\Documents and Settings\guicha\bureau\cano\silo" & _
Format(i, "000.txt")


need to remove the .txt otherwise the file says silo001,txt.txt

Thanks again.

Guillaume.



*** Sent via Developersdex http://www.developersdex.com ***


All times are GMT +1. The time now is 09:41 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com