soteman2005
New Member
- Joined
- Nov 24, 2005
- Messages
- 32
Hi,
I have this code to copy several worksheets and then save them in a new workbook in the same directory. This works but I have also tried to add a unique number on the end of the filename, so the script looks to see if the filename exists and if it does, it adds one. But at the moment, it tries to save with _01 everytime..
any idea???
thanks
I have this code to copy several worksheets and then save them in a new workbook in the same directory. This works but I have also tried to add a unique number on the end of the filename, so the script looks to see if the filename exists and if it does, it adds one. But at the moment, it tries to save with _01 everytime..
any idea???
thanks
Code:
Dim fso As FileSystemObject
Set fso = New FileSystemObject
Dim strPath As String
Dim filename As String
Dim x As Long
Dim dir As String
Application.ScreenUpdating = False
filename = "\Test" & "_" & Day(Date) & "_" & Month(Date) & "_" & Year(Date) & "_01"
dir = ActiveWorkbook.Path
Sheets(Array("A", "B", "C")).Copy
If fso.FileExists(filename) = True Then
For x = 2 To 50
filename = Left(filename, Len(filename) - 1) & Right("00" & x, 2)
If fso.FileExists(filename) = False Then
ActiveWorkbook.SaveAs filename:=dir & filename
Exit For
End If
Next
Else
ActiveWorkbook.SaveAs filename:=dir & filename
Application.ScreenUpdating = True
End If