Technology Get the latest on technology, electronics and software…

Excel Auto Serial #

Thread Tools
 
Old 12-14-2011, 11:03 AM
  #1  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
Excel Auto Serial #

Anyone know a simple quick way to create a automatic Serial Number generator (vba)? I.E. every time I open up Excel it creates a sequential # in cell A1?
Old 12-14-2011, 11:20 AM
  #2  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Where are you storing the number to increment? Does this xls need to be run on any machine or only one?
Old 12-14-2011, 12:39 PM
  #3  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
It is only on one pc, can would be cool if it can be on mulitple. Text file on the server...
Old 12-14-2011, 12:49 PM
  #4  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
So you need an XLT with a startup macro that will read a number from a known, constant location (whether local or network share), increment it, save it back to the file, then insert it into a specific location in your new workbook.

What if you close the workbook without saving it? Is it a requirement that the serial number not have any gaps in sequence?
Old 12-14-2011, 01:57 PM
  #5  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
Yes, yes, yes and yes.

Then nothing happens, yes in sequence please.
Old 12-14-2011, 02:43 PM
  #6  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Well, keeping the sequence intact is much more difficult since the increment and committing (saving) of the new number has to be delayed until the save. And then you can run into issues like what happens if you open two new workbooks at the same time?

Things sound "simple" when you start until you consider all the possibilities. I'll whip something "simple" up.
Old 12-14-2011, 03:33 PM
  #7  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
This is the most simple I could come up with. It will read the current serial number from a text file that you can define to be anywhere using the FILE_NAME constant, then it will increment the number and save it during the save to XLS.

You can open Excel, Hit Alt-F11, double-click ThisWorkbook, and paste this code in. Close the VBA editor and choose File>Save As and choose to save the file as an Excel Template (XLT).

If it doesn't work, remember how much you paid for it. Just kidding. Let me know if it doesn't work and I'll help make it work.

Code:
Private Const FILE_NAME = "c:\sernumber.txt"

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    If IsNumeric(ws.Cells(1, 1)) Then SaveSerial CInt(ws.Cells(1, 1))
        
End Sub

Private Sub Workbook_Open()
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    ws.Cells(1, 1) = ReadSerial
        
End Sub

Private Function ReadSerial() As Integer
    Dim fso As Object
    Dim s As Object
    Dim text As String
    Dim serNumber As Integer
    
    serNumber = 1
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(FILE_NAME) Then
        Set s = fso.OpenTextFile(FILE_NAME)
        text = s.ReadAll
        s.Close
        Set s = Nothing
    
    End If

    Set fso = Nothing
    
    If IsNumeric(text) Then serNumber = CInt(text)
    ReadSerial = serNumber
    
End Function

Private Sub SaveSerial(newSerial As Integer)
    Dim fso As Object
    Dim s As Object
    
    newSerial = newSerial + 1
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set s = fso.OpenTextFile(FILE_NAME, 2, True)
    
    s.Write (CStr(newSerial))
    s.Close
    
    Set s = Nothing
    Set fso = Nothing
    
End Sub
Old 12-14-2011, 05:40 PM
  #8  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
That rocks man^, thanks a bunch!

Humm, would it be possible to make it appear in the header and only +1 after the user hits the Print button?

I tried something different and it worked also, but it did not work in Excel 2003, I have Excel 2010, the user has 2003.
Old 12-14-2011, 07:10 PM
  #9  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
that was coded in 2007, so it's somewhere in between. When you mean header, do you mean the worksheet tab (Sheet1)?

I'm out right now but can do a little when I get home.
Old 12-15-2011, 12:17 AM
  #10  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
I mean like in Word, header? If not that's cool. But more important the print button trigger?
Old 12-15-2011, 07:09 AM
  #11  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Not sure about the header. But to move the updating of the serial # to the print event, I moved the code to the BeforePrint handler and added a flag to only increment the serial number once, so if you print multiple times, it doesn't increment each time.

Code:
Dim alreadySaved As Boolean
Private Const FILE_NAME = "c:\sernumber.txt"

Private Sub Workbook_BeforePrint(Cancel As Boolean)
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    
    If Not alreadySaved And IsNumeric(ws.Cells(1, 1)) Then
        SaveSerial CInt(ws.Cells(1, 1))
        alreadySaved = True
    End If
        
End Sub

Private Sub Workbook_Open()
    Dim ws As Worksheet
    
    Set ws = ActiveSheet
    ws.Cells(1, 1) = ReadSerial
        
End Sub

Private Function ReadSerial() As Integer
    Dim fso As Object
    Dim s As Object
    Dim text As String
    Dim serNumber As Integer
    
    serNumber = 1
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(FILE_NAME) Then
        Set s = fso.OpenTextFile(FILE_NAME)
        text = s.ReadAll
        s.Close
        Set s = Nothing
    
    End If

    Set fso = Nothing
    
    If IsNumeric(text) Then serNumber = CInt(text)
    ReadSerial = serNumber
    
End Function

Private Sub SaveSerial(newSerial As Integer)
    Dim fso As Object
    Dim s As Object
    
    newSerial = newSerial + 1
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set s = fso.OpenTextFile(FILE_NAME, 2, True)
    
    s.Write (CStr(newSerial))
    s.Close
    
    Set s = Nothing
    Set fso = Nothing
    
End Sub
Old 12-15-2011, 09:32 AM
  #12  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
I can't seem to get the last one you did to work, anything special I have to do?
Old 12-15-2011, 10:24 AM
  #13  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
Any error messages? Is the number in the text file increasing when you print?

I have made an assumption that the reason you want the number to increment on print is because you want to be able to open a new workbook, type whatever you need, print it, then close it without saving it. Is this the correct workflow? For the sequencing to work properly, you must follow the order of Create>Edit>Print>Save or Close>Create>Edit>Print>Save or Close.
Old 12-15-2011, 11:04 AM
  #14  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
I opened up a new excel workbook, copied the last set of codes you made and put it in the "view code" in the workbook and not module.

Saved as a macro enable workbook. Closed and then open and printed -> nothing.

Closed and open and put in some text then -> printed -> nothing.

Nothing as in no numbers show up on the printed page or the saved and then open sheet.
Old 12-15-2011, 12:33 PM
  #15  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
PM sent.
Old 12-15-2011, 12:44 PM
  #16  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
Sweet, that worked like a charm! Thanks Anachostic. Are you an vba coder or work with excel a lot?
Old 12-15-2011, 12:46 PM
  #17  
Pro
Thread Starter
 
sporting's Avatar
 
Join Date: Aug 2004
Posts: 518
Received 21 Likes on 17 Posts
Well don't want to jump the gun, but I'll see if it works on office 2003 too. I have 2010.
Old 12-15-2011, 02:23 PM
  #18  
Safety Car
 
Anachostic's Avatar
 
Join Date: Jul 2007
Posts: 4,845
Received 145 Likes on 90 Posts
I'm a life-long BASIC/Visual Basic/VB.NET coder. Sadly, I feel my days are numbered. C# is what everyone wants.

If the old joke "Blessed are the geeks, for they will inherit the earth" has any merit, then the Java geeks have claimed it with C# and the Unix geeks have claimed theirs with Android.
Old 12-15-2011, 06:31 PM
  #19  
Sanest Florida Man
 
#1 STUNNA's Avatar
 
Join Date: Aug 2007
Location: Florida
Posts: 43,609
Received 10,215 Likes on 6,187 Posts
^Damn you iOS!
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
08_UA7_Gr33k
Member Cars for Sale
13
02-11-2016 02:17 PM
InFaMouSLink
Car Parts for Sale
6
10-27-2015 06:52 PM
detailersdomain
Wash & Wax
3
10-09-2015 10:13 PM
malvothegreat
Car Parts for Sale
0
09-29-2015 12:38 AM
dirleton
2G RDX (2013-2018)
8
09-28-2015 04:48 PM



Quick Reply: Excel Auto Serial #



All times are GMT -5. The time now is 04:17 AM.