Consulting

Results 1 to 2 of 2

Thread: Workbook.SaveAs function, appends number to similar named files

  1. #1

    Workbook.SaveAs function, appends number to similar named files

    Hi, how do you modify the Workbook.SaveAs function to append a number based on whether there are similarly named files in the same folder?

    E.g. Set the filename as Output_n.xlsx

    If it is the first file that has the name, then save the file as Output_1.xlsx

    If it's the second or third file that has the name, save the file as Output_2.xlsx or Output_3.xlsx

    If it's the 12th file that has the name, save the file as Output_12.xlsx

    and so on
    Last edited by Cheesecube; 05-16-2020 at 01:19 AM.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Maybe something like this


    Option Explicit
    
    
    
    
    'SaveAsUI   Required    Boolean True if the Save As dialog box is displayed due to changes made that need to be saved in the workbook.
    'Cancel     Required    Boolean False when the event occurs. If the event procedure sets this argument to True, the workbook isn't saved when the procedure is finished.
    
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Dim P As String, N As String, F As String, E As String
        Dim c As Long
        Dim i As Long
        
        SaveAsUI = True
        Cancel = False
    
    
        i = InStrRev(ThisWorkbook.Name, ".")
        N = Left(ThisWorkbook.Name, i - 1)
        E = Right(ThisWorkbook.Name, Len(ThisWorkbook.Name) - i)
            
        If Right(N, 2) Like "##" Then
            c = Right(N, 2)
            N = Left(N, Len(N) - 2)
        Else
            c = 0
        End If
    
    
        F = Dir(ThisWorkbook.Path & Application.PathSeparator & N & Format(c, "00") & "." & E)
    
    
        Do While Len(F) <> 0
            c = c + 1
            F = Dir(ThisWorkbook.Path & Application.PathSeparator & N & Format(c, "00") & "." & E)
        Loop
    
    
        Application.EnableEvents = False
        ThisWorkbook.SaveAs ThisWorkbook.Path & Application.PathSeparator & N & Format(c, "00") & "." & E
        Application.EnableEvents = True
        
        ThisWorkbook.Close
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •