+ Reply to Thread
Results 1 to 28 of 28

Too Much Code

  1. #1
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Too Much Code

    Hi i have a textbox on my userform which adds up the numerical values entered into 24 textboxs on a userform page

    The code i use ensures that it does not add up the values if any textbox is empty or not numeric

    as the textboxs on the userform do not need to be filled in a specific order i have had to add this code to every textbox, which has resulted in alot of code. Is there any other way i can go about this? As im having issues with my userform i believe due to too much code

    Please Login or Register  to view this content.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Too Much Code

    Hi flahsdisk,

    I have a golf spreadsheet where I enter hole strokes into a userform. Here is the code that uses an INDEX number of the controls on the userform to add up the first 9 holes scores.

    Please Login or Register  to view this content.
    The idea is that each control on a userform has an index number. So you do a loop through the index numbers to add stuff up. Hope this example helps.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Hi Marvin thanks for replying

    would this need to be entered in each textbox? As on my userform a user will not always finish entering values on the same textbox.

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Too Much Code

    The idea is that each control on a userform has an index number. Instead of asking for the name of each one you can use this control index number and simply run through all of them in a single loop. You should attach your userform to a workbook and post it with your thread so we can see exactly what you have. Then it might make more sense to both of us.

    To attach a file, click on "Go Advanced" and then on the paper clip icon above the advanced message area.

  5. #5
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Hi Marvin

    Ive attached the workbook

    The page of the user form I'm referring to is AQ Volume add page
    Attached Files Attached Files

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Too Much Code

    Hi flashdisk,

    You have a very involved userform. Type the following into the immediate window while looking at your userform code and see what happens.

    Please Login or Register  to view this content.
    What I'm trying to get to is using the INDEX of the control instead of the name. If you can figure out what all those controls indexes are you might loop through them and not need to know their names.

    BTW - I can't run your code as I'm on 2013 VBA and missing some of the reference libraries needed to run your code. Sorry...

  7. #7
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Hi Marvin

    I typed the code in the window but it displayed this error
    bug.jpg

  8. #8
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Any help on alternative ways to have a textbox which adds up 24 textbox values?

  9. #9
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Too Much Code

    Hi flashdisk,

    Sorry but I don't have the Calendar Control on my machine. Your code crashes each time I get to a calendar command.

  10. #10
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    please see top post, still stuck unfortunetly

  11. #11
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Too Much Code

    Hi flashdisk,

    I've asked others, who have Excel 2003 to help with this question. Your Userform is very elaborate and almost beyond the scope of most of us here. Let's see if another guru will come to our rescue.

  12. #12
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Too Much Code

    Hi flashdisk

    What version of Excel are you using. You've posted an .xlsm file but your profile shows Excel 2003.
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Too Much Code

    I haven't looked at the file yet but you can put the code into a separate routine and call that from each textbox rather than replicating the code

    you could also simplify by creating a separate calendar form instead of using multiple calendar controls with mostly the same code and validations
    Last edited by JosephP; 06-06-2013 at 03:21 AM.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  14. #14
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Thanks Marvin

    Hi Jaslake, Im using Excel 2007 sorry.

    Hi Joseph, that is an idea by calling a seperate routine, although im a novice and not to sure how to do this

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Too Much Code

    add a separate routine
    Please Login or Register  to view this content.
    and in each textbox's exit event add
    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Hi Joseph, Thank you do much for your time, code looks great

    When adding a routine do i add this to a module?, tried this but i get a compile error when clicking away from a textbox

    Compile error says that sub or fucntion not defined?

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Too Much Code

    no that has to be in the userform

  18. #18
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Too Much Code

    Hi flashdisk

    If JosephP's solution works PLEASE let us know.

    I've looked at your User Form...still trying to understand...you've a lot of "stuff" going on.

    First blush...I'm fairly confident a Class Module (one procedure to handle all the requisite TextBoxes) can handle all those TextBox Change Events (and perhaps other elements of your Code).

    I'll look at it a bit...get back to you.

    May need to rename TextBox12 and TextBox13...save me time looking...do you have a TextBox20 or a TextBox21?

    AGAIN, if JosephP's solution works PLEASE let us know...don't wish to beat a dead horse.

  19. #19
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Hi all, just got in, will test the code now and let you know.

  20. #20
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Tested the code now but i get a run time error on the line below when exiting textbox31

    Please Login or Register  to view this content.
    @Jaslake

    Looking through no i don't have any textbox's named textbox21 or textbox20

    But yes there is alot of code which is why im now looking at ways to reduce it
    Last edited by flashdisk; 06-06-2013 at 04:50 PM.

  21. #21
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Too Much Code

    what error specifically?

  22. #22
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Run-Time error '13':

    Type Mismatch

    Edit: have attached test workbook with your test code
    Attached Files Attached Files
    Last edited by flashdisk; 06-06-2013 at 05:13 PM.

  23. #23
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Too Much Code

    typo
    Please Login or Register  to view this content.
    has a superfluous comma-it oughta be
    Please Login or Register  to view this content.

  24. #24
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Yes works perfect now, that was the cause

    Will this also work if i change the textbox's subs to change rathern than exit?

  25. #25
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Too Much Code

    yes-it will run with every keystroke in those textboxes if you do

  26. #26
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Thanks Joseph, you have been amazing

  27. #27
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Too Much Code

    my pleasure :-)

    you still have a lot of code in there though!

  28. #28
    Registered User
    Join Date
    05-01-2013
    Location
    Cov, England
    MS-Off Ver
    Excel 2003
    Posts
    60

    Re: Too Much Code

    Too much code!, but this has reduced the amount of code by almost a half after duplicating your code for the second tab as well

+ 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