+ Reply to Thread
Results 1 to 21 of 21

Code in userform to be flexible and work with multiple textboxes

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Code in userform to be flexible and work with multiple textboxes

    Hello forum!
    Below is a code that works with a textbox named PriceBox1 in my userform. The issue is that there are about 20 textboxes that need to use the same code. How could i set up the below code to be flexible and work with the "active textbox"? Otherwise i would have to copy the below code 20 times.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code in userform to be flexible and work with multiple textboxes

    Ok, so i did some searching and found a code that works with controls that are within a frame.

    Below is my modified code which indeed uses the ctrl that is within the frame as the active control but it's not perfect. For example, if i enter a value other than LISTED or a number and i switch controls then the code thinks that the active control is the one in question when in essence it was the first control that was activated.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Code in userform to be flexible and work with multiple textboxes

    Create a Custom Class and include a local TextBox object using Dim WithEvents. Because you declared it WithEvents you will see it in the left hand drop down in Vbe and you will be able to include and event handler for your Exit event. In the event handler you can call your sTB1_Code passing the class module TextBox as a parameter.
    You can then create an instance of this class and connect its TextBox property to your active TextBox.
    Alternatively, you could make a loop to create a Collection of Objects, all instances of your custom Class, each referencing a TextBox, so that you have one for each TextBox. If the Collection is declared as Public in a standard module, it will remain active and one of the collection members will fire each time you get an Exit Event on a TextBox.
    The key words are Class Module, Dim WithEvents and Collection. You could also Google VBA Control Array.



    Please read the manual: http://msdn.microsoft.com/en-us/libr...ffice.15).aspx

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Code in userform to be flexible and work with multiple textboxes

    Maybe one way, based on the original thread:

    Please Login or Register  to view this content.

    Note that it is no longer possible to process the content of the Text Box on exit. You will need to specifically select a Command Button.

    See the attached updated sample workbook.


    Regards, TMS
    Attached Files Attached Files
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Code in userform to be flexible and work with multiple textboxes

    I suspect that the Class approach suggested by CoolBlue is the ideal way to go. Unfortunately, Class Modules are still beyond my Pay Grade

    I would welcome seeing a worked example, as my learning style is to adopt and adapt, rather than trial and error ... in other words, I'm not always good at reading and applying the information in a book or a web page. But, if I see a solution that works, that addresses a specific problem, I can use it, understand it, pick it apart, improve and expand on it (if I'm lucky). I just need a starting point.

    Regards, TMS

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code in userform to be flexible and work with multiple textboxes

    Quote Originally Posted by TMS View Post
    I suspect that the Class approach suggested by CoolBlue is the ideal way to go. Unfortunately, Class Modules are still beyond my Pay Grade

    I would welcome seeing a worked example, as my learning style is to adopt and adapt, rather than trial and error ... in other words, I'm not always good at reading and applying the information in a book or a web page. But, if I see a solution that works, that addresses a specific problem, I can use it, understand it, pick it apart, improve and expand on it (if I'm lucky). I just need a starting point.

    Regards, TMS
    So you're suggesting i upload a sample workbook?
    Last edited by kosherboy; 01-15-2015 at 08:04 PM.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,447

    Re: Code in userform to be flexible and work with multiple textboxes

    So you're suggesting i upload a sample workbook?
    Be good if someone did. I would actually like to see an example of the approach described by CoolBlue. I'm sure it would help him if he had a framework, your workbook, to work with and adapt.

    With regard to your comment in the rep about only having one Command Button, you need a means of indicating to which cell to apply the formatting and value. So, you can have a command button for each cell, or maybe a list box with the field names. You could then have a control sheet mapping the field name to a cell. So, pick a field, enter a value, click THE command button. Only one command button required.

    Regards, TMS

    Regards, TMS

  8. #8
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code in userform to be flexible and work with multiple textboxes

    Ok, so here is a sample workbook Set textboxes as range.xlsm
    Please note the blue font below in code:
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Code in userform to be flexible and work with multiple textboxes

    Actually there is a bit of a problem with the method I suggested and the reason is because of the Enter and Exit events being unavailable to MSForms.TextBox Objects declared outside of a Form Module (in a Class Module for example).

    However, thanks to this thread, I have a work around. One way anyway to hack these events.

    There are two Class Modules: one to wrap the control and another to monitor the Active control in the Form and Raise events when this changes (a pair of Exit/Enter events). The events pass the Active Control or the Previously Active Control as a parameter.

    Please find attached...
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Code in userform to be flexible and work with multiple textboxes

    Here is another solution that doesn't consume so much CPU time. The previous version relied on a loop to constantly poll the active control in the frame.
    This version responds to MouseUp events on the controls and checks if an Exit/Enter event has occurred.

    Like the previous version, this only works for controls selected by mouse.

    Here is a map of the code...
    ExitEnter.png

    Ooops, it was easier than I thought to generalise it to work with key navigation as well...
    Unfortunately, I can't see a way to delete attachments while editing so I'll have to leave the previous version...
    here is a very slightly updated map of the version 5 spreadsheet that is attached...
    ExitEnter key.png
    Attached Files Attached Files
    Last edited by coolblue; 01-19-2015 at 04:37 AM.

  11. #11
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code in userform to be flexible and work with multiple textboxes

    Hi Coolblue,
    All your posts are fascinating! These parts of excel coding i never knew existed!
    Thanks for opening this door to me, these codes will save so much time and data. Thank you!

    On all your attachments when i entered the word "listed" the word got copied onto activecell.offset(1,23), this is my bad because i did not change it in the code. I wanted this word to get copied into the specific range that the textbox was referring to. I tried tweaking your code a bit in order to implement the word "listed" but was unsuccessful as I don't have the knowledge for it as of yet.
    Also, i noticed that in post #9 the cursor was on the top textbox and would not let me skip to the next textbox unless i entered a value in the first textbox. The last 2 attachments had the cursor starting off at the most bottom textbox. Ideally i would like to have the option of leaving textbox blank.

    Once again, thank you so much for your time and effort, i can see you really worked hard on this issue.

    Looking forward to hearing from you again
    Last edited by kosherboy; 01-19-2015 at 10:34 PM.

  12. #12
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Code in userform to be flexible and work with multiple textboxes

    Hi kosherboy, the only one to worry about is Set textboxes as range CB 5.xlsm... please ignore the others: they are only there coz I can't figure out how to delete attachments.

    Not sure what you mean about the bottom-most control; it always starts with the top one in focus for me.

    The functionality you are trying to add needs to be in the sTB1_Code routine. Just be aware that it will be the same for all controls.
    The constraints about not leaving blanks is also due to your code in sTB1_Code. That routine is the behaviour that occurs after an Exit event, so just adjust it to what you need.

  13. #13
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code in userform to be flexible and work with multiple textboxes

    Hi coolblue,
    When i open the userform it opens on my second screen and has the cursor blinking on the top most cell, when i drag it to the screen where excel is displayed the cursor moves to the bottom. I am having trouble figuring this issue out and also how to make the code give me the option of selecting any textbox.
    Also, i switched activecell.offset(1,23) to mTarget and the code copied "LISTED" to the specified cell....HOW SIMPLE!

  14. #14
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Code in userform to be flexible and work with multiple textboxes

    Quote Originally Posted by coolblue View Post
    ...
    Like the previous version, this only works for controls selected by mouse.
    One can poll the KeyDown event to see when a Tab or Return key is pressed.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  15. #15
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Code in userform to be flexible and work with multiple textboxes

    Quote Originally Posted by mikerickson View Post
    One can poll the KeyDown event to see when a Tab or Return key is pressed.
    Hi Mike, happy new year!
    I actually found a better way that is event driven and avoids the need to poll. I implemented that in version 5 and it was quite simple to include keystrokes. I triggered off key up events.
    The method I used doesn't need to know which key is pressed. It just checks for a change in the ActiveControl after key up and mouse up events.

    Sent from my iPhone using Tapatalk
    Last edited by coolblue; 01-20-2015 at 10:52 AM.

  16. #16
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code in userform to be flexible and work with multiple textboxes

    I tried incorporating your code into my own using active cell method but code sent compile error message: "constant expression required". The blue font is what i changed and the red is what got bugged:
    Please Login or Register  to view this content.
    Any help on this would be great.

  17. #17
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Code in userform to be flexible and work with multiple textboxes

    It sounds like you have two custom objects.
    A clsCommonTextBox to drive the code for each of the tb's with common event code
    and a clsEveryControl to track which control is Active and fire a pseudo-Enter or Exit event.

    if clsEveryControl detects a change in ActiveControl
    - If LastActive is common then fire pseudo-Exit through clsCommonTextBox
    - If NowActive is common then fire pseudo-Enter through clsCommonTextBox

    I should look at your code, (I suspect that LastActive_pseudoExit might fire after NowActive_(true)Enter).

    Thanks for the idea.

  18. #18
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Code in userform to be flexible and work with multiple textboxes

    Quote Originally Posted by mikerickson View Post
    It sounds like you have two custom objects.
    A clsCommonTextBox to drive the code for each of the tb's with common event code
    and a clsEveryControl to track which control is Active and fire a pseudo-Enter or Exit event.

    if clsEveryControl detects a change in ActiveControl
    - If LastActive is common then fire pseudo-Exit through clsCommonTextBox
    - If NowActive is common then fire pseudo-Enter through clsCommonTextBox

    I should look at your code, (I suspect that LastActive_pseudoExit might fire after NowActive_(true)Enter).

    Thanks for the idea.
    Using your terminology, it works like this...
    The UserForm maintains a Collection of clsEveryControl objects using the Name property of the wrapped Control as a Key. It can access any of these objects using the Name of the control.
    The clsCommonTextBox object (of which there is only one) has a private routine which, when called, will check to see if the ActiveControl has changed.
    All of the clsEveryControl objects (of which there are many) have a reference to the clsCommonTextBox object.
    The clsEveryControl objects will call the controlClick method on the clsCommonTextBox object on their MouseUp or KeyUp events.
    The controlClick method in the clsCommonTextBox will execute it's private routine that checks for any change in the ActiveControl and raise custom events accordingly. The custom events pass a reference to the source control to their subscribers.
    The UserForm also has a reference to the clsCommonTextBox object and subscribes to it's events. It's event handlers can reference the source clsEveryControl object using the control name of the source control that is passed to it by the clsCommonTextBox object.

    The Exit event is raised before the Enter event.

  19. #19
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Code in userform to be flexible and work with multiple textboxes

    Quote Originally Posted by kosherboy View Post
    I tried incorporating your code into my own using active cell method but code sent compile error message: "constant expression required". The blue font is what i changed and the red is what got bugged:
    Please Login or Register  to view this content.
    Any help on this would be great.
    I don't recomend using the ActiveCell like that but if you must...
    Please Login or Register  to view this content.

  20. #20
    Forum Contributor
    Join Date
    10-30-2013
    Location
    Melbourne
    MS-Off Ver
    Excel 2013
    Posts
    173

    Re: Code in userform to be flexible and work with multiple textboxes

    I'm not happy with the architecture of the previous versions so I am attaching another version which has much better encapsulation.
    I moved all of the positioning and supervisory code back into the form and dramatically simplified the TextBox and Frame wrapper objects. The only functionality remaining in these objects now is that which is required for producing the Exit/Enter events. The cTextBox Class could easily be generalised by adding redundant variables of any control type that needs to be supported (and event handlers to match), along with a Select statement in the Control setter. The cFrameEvents class is also easily extended to support multiple control types.

    kosherboy, with this version, you only need to modify code in the form (textBoxTargetRange and sTB1_Code).

    ExitEnter 6.png
    Attached Files Attached Files

  21. #21
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code in userform to be flexible and work with multiple textboxes

    Thank you Coolblue for all your help and support. I really appreciate all the time you spent on this thread. I learned a lot from it and i'm very thankful to you for that.

    Best regards,
    Kosherboy

+ 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. SetFocus doesn't work properly on a Userform on ComboBoxes and TextBoxes
    By pimre in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-15-2013, 03:15 PM
  2. Need help to get code created Userform textboxes to fire a change event
    By brusk in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-05-2013, 12:06 AM
  3. Code to prevent userform from saving duplicates based on 3 textboxes
    By Zygoid in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-28-2013, 11:04 AM
  4. Replies: 1
    Last Post: 11-29-2010, 06:56 AM
  5. Formatting Multiple Userform Textboxes
    By badeye in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-01-2010, 09:14 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