+ Reply to Thread
Results 1 to 24 of 24

Combining code using variables

  1. #1
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Lightbulb Combining code using variables

    Hi,

    I am trying to make my very long code more efficient as it becoming a challenge to maintain. The basic portions of the code are as follows:

    A userform pops up to ask the user for sets of words seperated by commas. Here is the relevant userform code:
    Please Login or Register  to view this content.
    In a regular module, I have code that will split the words from each array:
    Please Login or Register  to view this content.
    and then look for each word in a range and modify these words if found:

    Please Login or Register  to view this content.
    The actual userform and code currently contains seven sets of commands (one corresponding to each textbox on the userform. Therefore each time I make one modification, to the code, I have to make sure I modify it seven times and then test it seven times to make sure I coded it correctly each time. The number of textboxes on the userform is going to increase and I need help finding a better way to manage all this code.

    I tried using:

    Please Login or Register  to view this content.
    and same thing for the variant (a) in "myList_1(a)" but I am having no luck coding it correctly.

    If anyone has experience with these issues, could you please guide me so I can modify my code accordingly?

    Thanks.

    abousetta
    Last edited by abousetta; 06-06-2011 at 04:41 AM.

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

    Re: Combining code using variables

    Hi abousetta,

    It sounds like you need to loop through the controls on your userform. You can do this with an index number.
    I just created a userform and hung 4 textboxes on it. Then in the immediate window I typed
    Please Login or Register  to view this content.
    and pressed Enter. It came back as "TextBox2"

    This means you can loop through your TextBoxes on the Userform with
    For i = 0 to 6
    StrText = Userform1.Controls(i).Text
    'Do what you need here
    Next i

    Hope this helps. Excel has indexes built into most all its objects.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Hi @MarvinP,

    Thank you, but I seem to not understand how to compile this correctly.

    Please Login or Register  to view this content.
    gives me a run-time error.

    I tried changing .Controls to .TextBox and .Text to .Value and combinations of them, but all gave me errors. Do you see where I am going wrong?

    abousetta

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

    Re: Combining code using variables

    Hey,
    How many controls do you have on your userform?
    Start using the immediate window and debug.print
    Please Login or Register  to view this content.
    then
    Please Login or Register  to view this content.
    I bet you will find your TextBoxes are Controls 4 through 10.
    Run your loop for TextBoxes only.

    What I'm saying is that if Control(3) is a Label or ComboBox and you ask for Text it will complain.

    See if that works.

  5. #5
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Thanks for this. Yes, you are right the TextBoxes are not listed right after each other:

    Please Login or Register  to view this content.
    There seem to be 28 controls in total. Is there a way to change their order to make the TextBoxes the first seven (0 - 6) or do I need something like this to identify the textboxes:

    Please Login or Register  to view this content.
    abousetta

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

    Re: Combining code using variables

    Hi,
    I was thinking the TabIndex would change the control index number but I'm not finding that true.

    How about trying this:

    Loop through all the controls and if the
    Left(Controls(i).Name,4) is Text then do your code on it.

    You suggested that in your last post and I think that is how I'd do it.
    Last edited by MarvinP; 06-05-2011 at 12:02 AM.

  7. #7
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    I will try that and let you know.

    You are right. I did try the tab index and it is set for sequentially running through the textboxes, but doesn't affect the control index. I guess that the control index is set up once I create a new control and cannot be modified thereafter.

    abousetta

  8. #8
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Combining code using variables

    abousetta
    do you mean .. to loop the control type?

    Please Login or Register  to view this content.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Hi Pike,

    Maybe I misunderstood your instructions, but do you mean that replace the current code:

    Please Login or Register  to view this content.
    with

    Private Sub CommandButton1_Click()

    Please Login or Register  to view this content.
    I must be doing something wrong because it is compling but it is not passing the value of the text box to the standard module

    Please Login or Register  to view this content.
    abousetta

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

    Re: Combining code using variables

    @Pike,

    Thanks for showing me

    Please Login or Register  to view this content.
    TypeName wasn't a tool in my toolbox yet but now I see how useful it can be and have needed it many times in the past.

    @abousetta
    Pike's code is an example of how to tell if the control is a TextBox. It wasn't intended to pass your text (that is in a textbox) to your main routine, but I think you are very close to getting the right answer.

  11. #11
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Combining code using variables

    Since your textboxes all have the same naming convention:
    Please Login or Register  to view this content.
    Note: you need to convert your 7 string variables to one array of Strings.
    Remember what the dormouse said
    Feed your head

  12. #12
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Thanks @romperstomper. I apologize for the repeated posts, but I still can't get it to function properly. I am attaching my latest attempt. There must be something in the concept of how this works that I don't understand.

    Thanks.

    abousetta
    Attached Files Attached Files

  13. #13
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Combining code using variables

    You didn't implement the code I posted, you implemented something else. You ned my code above and your string variable should be declared as
    Please Login or Register  to view this content.
    I don't follow the rest of your code as a lot of it makes no sense to me. For example, posn is not an array so why do you keep referring to posn(i)?

  14. #14
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Thank you for your patience. Here is the my original code that I am trying to reduce in size by removing the redundancy. As you can see, its just a series of repeated code for each textbox. What I am trying to do is reduce it to one set of code and a loop to perform the actions for each list.

    I have tried the modified code after adding the Public declaration and it now does work fine in the userform. Sorry for not understanding what you meant the first time around.

    abousetta
    Attached Files Attached Files
    Last edited by abousetta; 06-05-2011 at 03:51 PM. Reason: Forgot attachment

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Combining code using variables

    I'd put all the code in the userformmodule, starting with

    Please Login or Register  to view this content.



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

    Re: Combining code using variables

    I have no idea why I'm reading you code but I just had an idea.

    Why don't you have an array called myList_x.

    You then let
    myList_x = myList_1 and call the repeated code.
    Then let myList_x = myList_2 and call the repeated code.

    You will need to change the repeated code to deal with myList_x.

    Does that save you some carpal tunnel pain?

  17. #17
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Hi snb,

    Today is just not my day... I put the function in the userform as you suggested but I got a run-time error '1004' (Unable to set the Size property of the Font class) and it highlights the line:

    Please Login or Register  to view this content.
    abousetta

  18. #18
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Hi MarvinP,

    Thanks for looking at this. My hands are not as tired as my eyes from trying to modify this code. I am trying to simplify it so that changes can be made in one place rather than in seven. This decreases the chances for error (once I get it working of course). I honestly can't think straight anymore. My mind is in a loop of its own. I will break and try to work on this again in a bit.

    Thanks everyone for your suggestions and help.

    abousetta

  19. #19
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Hi Romperstomper,

    OK, I finally understand how you want me to set up the userform, but now I am having difficulty understanding how to pass the userinput from the textbox to the variable in the standard module:

    Please Login or Register  to view this content.
    This is the part of the original code that splits the array into individual parts
    Please Login or Register  to view this content.
    Obviously
    Please Login or Register  to view this content.
    is wrong, but I am out of ideas.

    I am attaching the original workbook with the userform code updated.

    Any help is appreciated.

    Thanks.

    abousetta
    Attached Files Attached Files

  20. #20
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Combining code using variables

    Something like this.
    Attached Files Attached Files

  21. #21
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Thank you again and again for your patience with me.

    This is exactly what I wanted.

    I will study your technique and hope to help others one day as you have helped me today.

    abousetta

  22. #22
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Hello again,

    I thank you for all your efforts. This code is much more concise and managable that what I had before. I have spent several hours studying it (slow learner) and trying to understand your technique and coding principles. I have for simplicity changed some of the coding to standardize variable names, etc. If you don't mind having a final look at it and tell me if I have done any unforseen harm to your code? It seems to run fine, but in your more knowledgable eyes, you may see hidden threats that I am unaware of.

    Thanks again.

    abousetta

  23. #23
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Combining code using variables

    Functionally the code is fine, but:
    1. You should avoid using public variables where they are not necessary (there is no need for cell or i to be public for example).
    2. I suggest you do not use the same name for the variables in the declaration of your procedures as you use for the variables you pass to them, as that may lead you to believe they are intrinsically linked in some way (which they are not, even though they are being passed ByRef)

  24. #24
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Combining code using variables

    Hi,

    Thank you for taking a look at this again and for your insight. I understand your concerns and will modify the code accordingly.

    abousetta

+ 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