Add 001,002 strings to filename

dantheman9

Board Regular
Joined
Feb 5, 2011
Messages
175
Hi I have some files that need to saved in number order with a three number string eg 001, 002, 003 etc.

two questions, in the code below (in the loop) how can i increment the string number by one each time?
secondly, you will see Im using the getsaveasfilename, if there a method to add this number after that option (if so the user cannot remove this part of the file name?

Code:
Dim rng As Range
Dim wb As Workbook
Dim Fname As Variant
Dim filenumber As Variant
Set rng = Range("A1:A211")
Set wb = Workbooks.Add
filenumber = "001"
[COLOR=red]If Len(Dir(Path & filenumber & "*")) > 0 Then
Do While Len(Dir(Path & filenumber & "*")) > 0
filenumber = filenumber + 1
Loop
[/COLOR]End If

With wb
rng.Copy
.Worksheets(1).Range("A1").PasteSpecial Paste:=xlValues
' open path as set in path option box
ChDir (Path)
Application.DisplayAlerts = False
' set file name from textbox 1 (default is enter file name
Fname = Application.GetSaveAsFilename( _
[COLOR=red]InitialFileName:=filenumber & " - " & TextBox1,[/COLOR] _
[COLOR=red]' can i add the filenumber after this so the user cannot mod it?
[/COLOR]filefilter:="Text File, *.txt", _
Title:="Save File To...")
If Fname <> False Then
.SaveAs Fname, xlTextMSDOS
End If
.Close False
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
To get the filenumber to increment with the padded zeros, 001, 002, 003 ..., you can use the Format function. So
Code:
    [COLOR="red"]Dim filenumber As Integer
    filenumber = 1[/COLOR]
    
    If Len(Dir(Path & [COLOR="red"]Format(filenumber, "000")[/COLOR] & "*")) > 0 Then
        Do While Len(Dir(Path & [COLOR="red"]Format(filenumber, "000")[/COLOR] & "*")) > 0
            filenumber = filenumber + 1
        Loop
    End If
I assume the above is to find the lowest unused file in the Path Folder.

To have the file number add after the save as dialog box you could do this.
Code:
        ' set file name from textbox 1 (default is enter file name
        Fname = Application.GetSaveAsFilename( _
            InitialFileName:=[COLOR="Red"]TextBox1[/COLOR], _
            FileFilter:="Text File, *.txt", _
            Title:="Save File To...")
            
        If Fname <> False Then
           [COLOR="Red"] Fname = Format(filenumber, "000") & " - " & Fname[/COLOR]
            .SaveAs Fname, xlTextMSDOS

Where the File number is added after the save as dialog. I also removed the file number & " - " from the default file name to avoid doubling the file number (ex. 001 - 001 - File Name.txt)

You may not need the GetSaveAsFilename method instead you could use the value from the textbox1. That of course depends on your needs.
 
Upvote 0

Forum statistics

Threads
1,224,609
Messages
6,179,875
Members
452,949
Latest member
Dupuhini

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