+ Reply to Thread
Results 1 to 13 of 13

Shortening of VERY LONG UserForm VBA Code

  1. #1
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Shortening of VERY LONG UserForm VBA Code

    HiUserForm.png

    I have a UserForm (pic attached) that allows the user to input sales for up to 5 years for each month.
    They can do this by entering a 'master' percentage at the top which calculates the following years sales based on the previous or they can enter everything manually.
    Also, entering a 'master' percentage doesn't stop the user from changing a percentage on one particular month, so it's pretty much 100% able to do whatever the user wants.
    It also has totals.

    The code works perfect BUT it needs to be applied to 10's of other UserForms which form part of a big workbook.

    So, can any of this code be shortened because at the moment when I create a new UserForm I have to change every bit of code ever so slightly to make sure it works with the new TextBox names?

    Submits the data onto the worksheet:

    Please Login or Register  to view this content.
    The second submit button just opens the next UserForm.

    Makes all the individual percentage boxes equal the master above it:

    Please Login or Register  to view this content.
    Upon exit of entering an individual percentage, the following years sales will re-calculate (very long as it works for 60 months):

    Please Login or Register  to view this content.
    Last edited by PDBartlett; 04-11-2014 at 06:31 AM.

  2. #2
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    I couldn't fit the rest of the code on:

    Function to allow the total TextBoxes to work upon ANY change in the data input:

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    Year 1-3:

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    Year 4-5:

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    If even some of this code could be shortened it would be a huge help!

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Shortening of VERY LONG UserForm VBA Code

    Can you attach your workbook please?

  7. #7
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    Hi Kyle

    Business Modelling Template Upload.xlsm

    I've cut a load of stuff out so the workbook is just showing the sales element.
    It has 5 types of sales, I have userforms for 3 so far which all work fine.

    Look on the P&L Input sheet to click on the buttons to bring up the UserForm itself.

    So it's just, can the code be shortened?
    Can you rename textboxes faster? (other thread)

    Thanks!

  8. #8
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Shortening of VERY LONG UserForm VBA Code

    Ok this took a little more grey matter that anticipated

    This is a complete replacement for your code (other than writing it back to the worksheet), there's some complex(ish) stuff going on here to reduce the amount of code required and I'm still not particularly happy with it, try to work through to see how much you can follow and post back if there are any specific issues and I'll happily explain.

    As an aside, you don't need to keep re-creating the same form, you can simply show it multiple times.

    Class module: MonthNode
    Please Login or Register  to view this content.
    Class module: yearChange
    Please Login or Register  to view this content.
    Userform code:
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by Kyle123; 04-11-2014 at 09:58 AM.

  9. #9
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    To be perfectly honest I don't know what is going on with your code haha!

    Shall I replace all my code with all of the above and what happens?

  10. #10
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Shortening of VERY LONG UserForm VBA Code

    haha does it do what you want?

  11. #11
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    Something wrong with this?
    User defined type not defined?

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    04-04-2014
    Location
    Tetbury, England
    MS-Off Ver
    Excel 2010
    Posts
    254

    Re: Shortening of VERY LONG UserForm VBA Code

    Ahh ok I think I have it working on your spreadsheet, but how can I incorporate it into mine?

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Shortening of VERY LONG UserForm VBA Code

    I've rejigged the code to make it more efficient:
    YearChange class:
    Please Login or Register  to view this content.
    Userform:
    Please Login or Register  to view this content.
    The way this code works, so you get an idea is that it creates 12 lists of Months held in boxList. Each list contains 5 months, one for each year, since we tell each month what the next month in the chain is, each month node is responsible for updating the node next down the chain when the percentage changes. As a diagram, it looks like this \1

    This type of structure would normally be referred to as a singly linked list (singly as each node only knows the next node and not the previous one); however we have made the structure more efficient by indexing each node in the boxList array allowing direct access to any node - I think it's clearer in the diagram.
    Attached Images Attached Images
    Last edited by Kyle123; 04-14-2014 at 07:00 AM.

+ 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. Shortening of long array formula by short formula
    By paradise2sr in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-29-2013, 05:51 AM
  2. [SOLVED] Macro line too long, needs shortening?
    By Hyflex in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-09-2011, 01:46 PM
  3. Shortening long IF statement?
    By Lukus in forum Excel General
    Replies: 1
    Last Post: 12-21-2009, 05:44 PM
  4. Shortening Code
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2007, 11:46 AM
  5. Shortening Code
    By T De Villiers in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2007, 11:17 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