VBA Save file as numbered sequence

dknipe

New Member
Joined
Jan 18, 2018
Messages
13
Using VBA, how do I search a folder for files referencing a number (not a cell value) like AK001.txt, AK002.txt etc, and save current worksheet as the next number in sequence eg AK003.txt
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
This seems to work. U will have to change the folder path. I'm not sure about the saveas line of code and this might require change. HTH. Dave
Code:
Private Sub test2()
'AK003.txt format
Dim fso As Object, FolDir As Object, FileNm As Object, NumStr As Integer, MaxNum As Integer
Dim NewName As String, FolderStr As String, StrNum As String, MaxStr As String
MaxNum = 1
FolderStr = "C:\YourFolderName\"
Set fso = CreateObject("scripting.filesystemobject")
Set FolDir = fso.GetFolder(FolderStr)
For Each FileNm In FolDir.Files
If FileNm.Name Like "AK" & "*" & ".txt" Then
StrNum = Right(Left(FileNm.Name, 5), 3)
NumStr = CInt(StrNum)
If NumStr > MaxNum Then
MaxNum = NumStr
End If
End If
Next FileNm
MaxStr = CStr(Format(MaxNum + 1, "000"))
NewName = FolderStr & "AK" & MaxStr & ".txt"
'file format????
ActiveWorkbook.SaveAs Filename:=NewName, FileFormat:=xlText, CreateBackup:=False
Set FolDir = Nothing
Set fso = Nothing
End Sub
 
Upvote 0
This seems to work. U will have to change the folder path. I'm not sure about the saveas line of code and this might require change. HTH. Dave
Code:
Private Sub test2()
'AK003.txt format
Dim fso As Object, FolDir As Object, FileNm As Object, NumStr As Integer, MaxNum As Integer
Dim NewName As String, FolderStr As String, StrNum As String, MaxStr As String
MaxNum = 1
FolderStr = "C:\YourFolderName\"
Set fso = CreateObject("scripting.filesystemobject")
Set FolDir = fso.GetFolder(FolderStr)
For Each FileNm In FolDir.Files
If FileNm.Name Like "AK" & "*" & ".txt" Then
StrNum = Right(Left(FileNm.Name, 5), 3)
NumStr = CInt(StrNum)
If NumStr > MaxNum Then
MaxNum = NumStr
End If
End If
Next FileNm
MaxStr = CStr(Format(MaxNum + 1, "000"))
NewName = FolderStr & "AK" & MaxStr & ".txt"
'file format????
ActiveWorkbook.SaveAs Filename:=NewName, FileFormat:=xlText, CreateBackup:=False
Set FolDir = Nothing
Set fso = Nothing
End Sub

Thanks for this. I am getting error
[FONT=&quot]ActiveX component can't create object

Not sure if it is cos am on Mac Excel[/FONT]
 
Upvote 0
Likely the Mac has something to do with this. The code tested and worked for me on Windows. Dave
 
Upvote 0
Thanks yes doing quick search shows Mac do not have the Scripting Runtime library, so need to come up with an alternative way of doing this that will work for both pc and Mac
 
Upvote 0
Perhaps the DIR function will work. It doesn't need the Scripting Runtime library. Dave
Code:
Private Sub test()
'AK003.txt format
Dim fso As Object, FolDir As String, FileNm As Object, NumStr As Integer, MaxNum As Integer
Dim NewName As String, StrNum As String, MaxStr As String
Dim FolderStr As String 'Object
MaxNum = 1
FolderStr = "C:\YourFolderName\"
'Set fso = CreateObject("scripting.filesystemobject")
'Set FolDir = fso.GetFolder(FolderStr)
FolDir = Dir(FolderStr)
'For Each FileNm In FolDir.Files
Do While Len(FolDir) > 0
'If FileNm.Name Like "AK" & "*" & ".txt" Then
If FolDir Like "AK" & "*" & ".txt" Then
StrNum = Right(Left(FolDir, 5), 3)
NumStr = CInt(StrNum)
If NumStr > MaxNum Then
MaxNum = NumStr
End If
End If
FolDir = Dir
'Next FileNm
Loop
MaxStr = CStr(Format(MaxNum + 1, "000"))
NewName = FolderStr & "AK" & MaxStr & ".txt"
'file format????
ActiveWorkbook.SaveAs Filename:=NewName, FileFormat:=xlText, CreateBackup:=False
'Set FolDir = Nothing
'Set fso = Nothing
End Sub
 
Last edited:
Upvote 0
Ok got it to do what wanted with this:

Sub Sample()


Dim strPath As String
Dim MyDir As String
Dim strFile As String
Dim StrNum As String
Dim NumStr As Integer
Dim MaxNum As Integer
Dim NewName As String


MaxNum = 1


MyDir = ActiveWorkbook.Path
strPath = MyDir & "/Mileage-Claims/"
strFile = Dir(strPath)


'Loop through each file in the folder
Do While Len(strFile) > 0
If Right(strFile, 4) = "xlsm" Then
StrNum = Right(Left(strFile, 5), 3)
NumStr = CInt(StrNum)
If NumStr > MaxNum Then
MaxNum = NumStr
End If
End If


strFile = Dir
Loop
MsgBox MaxNum

MaxStr = CStr(Format(MaxNum + 1, "000"))
NewName = strPath & "MIL-" & MaxStr & ".xlsm"
MsgBox NewName


End Sub
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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