Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 53

Thread: Excel AutoNumber Files

  1. #1
    Site Admin
    The Princess
    VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location

    Excel AutoNumber Files

    I want to create an Excel template that has a cell that contains a number that increases by one every time I OPEN the file.

    Let's place the number in Sheet1!A1 and have it be formatted as 000000.

    Can you provide the code?
    I will add it to the knowledgebase under your name with a sample file.
    ~Anne Troy

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    You could put this into ThisWorkbook:

    Private Sub Workbook_Open()
    With Sheets("Sheet1")
        If IsEmpty(.Range("A1")) Then
            .Range("A1").Value = 0
        Else:
            .Range("A1").Value = .Range("A1").Value + 1
        End If
        End With
    End Sub
    An option. Many ways to skin this cat

  3. #3
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location
    guess I'm going too lean here but...

    From the first time the workbook is opened (after installing the code):
    Just Alt+F11 this into Workbook Module:

    Private Sub Workbook_Open()
    'Selecting the sheet where we want the count to take place
    Sheets("Sheet1").Select
    'Then adding by 1 each time we open the workbook
    'just change the counting cell here if it changes from Cell A1
    [A1].Value = [A1].Value + 1
    End Sub

    So if the cell is empty, a 1 will be there on open after code install.
    Of course, Changes must be saved on exit or the cell value will not increment as expected.

    Too lean? I'm curious.
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by Scott
    Too lean? I'm curious.
    Nope! Boy, I overcomplicated that one, eh?! Works great. Only thing barring is that the cell is pre-formatted in the way desired. "000000" will give you that many zeros always showing.

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Well...hey, you know me, I'm no coder. But it SEEMS that if I use this code, I'm always gonna get a ONE in that cell...I am using a TEMPLATE after all...
    ~Anne Troy

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Oops....so, presumably, I am saving this to a new file name each time.
    ~Anne Troy

  7. #7
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location
    hmmm...keyword "template"...
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  8. #8
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Let's do this...

    Let's have a TEXT file called c:\numbers.txt where we store the last *invoice* number.
    ~Anne Troy

  9. #9
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location
    My reading skills are superior~! 8)
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Okay, so you want to update this text file AND the cell value by 1 everytime the book is opened?

  11. #11
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Well...where else ya gonna keep the last value?
    ~Anne Troy

  12. #12
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    http://www.word.mvps.org/faqs/macrosvba/NumberDocs.htm

    I can't get this sucker to work, but I know I have before.
    ~Anne Troy

  13. #13
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location
    Ok, this is what I came up with so far...


    This will depend on a .txt in the same directory as the xl file with the name "lastvalue.txt".

    This code goes in the Workbook module.

    Private Sub Workbook_Open()
    Dim nmbr As Long
    Dim fNum As Integer
    fNum = FreeFile
    Open ThisWorkbook.Path & "\" & "lastvalue.txt" For Random As #fNum Len = Len(nmbr)
    Get #fNum, 1, nmbr
    nmbr = nmbr + 1
    Put #fNum, 1, nmbr
    Close #fNum
    ' for Cell A1
    Sheets("Sheet1").Range("A1").Value = nmbr
    End Sub


    Remember to keep the XL file and the text file in the same directory when trying this out.

    I am still tinkering with this so maybe it is a jump point for someone else.

    X
    Last edited by Aussiebear; 04-30-2023 at 01:30 PM.
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  14. #14
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    As I told you already Scott, works great for me!

    After saving it as a template file, to clear it and start again, I opened the text file, cleared the contents, saved and closed it. Then cleared the cell in the workbook template, saved and closed it out.

    Works like a charm! Great stuff Scott!!!

  15. #15
    Just A Dude VBAX Tutor Scottie P's Avatar
    Joined
    May 2004
    Location
    Remote from 18901 USA
    Posts
    263
    Location
    Cool and Thanks! Really glad it works out.
    Do you think that this arrangement would work in a shared environment if the read/write permissions are set on the txt file??
    Life is Visual: Presence is Perception...
    How we see the world is how we respond to it. ~* Peace *~

  16. #16
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I'm not sure how well it'd work there, but I'd have to test it out. With limited testing (.txt file 'as-is') and multiple copies of the .xlt, it'll write to the .txt file anytime the book(s) is opened. That also means, even if you exit out of it and don't save it, you're still going to increment it by one. I guess this is a good reason why I'm a big fan of keeping everything in one place.

    But, I haven't tested it too thoroughly yet.

  17. #17
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Let me rephrase that..

    The only real drawback I see, is if somebody closes out the workbook without saving it. In regards to a sequential number, it will advance regardless of the save. So multiple use's could very well be beneficial, with that one word of caution being said.

  18. #18
    Knowledge Base Approver VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    or using the Registry to handle multiple filenames


    Private Sub Workbook_Open()
    Dim Quest      As Integer
        Sheets(1).Range("a1").Value = GetSetting("Counter", "Value", ActiveWorkbook.Name, 1)
        SaveSetting "Counter", "Value", ActiveWorkbook.Name, Sheets(1).Range("a1").Value + 1
        Quest = MsgBox("Reset Key?", vbYesNo + vbCritical)
        If Quest = vbYes Then DeleteSetting "Counter", "Value", ActiveWorkbook.Name
    End Sub
    Cheers

    Dave

  19. #19
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Yeah, but using the registry doesn't allow for >1 user.
    ~Anne Troy

  20. #20
    Knowledge Base Approver VBAX Expert brettdj's Avatar
    Joined
    May 2004
    Location
    Melbourne
    Posts
    649
    Location
    Hehe

    Private Sub Workbook_Open()
        Dim Quest      As Integer
        Dim CurUser As String
        CurUser = Environ("username")
        Sheets(1).Range("a1").Value = GetSetting("Counter", "Value", CurUser & " " & ActiveWorkbook.Name, 1)
        SaveSetting "Counter", "Value", CurUser & " " & ActiveWorkbook.Name, Sheets(1).Range("a1").Value + 1
        Quest = MsgBox("Reset Key?", vbYesNo + vbCritical)
        If Quest = vbYes Then DeleteSetting "Counter", "Value", CurUser & " " & ActiveWorkbook.Name
    End Sub
    Cheers

    Dave

Posting Permissions

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