+ Reply to Thread
Results 1 to 8 of 8

Selection change event on text inside a textbox

Hybrid View

  1. #1
    Registered User
    Join Date
    02-11-2014
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Selection change event on text inside a textbox

    Hello,
    This is the first question I post. I have been using the Excel Forum for quite a while now and it has been extremely useful so far for finding answers in older threads so thank you everybody However this time I can’t find anything so I though I’ll ask the question.

    As part of a program I’m writing in VBA (for Excel 2010), I have a textbox in a user form used as an interface to write a formula in cells in Excel.
    The resulting value of a formula from a cell is loaded up into the textbox. It would be shown in the textbox like “See 1.2 and 1.3” where the formula in the cell is
    =”See “ & CellAddress1.value & “ and “ & CellAddress2.value
    This is just used as an example but the principle is there.
    It is worth noting that I’m writing this for very inexperienced Excel users but I need them to be able to edit the string part of the formula without breaking the formula.

    However where I’m struggling is to pick up a selection change event inside a text box already selected. I need to be able to check if the textbox.SelStart is within an address value or within the string in the textbox.
    The event Enter won’t work if the user is already editing the textbox (i.e. typing stuff) and then clicking or using the keyboard arrow to move the cursor somewhere else inside the textbox. I don’t think the event Change is the solution either as it would mean that the user would have already typed something and as a result the formula may already be broken.

    I have had a good look around and I didn’t find an event for a selection change inside a textbox. Does it exists and/or is there a way that would have the same result?

    Many thanks in advance.

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Selection change event on text inside a textbox

    I don't quite follow the specifics, but it seems you want to capture what people are typing into the list box in real time. I can't think of an obvious method off the top of my head, and it sounds like you have done some research that has led to dead ends. So, it is time to think of other possibilities.

    First, capturing the typing events does not seem to be possible. Maybe you can add a 'Test' button that the user will press to ensure their input is valid? Of course, the test routine would also run when the user hits 'OK' so that you can keep the form open with a warning message that the input contains errors.

    Second, maybe you could use the Application.OnTime method to run a function which reads the textbox every second and checks for accuracy? This may be annoying as the person is typing and the partial input has errors.
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

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

    Re: Selection change event on text inside a textbox

    I reckon you'll need to trap the keyup-testing if the key is an arrow key-and mouseup events and check the selstart then
    Josie

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

  4. #4
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Selection change event on text inside a textbox

    @JosephP - would those work inside a form box? I wasn't sure and it seems the OP looked at some of the other events.

  5. #5
    Registered User
    Join Date
    02-11-2014
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Selection change event on text inside a textbox

    Thank you Josie and Pauley for your answers. This is very helpful I thought I hit a wall on that one but it seems that it could work.

    The mouseup event works by using the code below from this website:

    http://www.vbaexpress.com/forum/show...MouseUp-Events

    Option Explicit 
    Enum MouseButtons 
        fmButtonLeft = 1 'The left button was pressed.
        fmButtonRight = 2 'The right button was pressed.
        fmButtonMiddle = 4 'The middle button was pressed.
    End Enum 
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) 
        Select Case Button 
        Case fmButtonLeft 
            MsgBox "The left button was pressed" 
        Case fmButtonRight 
            MsgBox "The right button was pressed" 
        Case fmButtonMiddle 
            MsgBox "The middle button was pressed" 
        End Select 
    End Sub
    It seems that it is possible to use the KeyUp event but I can’t get that one to work yet. Here is the code I found:

    Private Sub Textbox1_KeyUp(KeyCode As Integer, Shift As Integer)
    
        If KeyCode = vbKeyLeft Then
            MsgBox "Left arrow"
        ElseIf KeyCode = vbKeyRight Then
            MsgBox "Right arrow"
        End If
        
    End Sub
    I found it on this website.
    http://vbcity.com/forums/t/12201.aspx

    However I get the following error message:
    Compile error:
    Procedure declaration does not match description of event or procedure having the same name.

    Is this to do with the declaration of KeyCode and Shift?

    Thank you for your help

    JL

  6. #6
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Selection change event on text inside a textbox

    Hmm, well, it usually means you are not matching the expected parameters, but it appears you are. Only thing I see suspicious is that the top code says TextBox1 and the bottom one says Textbox1. Probably the capital B.

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,320

    Re: Selection change event on text inside a textbox

    The procedure declaration should actually be:
    Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    as userforms are not the same as the VB6 forms in that link.
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Registered User
    Join Date
    02-11-2014
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Selection change event on text inside a textbox

    Perfect! It works now with Rory’s procedure declaration

    Thank you so much all!

    J-L

+ 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] Textbox Change event handler works once
    By Solus Rankin in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-30-2013, 12:28 PM
  2. TextBox Change Event HELP
    By dluhut in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-13-2013, 10:16 AM
  3. How to search a text inside the textbox
    By azid_miracle in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-04-2012, 08:20 AM
  4. change selection event firing before selection change
    By martindwilson in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-01-2009, 09:34 AM
  5. Textbox Change event
    By Kerno in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-03-2007, 05:08 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