+ Reply to Thread
Results 1 to 19 of 19

User-form text box, insert text at cursor location

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    User-form text box, insert text at cursor location

    With this code, I can select multiple cells in Excel and the strings are displayed in form textboxes automatically.
    There is a button in the form which is intended to insert the character "#" at the cursor location of the textbox.
    There are 2 problems:
    (1) It inserts the character "#" in all textboxes rather than just the 1 textbox where the cursor is.
    (2) In the first textbox, it does insert the "#" character at the cursor location, as intended. With the other textboxes, it will either insert the character at the end or the beginning.
    How can I fix this?

    Snapshot.png

    Module code
    Please Login or Register  to view this content.
    Form code
    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    insert the # in the textbox rather than calling the initialize userform event each time.

    Please Login or Register  to view this content.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    It still gives me the same issues as in the original post.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    When I test it the # is added where ever the insertion point for each text box.

    If you only want to update 1 textbox you will need a way to identify which one. Either provide insert button form each textbox or maintain a reference to last exited textbox. Both of these will require class object to trap events of controls added at run time.

  5. #5
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    Does a class object have a relationship to a class module?
    Have you seen any examples which relate to what I'm trying to do?

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    It's still not clear, to me at least, exactly what you are trying to do.

    Yes you would need a class module to holder the code and then create objects based on it.

    This may help
    http://yoursumbuddy.com/raiseevent-m...form-controls/

  7. #7
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    Thanks, I will have to digest this.
    What I'm trying to do: To insert the character "#" in a single chosen textbox on the form (which then transfers it to a single cell which has a relationship to that textbox) ... at the cursor location. The cursor location is on the form, at one of the textboxes.
    One of the issues is that the code inserts the character in all textboxes.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    The biggest problem you have is that the Enter and Exit routines are not exposed when controls are added on the fly.

    Simplest solution could be to add button for each textbox

  9. #9
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    I got to the point of adding a button for each textbox, but then I stopped for 2 reasons, firstly because there will be 10-20 different types of characters like the "#" case here ... so the form will become unmanageable, and also I don't know how write the Sub for a variable number of button click events.

    I like your idea of the class module. Do you think it's worth pursuing or will the level of complexity be too high?

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    Why do you need a button to add the # or whatever other characters? Can the person not just type in the textboxes to change the contents?
    Once they have finished then you update the contents of the cells.

    If there is a subset of characters to be used then you could use a class to restrict the characters the user can enter.

  11. #11
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    Quote Originally Posted by Andy Pope View Post
    Can the person not just type in the textboxes to change the contents?
    The characters will be Private Use Characters which are created in a font editor. For this reason I can't type them in.

  12. #12
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    I can understand a bit better what is happening.
    With every textbox the cursor is automatically set after the last character and seems to remain there. That's why the "#" character is added to all textboxes/cells.
    The code below checks if the cursor is not at the last position. It's not a great solution because I can't add at the end of the string.
    There seems to be a ".SetFocus" property for form textboxes but this may be if the string or part of the string is highlighted. I wonder if there is something similar for a cursor position ... but then again the cursor seems to be in all textboxes ... I don't know.
    Do you think it's still worth pursuing the class module method?

    Please Login or Register  to view this content.

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    If you change the cursor position in each textbox it will insert the character in the correct place.
    But you said you did not want to do all the textboxes at once.

    Another alternative is to put the insertable characters in a list or dropdown and then use the button per textbox to insert character into correct textbox.

  14. #14
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    Quote Originally Posted by Andy Pope View Post
    Another alternative is to put the insertable characters in a list or dropdown and then use the button per textbox to insert character into correct textbox.
    I wouldn't know how write the Sub for a variable number of textboxes though.

    Is the class module still in play? Is this how to tell the code which is the active textbox?

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    You would need two classes. One for the dynamic controls (textbox and button) and delegation for event raising.

    Additionally there is a listbox of special characters.
    Attached Files Attached Files

  16. #16
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    @Andy Pope.
    Thanks very much for your input.
    This works as intended.

  17. #17
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    Would it be possible to advise if; rather than having a button for each textbox, the single button at the top could be used. I know you have assisted a lot, so a Yes or No would at least give me a direction.

  18. #18
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,461

    Re: User-form text box, insert text at cursor location

    No. As stated before there is no Enter/Exit event for controls on the fly. Therefore you would not know which textbox to update.

  19. #19
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: User-form text box, insert text at cursor location

    OK thanks very much for closing this out.

+ 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. Populate text boxes on user form, from combo box on same user form
    By Richardswaim in forum Excel Programming / VBA / Macros
    Replies: 26
    Last Post: 07-03-2016, 09:35 AM
  2. Delimited Text To Columns Utlizing a User Form Text Box Can't Manually Enter Data
    By simpdogg in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-04-2016, 03:58 PM
  3. [SOLVED] populate text box in user form with cell location based on click within worksheet
    By mcdermott2 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 01-19-2015, 09:15 PM
  4. [SOLVED] Pop up calendar to insert date in text box on user form
    By emilyloz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-11-2013, 08:16 AM
  5. Insert Rows Based on text Box Entry in User Form
    By aqhibjaveed2183 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-16-2013, 02:44 AM
  6. Replies: 12
    Last Post: 10-03-2008, 09:02 AM
  7. Formatting text in a user form text box
    By DB in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-23-2005, 04:05 PM

Tags for this Thread

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