+ Reply to Thread
Results 1 to 10 of 10

Using a text file to hold sequential numbers

Hybrid View

  1. #1
    Registered User
    Join Date
    04-25-2009
    Location
    Manchester,England
    MS-Off Ver
    Excel 2003
    Posts
    17

    Using a text file to hold sequential numbers

    Hi All
    i found a macro that im trying to get working from the site below to generate a number everytime my template is opened.

    ive put the code in the thisworkbook module as directed and changed the relevant path but it doesnt do anything i think im missing something but cant see what.
    any help would be appreciated
    this is the code ive placed in thisworkbook module of my template


    Public Function NextSeqNumber(Optional sFileName As String, Optional nSeqNumber As Long = -1) As Long
            Const sDEFAULT_PATH As String = "<your path here>"
            Const sDEFAULT_FNAME As String = "defaultseq.txt"
            Dim nFileNumber As Long
            
            nFileNumber = FreeFile
            If sFileName = "" Then sFileName = sDEFAULT_FNAME
            If InStr(sFileName, Application.PathSeparator) = 0 Then _
                sFileName = sDEFAULT_PATH & Application.PathSeparator & sFileName
            If nSeqNumber = -1& Then
                If Dir(sFileName) <> "" Then
                    Open sFileName For Input As nFileNumber
                    Input #nFileNumber, nSeqNumber
                    nSeqNumber = nSeqNumber + 1&
                    Close nFileNumber
                Else
                    nSeqNumber = 1&
                End If
            End If
            On Error GoTo PathError
            Open sFileName For Output As nFileNumber
            On Error GoTo 0
            Print #nFileNumber, nSeqNumber
            Close nFileNumber
            NextSeqNumber = nSeqNumber
            Exit Function
        PathError:
            NextSeqNumber = -1&
        End Function
    ive also placed the code below in in a macro

    Public Sub Workbook_Open()
            ThisWorkbook.Sheets(1).Range("B2").Value = NextSeqNumber
        End Sub
    the original code came from

    http://www.mcgimpsey.com/excel/udfs/sequentialnums.html

    thanks

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using a text file to hold sequential numbers

    Hello spanker,

    Is an error being generated? What was the cell value before the workbook was opened and afterwards?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    04-25-2009
    Location
    Manchester,England
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Using a text file to hold sequential numbers

    Hi Leith

    no theres no error generated
    the cell value was 000000019 which was entered manually ive also tried it with the cell value blank

    have i placed the code in the corredct places?

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Using a text file to hold sequential numbers

    Bit overcomplicated,are you using a *.xlt workbook?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  5. #5
    Registered User
    Join Date
    04-25-2009
    Location
    Manchester,England
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Using a text file to hold sequential numbers

    Hi Royuk
    Bit overcomplicated,are you using a *.xlt workbook?
    yes its an axcel template file .xlt

    to be honest im pretty new to this so i dont know how it does it (or doesnt in this case)

    is there an easier way?

    thanks

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Using a text file to hold sequential numbers

    You could store it in the Registry.

    If it wasn't a *.xlt I would store it in a Named Range.

    The code that yo posted works fine, have you put the code in the correct place & made the required changes?

    Attach your workbook so that we can check

  7. #7
    Registered User
    Join Date
    04-25-2009
    Location
    Manchester,England
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Using a text file to hold sequential numbers

    i did get the registry version working but, i need to put the workbook on another machine thats why i wanted to use the text file version,

    the template is going to be on a shared drive so every time its opened it creates a new file and saves it.

  8. #8
    Registered User
    Join Date
    04-25-2009
    Location
    Manchester,England
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Using a text file to hold sequential numbers

    ive attached the file
    thanks
    Attached Files Attached Files

  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Using a text file to hold sequential numbers

    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

  10. #10
    Registered User
    Join Date
    04-25-2009
    Location
    Manchester,England
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Using a text file to hold sequential numbers

    Thanks for all the help guys
    leith the above code did the trick

    Excellent!

+ 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