+ Reply to Thread
Results 1 to 3 of 3

Help with turning off auto numbering once a excel file is saved

Hybrid View

  1. #1
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help with turning off auto numbering once a excel file is saved

    This is the technique I use. It presumes you are opening the template via shortcuts so that it never really opens the template, but a new workbook, each time. The workbook_open does the same thing yours does, with the exception you forgot to include the Sheet Name reference in yours. Best include that...

    Also, I've designated cell AA1 as the "doublecheck" cell on my new workbook, and it will only increment the number if that cell is empty. The workbook_beforesave event not only increments the actual template, but it puts a flag in AA1 to make sure this workbook never does any of this activity again.

    Option Explicit
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    With Sheets("Sheet1")
        If .Range("AA1") = "" Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            Application.DisplayAlerts = False
            
            Workbooks.Open Filename:= _
                "C:\Documents and Settings\Jerry\My Documents\Excel Tips\Invoice.xlt", Editable:=True
            .Range("B2").Value = .Range("B2").Value + 1
            ActiveWorkbook.Close True
        
            .Range("AA1") = "Incremented"
            Application.ScreenUpdating = True
            Application.EnableEvents = True
        End If
    End With
    End Sub
    
    Private Sub WorkBook_Open()
    
        With Sheets("Sheet1")
            If .Range("AA1") = "" Then .Range("B2").Value = .Range("B2").Value + 1
        End With
            
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Help with turning off auto numbering once a excel file is saved

    With multiple people accessing the same template, I realized you really might need to make the "numbering" part occur compltely during the SaveAs part. So here's the function without the Workbook_Open macro at all. When you do a SaveAs, it will secretly reopen the template, increment the CURRENT number by one, remember it, save the template, put that number into your current workbook, then continue with a normal SaveAs function.

    Option Explicit
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim NewVal As Long
    
    With Sheets("Sheet1")
        If .Range("AA1") = "" Then
            Application.ScreenUpdating = False
            Application.EnableEvents = False
            Application.DisplayAlerts = False
            
            Workbooks.Open Filename:= _
                "C:\Documents and Settings\Jerry\My Documents\Excel Tips\Invoice.xlt", Editable:=True
            With ActiveWorkbook.Sheets("Sheet1")
                .Range("B2").Value = .Range("B2").Value + 1
                NewVal = .Range("B2").Value
            End With
            ActiveWorkbook.Close True
        
            .Range("B2").Value = NewVal
            .Range("AA1") = "Incremented"
            Application.ScreenUpdating = True
            Application.EnableEvents = True
        End If
    End With
    
    'normal save continues from here...
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1