lardman363
Automotive
- Feb 8, 2013
- 173
Imagine you have an excel spreadsheet with data in a column. You want to loop through each cell in the column and give the user an option to keep what is in the cell or change it.
I made a simple userform (textbox and a button) that simulates looping through data. Each time new data is automatically entered in the textbox, I would like it to be highlighted automatically...because it is 99% certain the user will want to enter something different.
When they pick the button, what ever is in the textbox will be entered in the cell and the code moves on to the next cell, adds the next cell's value to the text box, repeat
The problem is I can only get the text to highlight on the first iteration.
I have read about retaining focus using the BeforeUpdate event on the text box but I cannot get it to work...it prevents me from selecting the button
Everyone seems to need to check to see if the textbox1.value adheres to some rule, I don't need to check it...i just need it to be highlighted each time a default value is entered automatically in the textbox.
Ant help is appreciated.
I made a simple userform (textbox and a button) that simulates looping through data. Each time new data is automatically entered in the textbox, I would like it to be highlighted automatically...because it is 99% certain the user will want to enter something different.
When they pick the button, what ever is in the textbox will be entered in the cell and the code moves on to the next cell, adds the next cell's value to the text box, repeat
The problem is I can only get the text to highlight on the first iteration.
Code:
Private iCounter As Integer
Private Sub UserForm_Initialize()
iCounter = 1
TextBox1.Text = CStr(iCounter)
End Sub
Private Sub TextBox1_Enter()
'This works only the first time
TextBox1.SetFocus
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End Sub
Private Sub CommandButton1_Click()
If iCounter < 3 Then
iCounter = iCounter + 1
Else
Unload Me
End If
TextBox1.Text = CStr(iCounter)
End Sub
I have read about retaining focus using the BeforeUpdate event on the text box but I cannot get it to work...it prevents me from selecting the button
Code:
Private Sub UserForm_Initialize()
iCounter = 1
TextBox1.Text = CStr(iCounter)
End Sub
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = True
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
End Sub
Private Sub CommandButton1_Click()
If iCounter < 3 Then
iCounter = iCounter + 1
Else
Unload Me
End If
TextBox1.Text = CStr(iCounter)
End Sub
Everyone seems to need to check to see if the textbox1.value adheres to some rule, I don't need to check it...i just need it to be highlighted each time a default value is entered automatically in the textbox.
Ant help is appreciated.