Filename Saving Questions

ummjay

Board Regular
Joined
Oct 1, 2010
Messages
193
Hi! I have code which saves a filename based off of a cell value + today's date. How can I adjust it so, if it already exists with value + today's date, it saves as value + date 1, value + date 2, etc? thanks!

VBA Code:
'set Range
Dim lr&: lr = ActiveSheet.Cells.Find("*", [A1], _
    xlFormulas, SearchDirection:=xlPrevious).Row
Set WBrng = Range("A1:J" & lr)
WBrng.Copy

Set Wb = Workbooks.Add(1)
    With Wb.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0

    Set Wb = ActiveWorkbook ' or a specific open workbook
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    sFile = TraderName & Format(Now(), " mmddyyyy") & ".xlsx"
    Wb.SaveAs Filename:=sPath & "\" & sFile

    End With
''end
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
replace Wb.SaveAs Filename:=sPath & "\" & sFile

with

you might need to add
Dim sfilename As String, filecount As Integer
to your dim statements


VBA Code:
    sfilename = sPath & "\" & sFile
    
    If Dir(filepathname) <> "" Then
        filecount = 1
        sfilename = Replace(sfilename, ".xlsx", "_1.xlsx")
        Do While Dir(filepathname) <> ""
          sfilename = Replace(sfilename, "_" & filecount & ".xlsx", "_" & filecount + 1 & ".xlsx")
          filecount = filecount + 1
        'Debug.Print sfilename
        Loop
    End If
        'Debug.Print sfilename
    wb.SaveAs filename:=sfilename
 
Upvote 0
Thanks, so it works the 1st time, I tried to run it a second time, to see if it would generate a new filename (with 1) after it, and got an error on this:
Wb.SaveAs Filename:=sFilename
 
Upvote 0
Replace filepathname in the dir statement with sfilename .
Sorry i forgot to update this from my test code
 
Upvote 0
still not working, same thing.

VBA Code:
Set Wb = ActiveWorkbook ' or a specific open workbook
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    sFile = TraderName & Format(Now(), " mmddyyyy") & ".xlsx"
    
    sFilename = sPath & "\" & sFile
    
    If Dir(sPath) <> "" Then
        filecount = 1
        sFilename = Replace(sFilename, ".xlsx", "_1.xlsx")
        Do While Dir(sFilename) <> ""
          sFilename = Replace(sFilename, "_" & filecount & ".xlsx", "_" & filecount + 1 & ".xlsx")
          filecount = filecount + 1
        'Debug.Print sfilename
        Loop
    End If
        'Debug.Print sfilename
    Wb.SaveAs Filename:=sFilename
    
    'Wb.SaveAs Filename:=sPath & "\" & sFile

    End With
''end
 
Upvote 0
that worked! project creep here, but i then take that file and attach it to an email via outlook vba, it was working before I added the logic to add _1, _2 etc.. Seems like it doesnt know which to attach. any ideas?

currently i have:
.Attachments.Add sFile

should it be sFilename?
 
Upvote 0

Forum statistics

Threads
1,223,714
Messages
6,174,052
Members
452,542
Latest member
Bricklin

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