Hello spanker,
The problem is the Workbook_Open event. Worksheet formula and VBA functions are not calculated (run) until after the workbook has opened. Instead you can use the Auto_Open() macro method to execute the code. I wrote this macro to save the serial number in a text file and run the macro using the Auto_Open() method. Copy all this code into a separate VBA module and change the default file path and file name to what you want to use. This macro works with Excel 2000 - 2007.
'Written: October 15, 2009
'Author: Leith Ross
'Sumamry: Creates a text file using the file path and name specfied or the defaults
' if the file doesn't exist. This file holds a serial number starting at one
' and is incremented and saved each time the function is called.
Function GetSerialNumber(Optional FilePath As String, Optional FileName As String) As String
Dim DefaultName As String
Dim DefaultPath As String
Dim FSO As Object
Dim SeqNum As Variant
Dim TxtFile As Object
DefaultPath = "C:\Documents and Settings\Admin.ADMINS\My Documents"
DefaultName = "Invoice"
FilePath = IIf(FilePath = "", DefaultPath, FilePath)
FilePath = IIf(Right(FilePath, 1) <> "\", FilePath & "\", FilePath)
FileName = IIf(FileName = "", DefaultName, FileName)
FileName = FilePath & IIf(InStr(1, FileName, ".") = 0, FileName & ".txt", FileName)
Set FSO = CreateObject("Scripting.FileSystemObject")
'Open the file for Reading and Create the file if it doesn't exists
Set TxtFile = FSO.OpenTextFile(FileName, 1, True, 0)
'Read the serial number
If Not TxtFile.AtEndOfStream Then SeqNum = TxtFile.ReadLine
TxtFile.Close
'Update the serial number
Set TxtFile = FSO.OpenTextFile(FileName, 2, False, 0)
SeqNum = Format(IIf(SeqNum = "", "1", Val(SeqNum) + 1), "0000")
TxtFile.WriteLine SeqNum
TxtFile.Close
GetSerialNumber = SeqNum
Set FSO = Nothing
Set TxtFile = Nothing
End Function
Sub Auto_Open()
Sheets(1).Range("B2") = GetSerialNumber()
End Sub
Bookmarks