Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 33

Thread: auto serial number

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location

    auto serial number

    Hello , when updating a report in a workseet I want that each time I am activating the worksheet a new serial number will be automatically generate to the report.
    The first serial number will be 1000/3000 the second 1001/3000 the third 1002/3000 and sow on
    Thank you
    itzik

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi Itzik,

    New member!

    Problem: The serial no. is not a number. So the questions that follow are:

    - Is it always 4 digits "/" 4 digits?
    - Always start at 1000?
    - What happens at 9999?
    - Does only the first 4 digit number increment?
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    1000/3000 can be used as a number if custom formatting is used as in 0000"/3000".
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    It seems to me that only the first part was autonum, the second was static.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Answers that question! Need the OP to answer the others tho, I think.
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  7. #7
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location
    hi rbrhodes
    - Is it always 4 digits "/" 4 digits?
    the first 4 digit number wiil increment ‏‎until 9999 . at that point a new sequence will start as 1000/3001 > 1001/3001 >1002/3001

    thank you
    itzik

  8. #8
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi itzik,

    OK now we can look at it:


    Starts at:

    1000/3000
    1001/3000 (first 4 digits goes up by 1)
    1002/3000 etc

    9999/3000 First 4 turns into 1000 again and

    1000/3001 second 4 digits goes up by one
    1000/3002
    etc
    1000/9999

    Probably enough...
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  9. #9
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location

  10. #10
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Try it out. It's stored on Sheet (1) A1 and put to Sheet (2) A1 change that as you will...

    [VBA]
    Option Explicit
    Private Sub Worksheet_Activate()

    Dim First4 As Long
    Dim SerNum As String

    'Serial number has to be kept somewhere.
    ' Right now it's on Sheet1 in Cell A1 and this will get it

    SerNum = Sheets(1).Range("A1")

    'Get First 4 digits
    First4 = CLng(Left(SerNum, 4))

    'Check if first 4 at 9999 yet
    If First4 < 9999 Then
    'Add 1 to First 4
    SerNum = First4 + 1 & Right(SerNum, 5)
    'Store it
    Sheets(1).Range("A1") = SerNum
    'Put it
    Sheets(2).Range("A1") = SerNum
    Else
    'Add 1 to Second 4
    SerNum = "1000/" & CLng(Right(SerNum, 4) + 1)
    'Store it
    Sheets(1).Range("A1") = SerNum
    'Put it
    Sheets(2).Range("A1") = SerNum
    End If

    End Sub
    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  11. #11
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location
    Thank you all
    That what I did
    1. According to mdmackillop i changed the custom formatting to 0000"/3000"[/font]


    2. the line in Rbrhodes code SerNum = First4 + 1 & Right(SerNum, 5)

    Changed to SerNum = First4 + 1 & Right(SerNum, 0)

    It work perfect

    ‏‎again thank you all

    itzik

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi itzik,
    Using custom formatting is not required by DR's code, which deals with the text of numbers, rather than their value.

    If you were to custom format Column A as described, you can enter numbers as normal, ie, enter 1000, 1001 etc. and add your increment as in =A1+1. It will not though, handle the 9999 to 1000 change
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location
    yes but i need the number to display‎‏ as 1000/3000

  14. #14
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Which it does.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  15. #15
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location
    that what i did

  16. #16
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If you have a problem with the solution, please post your workbook.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  17. #17
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location

    ‏‎improving the code

    Hi
    The workbook attached
    The code that Rbrhodes suggested and that I modified a bet work ok.
    But it work only on sheets that are specified in the code, sheet 1 to 3.
    I want it to work automatically on each sheet that I will add in the future.
    Can you help me whit it?
    Itzik

  18. #18
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I want that each time I am activating the worksheet a new serial number will be automatically generate to the report.

    This implies that every time you open a sheet, it will add an incremental number.
    Your example implies each sheet has an incremental number in A1.
    We can do either or both, but we need a much clearer idea of what you are after.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  19. #19
    VBAX Regular
    Joined
    Aug 2009
    Posts
    15
    Location
    Quote Originally Posted by mdmackillop
    This implies that every time you open a sheet, it will add an incremental number.
    this is what Iam after.
    itzik

  20. #20
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    .....we need a much clearer idea of what you are after.

    Last request for proper information.

    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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