+ Reply to Thread
Results 1 to 29 of 29

Code to generate registry stored number from specific number

  1. #1
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Code to generate registry stored number from specific number

    I made a maco enabled Purchase Order form about 5 years ago. Our business uses it quite extensively. When you hit a button, it generates a purchase order number from the computer's registry and saves it in a specific folder and renames it as the PO#. The PO# is sequential and we were on PO# 00990.

    The problem we ran into was our hard drive crashed and I had to install a new one. Now the PO form want's to naturally start back at #00001. Is there any way to modify the code so that it will start the next PO# at a specific number, say 00991?

    Below is the code I used but to be honest, I don't remember anything about it anymore. I haven't really been immersed in this side of excel for quite some time.
    Please Login or Register  to view this content.
    I tried changing the "00000" in red to "00991" but that does not fix the problem.
    Last edited by d_striker; 02-07-2018 at 08:12 PM. Reason: Added Code Tags

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Need Help Modifying Code

    Probably:

    Please Login or Register  to view this content.
    BTW - You need to put code tags around your code. Just highlight the code in post #1 and hit the #
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Need Help Modifying Code

    according to your code, you can enter the last invoice number in cell J5 and it should then give you the correct next number when you run the code. I don't believe you need to worry about the leading zeros in J5, the code should put those on.

    I tried changing the "00000" in red to "00991" but that does not fix the problem.
    don't do that!
    Last edited by JLGWhiz; 02-07-2018 at 07:36 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  4. #4
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Need Help Modifying Code

    I just tried what you suggested. I think there is something else going on though. Once I click the button, I get the error below. I'm not sure why. I recreated a file location and placed that location in the code.

    5zOZZP4.png

    When I run the debugger, this line of code is highlighted:
    exgKtkT.png

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

    Re: Need Help Modifying Code

    Hello d_striker,

    Administrative Note:
    • We would love to continue to help you with your query, but first, before we can proceed…
    • Please see Forum Rule #1 about proper thread titles and adjust accordingly...
    HTH
    Regards, Jeff

  6. #6
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Need Help Modifying Code

    Quote Originally Posted by xladept View Post
    Probably:

    Please Login or Register  to view this content.
    BTW - You need to put code tags around your code. Just highlight the code in post #1 and hit the #
    That didn't do it. Still assigning numbers starting at 0.

  7. #7
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Need Help Modifying Code

    Quote Originally Posted by jeffreybrown View Post
    Hello d_striker,

    Administrative Note:
    • We would love to continue to help you with your query, but first, before we can proceed…
    • Please see Forum Rule #1 about proper thread titles and adjust accordingly...
    Sorry. Edited.

  8. #8
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Need Help Modifying Code

    ALright....So I got the filepath issue worked out. But it still won't generate the number starting with 00991.

  9. #9
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Need Help Modifying Code

    Quote Originally Posted by JLGWhiz View Post
    according to your code, you can enter the last invoice number in cell J5 and it should then give you the correct next number when you run the code. I don't believe you need to worry about the leading zeros in J5, the code should put those on.



    don't do that!
    I tried this and while it will save in the location as whatever number you manually input in J5, if you don't manually input a number it will assign the next number. It's not automatically generating a number starting at 00991.

  10. #10
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Code to generate registry stored number from specific number

    Cell J5 is your control register for the PO number. The code increments the value in J5 by 1 each time you run the code. If you want it to geterate 00991 the put 990 in J5. Then don't adjust J5 again, the code will do that. Are you sure you made this macro five years ago.

    Woops! Just noticed this.

    Please Login or Register  to view this content.
    Need to see that macro to see where it saves the value to. I assumed it was J5
    Last edited by JLGWhiz; 02-07-2018 at 08:39 PM.

  11. #11
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by JLGWhiz View Post
    Cell J5 is your control register for the PO number. The code increments the value in J5 by 1 each time you run the code. If you want it to geterate 00991 the put 990 in J5.
    Perhaps I'm not doing it in the proper sequence?

    Here's what I tried:

    -Open file.
    -input "990" in cell J5
    -Click macro enabled button.

    This code will then save this macro enabled file into a regular excel workbook in the filepath specified in the code. File name becomes "990." I then close the file and reopen the macro enabled file as if I were creating the next purchase order. I click the button and it saves it as the next number from the registry. I've saved it like twenty times now so I have files saved from 00001-00020. In other words, it's not generating the number from 990.

  12. #12
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Code to generate registry stored number from specific number

    Using this line you would have to set to the next number available.
    Please Login or Register  to view this content.
    Don't have time right now but will check back later.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  13. #13
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by bakerman2 View Post
    Using this line you would have to set to the next number available.
    Please Login or Register  to view this content.
    Don't have time right now but will check back later.
    Thanks for the input! I've left work for the day but I'll try that tomorrow.
    Last edited by d_striker; 02-07-2018 at 09:06 PM.

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Code to generate registry stored number from specific number

    the first code sets the invoicenumber.
    Second one is just a test.
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by JLGWhiz View Post
    Cell J5 is your control register for the PO number. The code increments the value in J5 by 1 each time you run the code. If you want it to geterate 00991 the put 990 in J5. Then don't adjust J5 again, the code will do that. Are you sure you made this macro five years ago.

    Woops! Just noticed this.

    Please Login or Register  to view this content.
    Need to see that macro to see where it saves the value to. I assumed it was J5
    Yeah, I think 5 years ago. Had a lot more time back then and learned just enough VBA to scratch out this code. I wouldn't even really call it "learning" VBA. More like google searching and asking for help on this forum. I haven't done any VBA since then.
    Last edited by d_striker; 02-07-2018 at 09:37 PM.

  16. #16
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by bakerman2 View Post
    the first code sets the invoicenumber.
    Second one is just a test.
    Please Login or Register  to view this content.

    When I adjusted that number as xladept suggested in post #2, it didn't seem to have any impact.

  17. #17
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Code to generate registry stored number from specific number

    Check this out.
    First code sets number to 991. Use this once to set the initial number.
    Second code retrieves number, adds 1 and msgbox to check. Every time you run this code number will increase by 1.
    Please Login or Register  to view this content.
    Last edited by bakerman2; 02-07-2018 at 10:27 PM.

  18. #18
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Code to generate registry stored number from specific number

    Please Login or Register  to view this content.
    What about the SaveSetting macro? Does it exist? Can we see it?

  19. #19
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by bakerman2 View Post
    Check this out.
    First code sets number to 991. Use this once to set the initial number.
    Second code retrieves number, adds 1 and msgbox to check. Every time you run this code number will increase by 1.
    Please Login or Register  to view this content.
    Thank you for typing all of that out. I am not exactly certain where in the existing code to place what you posted. Should I simply add it or do I need to remove some of the old code and replace it with that?

  20. #20
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by JLGWhiz View Post
    Please Login or Register  to view this content.
    What about the SaveSetting macro? Does it exist? Can we see it?
    The only other code I have in it is listed as Module 1. I forgot about it actually until you mentioned other code. This is what is listed in Module1.
    Please Login or Register  to view this content.
    It's identical to the code in Sheet1. I'm not sure why I have it in there twice. I removed the code in Sheet1 as it looks like the button I'm using is actually linked to the Module1 code.
    Last edited by d_striker; 02-08-2018 at 03:37 PM.

  21. #21
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code to generate registry stored number from specific number

    This put 991 in a vacant J5:

    Please Login or Register  to view this content.
    ** J5 must be empty at the outset
    Last edited by xladept; 02-08-2018 at 03:41 PM.

  22. #22
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by JLGWhiz View Post
    Please Login or Register  to view this content.
    What about the SaveSetting macro? Does it exist? Can we see it?
    It is this

    https://msdn.microsoft.com/en-us/vba...ting-statement
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  23. #23
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by scottiex View Post
    So the last PO number is being saved to Excel Application registry. Weird! Then the SetSetting statement should reset the value to what ever beginning number the user wants to use, right? What about the other arguments for the function? Application, Key, etc?
    Last edited by JLGWhiz; 02-08-2018 at 04:24 PM.

  24. #24
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by xladept View Post
    This put 991 in a vacant J5:

    Please Login or Register  to view this content.
    ** J5 must be empty at the outset
    I deleted my code and assigned yours to the button. It won't run for some reason.

  25. #25
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code to generate registry stored number from specific number

    Did you include the SetSetting program? Which (I think) needs to be in a module??

    *** If it ran don't run it again - instead remove the code from your button code (you can just put an apostrophe before it) and clear J5 and then just run your button code!********
    Last edited by xladept; 02-08-2018 at 05:15 PM.

  26. #26
    Registered User
    Join Date
    08-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2016 (PC) & 2011 (mac)
    Posts
    33

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by xladept View Post
    Did you include the SetSetting program? Which (I think) needs to be in a module??

    *** If it ran don't run it again - instead remove the code from your button code (you can just put an apostrophe before it) and clear J5 and then just run your button code!********
    Thanks! It worked. You're a genius.

  27. #27
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Code to generate registry stored number from specific number

    You're welcome and thanks for the compliment and for the rep!

  28. #28
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,908

    Re: Code to generate registry stored number from specific number

    I am not exactly certain where in the existing code to place what you posted
    You didn't have to place anything in your existing code.
    The only thing you had to do is run sub tst once (like I said in my post #17) to set initial number (990) and then further make use of your original code.

  29. #29
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: Code to generate registry stored number from specific number

    Quote Originally Posted by bakerman2 View Post
    You didn't have to place anything in your existing code.
    The only thing you had to do is run sub tst once (like I said in my post #17) to set initial number (990) and then further make use of your original code.
    Maybe a reply to post #19 would have helped.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Generate random Code consist of number and letter using VBA code
    By mazan2010 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 06-27-2017, 11:03 AM
  2. [SOLVED] Generate random values between two number and specific string
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 12-28-2016, 04:15 PM
  3. code to generate serial number based on number of records
    By winmaxservices1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-05-2015, 03:21 AM
  4. Generate reference/number with specific criteria.
    By Dremzy in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 09-11-2014, 10:53 AM
  5. [SOLVED] Macro to generate a random number between 2 specific values into specific cells.
    By Nerfmagnet in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-10-2013, 11:45 AM
  6. Replies: 1
    Last Post: 10-28-2012, 05:42 AM
  7. [SOLVED] Need Job Number to auto generate in specific format
    By tjamestx in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-21-2012, 10:27 AM

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