Quantcast
Channel: Excel Help Forum - Excel Programming / VBA / Macros
Viewing all articles
Browse latest Browse all 50199

Highlight Textbox Text during a loop

$
0
0
This is driving me crazy...

I found a lot of information online, but it is confusing.

I have a userform that loops through data and enters the current value as textbox1.text. I want the textbox1.text to be highlighted automatically so the user doesn't have to highlight it all if they want to change the value. I can get the highlight to work only on the first iteration (iCounter = 1), when iCounter > 1 the text will not highlight.

I don't need to check the .text, but I think I need a Cancel = True in there somewhere...just not sure where...or if I am using the correct event. The .setfocus automatically moves to the CommandButton...this is why I think the highlight will not occur.

All I want to do is have the text in the textbox highlight every time iCounter increases. Any help would be appreciated.

Code:

Private iCounter As Integer

Private Sub UserForm_Initialize()
    iCounter = 1
    TextBox1.Text = CStr(iCounter)
End Sub

Code:

Private Sub TextBox1_Enter()
    'This works only the first time
    TextBox1.SetFocus
    TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Text)
End Sub

Code:

Private Sub CommandButton1_Click()
    If iCounter < 3 Then
        iCounter = iCounter + 1
    Else
        Unload Me
    End If
    TextBox1.Text = CStr(iCounter)
End Sub

Capture.JPG

Viewing all articles
Browse latest Browse all 50199

Trending Articles