+ Reply to Thread
Results 1 to 15 of 15

Looping Through Userform Controls

  1. #1
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Looping Through Userform Controls

    Hello again:
    I am currently using the following macro to calculate the cost of a recipe entered using a userform (frmRecipeBox) and then storing the information in tblRecipes on Sheets("Recipe Box"). My question is this, is it possible to reduce the amount of code by using a loop? I have played around with somethings and did a lot of searching but can't find amything that works for me. Again, I am very new at all this and learning as I go. I actually need to calculate the cost of 15 ingredients making the code almost 3 times the amount however that is too much text to post here. I am afraid that so much code will invite trouble at some point.
    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping Through Userform Controls

    You could use a loop here and I was just about to post some code as I thought I saw a naming pattern.

    Unfortunately I was wrong, there is an obvious naming pattern for the variables but not for the controls, though I think there is a pattern.

    Anyway, give this a try.
    Please Login or Register  to view this content.
    PS You could make things a lot easier if you only used one set of textboxes for the ingredients and had the user add each ingredient to a 3 column listbox.

    It would also probably be a lot easier, for the user and you, if you used a combobox for the weights.
    If posting code please use code tags, see here.

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Looping Through Userform Controls

    Try something like this. Not tested.
    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Re: Looping Through Userform Controls

    I tried this code but it gave an error at
    frmRecipeBox.Controls("Cost" & I).Value = Application.VLookup(myIng, MyRange, Res, 0) * myAmt
    Couldn't find the object. There is no control named "Cost". There is a cost textbox for each ingredient (Cost1, Cost 2........Cost15). These controls do not get saved to the table. They are just used to calculate the total cost of the recipe by looking up the cost of each ingredient, adding them together and putting the result in the control named rec65. There is a naming pattern for the controls that are being added to the table row. They are named rec1 to rec 68.
    I am attaching a copy of my form and the complete macro so you can get a better idea. What if I changed the naming patterns to something like Ing1....Ing15; Amt1....Amt15, etc.
    Thanks
    Attached Files Attached Files

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping Through Userform Controls

    The code I posted would not be looking for a control named 'Cost'.

    This would be a whole lot easier using a listbox.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping Through Userform Controls

    Actually are you sure the error isn't on this line of code which has a typo?
    Please Login or Register  to view this content.
    That should be this.
    Please Login or Register  to view this content.

  7. #7
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Looping Through Userform Controls

    I'd like to help but I'm getting errors like SortByRecipe (Sub or Function not found)
    Then Run-time error '9' Subscript out of range, it's probably an inlcomplete file
    Sorry
    ---
    Hans
    "IT" Always crosses your path!
    May the (vba) code be with you... if it isn't; start debugging!
    If you like my answer, Click the * below to say thank-you

  8. #8
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Re: Looping Through Userform Controls

    You are right. Incomplete file. SortByRecipe is a macro and can be commented out.

  9. #9
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Re: Looping Through Userform Controls

    Sorry. You are right. There is a typo there. I will try the code again when I get a chance and let you know how it went. Thanks

  10. #10
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Re: Looping Through Userform Controls

    This code does not work as is, but I don't have time right now to look closer. I will let you know how I make out. Thanks

  11. #11
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Re: Looping Through Userform Controls

    Thanks Norrie. You are right, there was a type. I saw that earlier but I don't know enough about loops to actually recognize it as a typo. I tried the code again with the typo corrected and I am getting calculations but not on all ingredients all the time, which makes very little sense. Actually my original code was doing the same thing so the problem likely lies with my naming pattern or something. I don't have time at the moment to play with it but I will let you know how it goes. Thanks again.

  12. #12
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Looping Through Userform Controls

    Can you attach the corrected file?

  13. #13
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Re: Looping Through Userform Controls

    Eukeka!!!!!! I found the problem. Once I made the correction to the typo AND fixed a couple of data errors in the underlying data table, all is working. I had imported the real table data after I had my original code working and I didn't notice that there were a few entries in a column that didn't follow the proper data validation. Anyway, I am marking this thread as Solved.
    Thank you everyone.

  14. #14
    Forum Contributor
    Join Date
    04-07-2015
    Location
    Halifax, NS
    MS-Off Ver
    365
    Posts
    100

    Re: Looping Through Userform Controls

    Resolved. Thanks anyway. I used Norie's code (with typo corrected). Works well.

  15. #15
    Forum Expert Keebellah's Avatar
    Join Date
    01-12-2014
    Location
    The Netherlands
    MS-Off Ver
    Office 2021 (Windows)
    Posts
    7,937

    Re: Looping Through Userform Controls

    Congrats

+ 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] Looping through controls by frame on userform (frame within a frame)
    By njs27 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-27-2015, 10:21 AM
  2. Looping through label controls
    By anthony1312002 in forum Excel General
    Replies: 2
    Last Post: 02-03-2012, 03:20 PM
  3. looping controls in to an array
    By stoney1977 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-20-2008, 12:57 PM
  4. looping through controls
    By cmpcwil2 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-18-2006, 05:48 AM
  5. Looping through controls
    By Libby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-27-2005, 12:06 PM
  6. [SOLVED] Looping through controls on form
    By Robbyn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-15-2005, 11:06 AM
  7. Problem with looping through userform controls
    By Jeremy Gollehon in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-18-2005, 10:06 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