+ Reply to Thread
Results 1 to 6 of 6

Question on autonumber

  1. #1
    Registered User
    Join Date
    08-22-2005
    Posts
    9

    Question on autonumber

    I’m sure this have been asked a thousand times, but I just need a starting hand. I have built a quote sheet and now I want to have the quote number automatically update with the next available number. I am thinking I can put a TXT file in the same dir and then call it from excel but I don’t know where to write. I want to call it Onopen.

    I am thinking it should look like this. ( I’m a novice with code.)

    Breneck Quote Sheet 2.Main.Range (“C17”).Value = NextSeqNumber (“Quote.txt”)

    Am I on the right track?

  2. #2
    Gareth
    Guest

    Re: Question on autonumber

    There's a number of ways - it would probably depend on how you operate
    and the number of users using the workbook. You could store it in the
    workbook properties or even in the cell itself and just increment it.

    However, I guess you have multiple users and therefore people might be
    opening it as read only therefore you want to store it elsewhere. In
    that case, to write/read from a file so something like the below.

    Note that this obviously doesn't prevent quote numbers from not being
    used when someone just opens and closes a workbook for no reason.

    HTH,
    Gareth

    In your ThisWorkbook code module place:

    'assuming your worksheet is named 'Breneck Quote'
    Private Sub Workbook_Open
    Thisworkbook.sheets("Breneck Quote").Range ("C17").Value _
    = fcnGetNextAvailableQuoteNumber
    End Sub

    Private Function fcnGetNextAvailableQuoteNumber() as string
    Const myFileName as string = "quotenumber.txt"
    Dim F as long
    Dim MyNo as Variant

    'get the quote number
    If Dir(ThisWorkbook.Path & "\" & myFileName) <> "" Then
    F = FreeFile
    Open Thisworkbook.path & "\" & myFileName _
    for input as #F
    Input #F, MyNo
    Close #F
    End if

    'Set to 1 if it was empty (or something else was in the file)
    If not isnumeric(myno) then MyNo = 1 else MyNo = MyNo +1

    'return the number (I assume you want it formatted with
    'zeros or something
    fcnGetNextAvailableQuoteNumber = Format(MyNo,"00000000")

    'update the file
    F = FreeFile
    Open Thisworkbook.path & "\" & myFileName _
    for output as #F
    Print #F, MyNo
    Close #F

    End function


    Xenos wrote:
    > I’m sure this have been asked a thousand times, but I just need a
    > starting hand. I have built a quote sheet and now I want to have the
    > quote number automatically update with the next available number. I am
    > thinking I can put a TXT file in the same dir and then call it from
    > excel but I don’t know where to write. I want to call it Onopen.
    >
    > I am thinking it should look like this. ( I’m a novice with code.)
    >
    > Breneck Quote Sheet 2.Main.Range (“C17”).Value = NextSeqNumber
    > (“Quote.txt”)
    >
    > Am I on the right track?
    >
    >


  3. #3
    Registered User
    Join Date
    08-22-2005
    Posts
    9
    Gareth, Thank you… it rocks.

  4. #4
    Gareth
    Guest

    Re: Question on autonumber

    You're welcome mate.

    Xenos wrote:
    > Gareth, Thank you… it rocks.
    >
    >



  5. #5
    Registered User
    Join Date
    08-22-2005
    Posts
    9
    Gearth, how do I direct the txt file to c:\Quote ?

  6. #6
    Gareth
    Guest

    Re: Question on autonumber

    Hi,

    Replace the function fcnGetNextAvailableQuoteNumber I gave you with the
    below one.

    FYI - I just changed
    Const myFileName as string = "quotenumber.txt"
    to
    Const myFileName as string = "c:\quote\quotenumber.txt"

    and instead of using ThisWorkbook.Path & "\" & myFileName I use just
    myFilename.

    G

    Private Function fcnGetNextAvailableQuoteNumber() as string
    Const myFileName as string = "c:\quote\quotenumber.txt"
    Dim F as long
    Dim MyNo as Variant

    'get the quote number
    If Dir(myFileName) <> "" Then
    F = FreeFile
    Open myFileName for input as #F
    Input #F, MyNo
    Close #F
    End if

    'Set to 1 if it was empty (or something else was in the file)
    If not isnumeric(myno) then MyNo = 1 else MyNo = MyNo +1

    'return the number (I assume you want it formatted with
    'zeros or something
    fcnGetNextAvailableQuoteNumber = Format(MyNo,"00000000")

    'update the file
    F = FreeFile
    Open myFileName for output as #F
    Print #F, MyNo
    Close #F

    End function

    Xenos wrote:
    > Gearth, how do I direct the txt file to c:\Quote ?
    >
    >


+ 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