+ Reply to Thread
Results 1 to 25 of 25

Macro to increase serial number by one after each print cyle

  1. #1
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Macro to increase serial number by one after each print cyle

    Hello all,

    I'm working on an Excel spreadsheet where I need a serial number to increase by one each time I print the document. For example in cell B6 I have serial number RAIL-0001. I would like to set it up so that the next time I print the number will change to RAIL-0002. Is this possible? Any help would be greatly appreciated. Thanks for having me!
    Last edited by MATTN; 10-23-2012 at 10:39 AM.

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Macro to increase serial number by one after each print cyle

    Without seeing your current code, for sequential numbering I normally use a method like below.

    I place on a separate helper sheet (in this case Sheet3) Column A numbers beginning with 1 to whatever.

    The following code will increase the value everytime you run the code. Perhaps you could use this idea.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-09-2009
    Location
    london, england
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Macro to increase serial number by one after each print cyle

    I gave it a go and came up with the following code below. It was tried and tested in Excel 2010.
    Wonder if it meets your requirements.
    Note that you can merge some lines of code if you want to make it more compact and remove the debug.print where applicable


    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Thanks John and Ducky, I'm lost on where to paste the code. Does it go in the "fx" line when I highlight the cell? I've seen other post that say the code has to go in the visual basic editor under the developer tab. Please bare with me. Codes and Macros have never been my strong point in Excel. Thanks again for your help!

  5. #5
    Registered User
    Join Date
    11-09-2009
    Location
    london, england
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Macro to increase serial number by one after each print cyle

    Q. Where to paste the code?
    A. Yes, Paste in Visual Basic Editor.
    1. On ribbon click Developer
    2. On toolbar click Visual Basic
    3. From Project View (left hand side) click ThisWorkBook
    4. In code window (right hand side) paste the text

    Just noticed that both John and I have only addressed the increment of the serial number and not automated the process of printing.
    Are you looking for code to print out the whole workbook / particular worksheet and then increment the serial number?

  6. #6
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Thanks. If the serial number increased by one every time I print the workbook that would be perfect versus having to manually change the number everytime I print. I wasn't sure if it is possible to make this work using a serial number with a combination of letters, a dash, and then the number. Only the number needs to change. ie "RAIL-0001" when printed would change to "RAIL-0002". I hope this makes sense.

    Matt

  7. #7
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    Untested (with a print), but try...

    Go to the VBE >> Alt + F11

    Please Login or Register  to view this content.
    This code goes in the ThisWorkbook module
    HTH
    Regards, Jeff

  8. #8
    Registered User
    Join Date
    11-09-2009
    Location
    london, england
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Macro to increase serial number by one after each print cyle

    Based on your clarification and suggested code by jeffreybrown. I suggest that you merge the code as follows:

    Please Login or Register  to view this content.
    I have kept function to update the serial number out of Workbook_BeforePrint procedure but you are welcome to merge all the code together.

    The reason that I suggest using the IncrementSerialNumber() function is because:
    - it can increment any serial number provided it is in the following format: sometext-####
    - if the number is RAIL-0009 then it will be incremented to RAIL-0010 opposed to RAIL-00010

    Do try both our suggestions to see which one fulfils your requirement.
    Note: due to time constraint, I haven't been able to test the code changes.
    Hope that helps.

  9. #9
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Run-time error '9': subscript out of range is what I see when attempting to print. Here what I am doing and maybe you can see what I'm doing wrong. I open the workbook click the cursor on the cell that I want the serial number in (blank), I then go to the VBE and click on this workbook and past the entire code that you provided. From there I'm not sure what to do. Do I have to save it as something and go back to the workbook and load it using the "Macros" button? Thanks for sticking with me on this.

  10. #10
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    Hi Matt,

    Good idea to say who you are responding too, myself or ducky!

  11. #11
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    My fault I was responding to both. Same result for both macros which leads me to believe that I am doing something wrong on my end. Thanks for your patience.

  12. #12
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    Hi Matt,

    It works for me, but maybe you can explain what you are doing?

    Here is what I did.

    Took the code I posted in post #7 >> Opened up the VBE >> ThisWorkbook module >> Paste macro on right side of screen

    In cell B6 >> RAIL-0001

    As soon as I print that worksheet >> B6 changes to RAIL-0002

  13. #13
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Thanks Jeffrey,

    Here is what I did.

    Copied the code from #7>>opened the VBE>>this workbood module>>pasted the macro on the right side of the screen>>RAIL-0001 in cell B6>>when I hit print It gave me the runtime error'9': subscript out of range. I ended it and the workbook printed but with cell B6 empty. I'll open a new workbook and try the same to see if I have any luck. Thanks again.

  14. #14
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Jeffrey,

    I tried your code on a blank workbook and it worked perfectly so maybe the problem is in my workbook itself. Hmmmmm Any suggestions?

    Thanks,

    Matt

  15. #15
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    The only suggestion I have is to make sure no other code is interfering, and if so, post your workbook here.

  16. #16
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Hi Jefferey sorry for the delay. I don't see any other code so I'm posting the workbook for you to look at. As always thaks for your help!
    Attached Files Attached Files

  17. #17
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    Hi Matt,

    Well first off, there is no code in this workbook????

    Place this code in the ThisWorkbook module
    Please Login or Register  to view this content.
    I see on sheet blank (3), in cell B6 you have RAIL-0001.

    I placed the code above in the appropriate place within the workbook and instead of printing, just to test, on the keyboard I selected ctrl + p which opens up the print dialogue box. At that time you should see B6 change from RAIL-0001 to RAIL-0002.
    Last edited by jeffreybrown; 10-31-2012 at 11:15 AM.

  18. #18
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Hi,

    I realize there was no code in that workbook. I sent it to you without for you to try. Here it is again with the code allthough most likely done incorrectly on my end seeing as though it still doesn't seem to work. Thanks for your patience.
    Attached Files Attached Files

  19. #19
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    Hi Matt,

    Again, this is ThisWorkbook module code, not general code. You have the code in a general module.

    Open up the VBE, find ThisWorkbook on the left side of the screen, double click to open and paste the code on the right side of the screen.

    http://www.rondebruin.nl/code.htm

  20. #20
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    It works! Sorry it took me so long to get it. Can't thank you enough for the support!

  21. #21
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    Quite alright, yes glad you now have a working project.

    Your welcome and thanks for the feedback.

    =======================================================================

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  22. #22
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Okay marked as Solved. If you don't mind one more quick question and I will leave you alone. How do I modify the code so that if I printed lets say 10 or 20 copies that they would still increase by one?

  23. #23
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Macro to increase serial number by one after each print cyle

    Wouldn't you just select 10 or 20 copies and that equals one print?

    The next time you hit print the code would fire again.

  24. #24
    Registered User
    Join Date
    10-11-2012
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Macro to increase serial number by one after each print cyle

    Yes, however I need each page to have its own serial number. So if I were to select 10 copies all ten would be marked the same vs RAIL-0001, RAIL-0002, RAIL-0003 etc. For example the numbers could eventually be as high as RAIL-8000 someday.

  25. #25
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Macro to increase serial number by one after each print cyle

    Use a separate macro for that.

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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