+ Reply to Thread
Results 1 to 26 of 26

Macro to generate next number from last cell of another workbook

  1. #1
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Macro to generate next number from last cell of another workbook

    Hi,

    Just wondering if anyone can help me with a coding to generate the next number from the last cell of another workbook.

    It is used for a quoting spreadsheet where all quote numbers are stored on the 'Master' workbook in cell A2 and onwards.

    When a button is clicked on the quote template spreadsheet, it clears the quote template then generates the next number after checking the master spreadsheet.

    So far for my coding I have
    Please Login or Register  to view this content.
    If anyone could help I would be ever so grateful.

    Thanks
    Last edited by larzep; 02-26-2012 at 09:34 PM.

  2. #2
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Are you allowed to open the Master workbook to look at the last line (through code)?
    -Greg If this is helpful, pls click Star icon in lower left corner

  3. #3
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    I would prefer the Master workbook to remain closed and not have too many sheets opening up at the same time.

    If this is not possible to have a macro search a closed workbook and generating the next number from the last cell in a column, then anything will help.
    Last edited by larzep; 02-21-2012 at 02:30 AM.

  4. #4
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    If you have a cell in your template you can assign this formula to it which should make it pretty simple
    Please Login or Register  to view this content.
    This makes the assumption that your data is on Sheet1 in your Master workbook and that you want to find the bottom row of Column A and the data in Column A is alphanumeric. Then you can reference that cell in your code. Otherwise, you can also use ADO (or other database connection methods) to connect to the closed workbook and get the record count of the table. If ADO is the route you want to go do a search in this forum for ADO from Excel Database...I'm sure there's a lot out there already. Thanks!

  5. #5
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Anyone can assist? It has got me totally stumped
    Last edited by larzep; 02-24-2012 at 01:07 AM.

  6. #6
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    can anyone assist?

  7. #7
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Thought of another way without using ADO. Try this
    Please Login or Register  to view this content.

  8. #8
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Question: Does the Master workbook need to increment each time this happens? Example A529 is the current last row in the Master workbook and then you run this process...should the Master workbook be updated to have something in A530? If that's the case, you'd be best opening the Master, adding your data and closing it in code. Writing to a closed workbook would mean using that workbook as a database and connecting with ADO to update it...not near as simple as opening/updating/closing. If you don't want the workbook viewed while updating it, you can hide it.

  9. #9
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Hey gjlindn,

    Thank you for your response. My quote template has 3 buttons - New, Update and Save + Print, . When ever you open up the template, you will need to click 'New'. This is where i have the coding to clear all the cells listed in my macro above and then pull the next value from the last row in the master workbook i.e. ABD0529. Once the quote is completed, you will then need to click the update button where certain cell values, including the quote number, will be copied and saved into the master in the last row. Last but not least you will then be required to click save and print. This will automatically save a copy of the quote template in a designated folder, creates a PDF and then prints.

    In my update button it has the macro to open up the master, add in the values and then closing in.

    I will trial out your code, but with the line

    [Code] .Names("NextRow").Delete [code]

    could you please just elaborate what this will do?

    Sorry to be such a pain and thanks for all your efforts

  10. #10
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Hi larzep.
    Please Login or Register  to view this content.
    just deletes the named range "NextRow" created in this line earlier in the code
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Hey,

    I've just uploaded the spreadsheets in questions.

    Hope that helps.

    I tried to enter in your coding suggested but it is returning a value of '7' instead of the next sequential number.

    Please Login or Register  to view this content.
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Sorry...I thought you were looking for the next row number. I'll update the code.

  13. #13
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Change this line
    Please Login or Register  to view this content.
    to this
    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Hi Gjlindn,

    The code works like a charm! You have been amazing and super helpful.

    Can not thank you enough. Much appreciated. With this I can finally streamline my quoting process and remove the chances of doubling up on quote numbers.

    Thanks again for your time.

    Cheers.

  15. #15
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    You're welcome. If you could click the star icon in the post with the solution, I'd be very appreciative. Thanks!

  16. #16
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Hey Gjlindn,

    Sorry to bring back the dead, but got another question if you could help me out. I have tried that coding on a new set of sequential numbers and it is not returning the next value.

    Example: Q9474, Q9475, Q9476, etc, etc.

    With the coding

    Please Login or Register  to view this content.
    It only gives me a value " Q77".

    Do you know how to fix this?

  17. #17
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Sorry, the previous code I posted assumed the first 3 characters would always be text and the number would begin on the 4th character. Try this instead:

    Please Login or Register  to view this content.
    Last edited by gjlindn; 02-29-2012 at 11:01 PM.

  18. #18
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    I just tried the coding but it is returning a run-time error '1004' and when i go to debug the issue it highlighted.

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Also while i have your amazing assistance, i have a macro that creates a copy of the active worksheet into a new workbook. It then renames both the new workbook and worksheet based on the values of two cells then saves it to a certain location.

    Please Login or Register  to view this content.
    The thing is when the cell contains more then 31 characters, it gives me a run-time error 1004. Is there anyway to fix this? Alot of my data will have names that are longer then 31 letters.

  20. #20
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Hi larzep. Did you remember to change the path and workbook name and sheet name to match your workbook? The code you pasted above still has my temp path/names in it.

    Your other issue really should be a new thread since it's not related to the subject here (that way if other users run into the same issue they can find the solution created for you). I can tell you that this line will be an issue if you have more than 31 characters for a worksheet name
    Please Login or Register  to view this content.
    If you post this as a new thread I will be more than happy to help you with the solution. Thanks!

  21. #21
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Hi Gjlindn,

    Yes i have changed the parth and workbook name and sheet name to match. The coding i posted up is just a dummy one to exclude some of the details from my files. But if it helps to have the original one then ill post it below.

    Please Login or Register  to view this content.

  22. #22
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Hi Gjlindn,

    Yes i have changed the parth and workbook name and sheet name to match. The coding i posted up is just a dummy one to exclude some of the details from my files. But if it helps to have the original one then ill post it below.

    Please Login or Register  to view this content.

  23. #23
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    This line appears to be incorrect
    Please Login or Register  to view this content.
    Should be
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    Hey Gjlindn,

    Finally got a moment to continue this project. I have used the following coding but i still get errors.

    Please Login or Register  to view this content.
    The errors i get is:
    Run-time error '1004': The formula typed contains an error. Try one of the following - Make sure you've included all parentheses and required arguments.
    When I click debug it always highlight the string
    Please Login or Register  to view this content.

  25. #25
    Valued Forum Contributor gjlindn's Avatar
    Join Date
    08-01-2011
    Location
    Dodgeville, WI
    MS-Off Ver
    Excel 2003, 2007, 2010, 2013
    Posts
    369

    Re: Macro to generate next number from last cell of another workbook

    Looks like I added an extra parenthesis. Change it to this
    Please Login or Register  to view this content.
    Thanks!

  26. #26
    Registered User
    Join Date
    03-21-2011
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    58

    Re: Macro to generate next number from last cell of another workbook

    You are a legend! thank you. Works perfectly

+ 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