Textbox starts at wrong character position

Denny57

Board Regular
Joined
Nov 23, 2015
Messages
185
Office Version
  1. 365
Platform
  1. Windows
I have a textbox to which I have assigned some code so that 6 digit numbers are displayed split 3 & 3 with a space between each set and uploaded to the worksheet as "nnn nnn". This is also the format that successfully returns to the userform following a successful search.

The Code is
Private Sub txtUnitNumber_AfterUpdate()

txtUnitNumber.Text = Left(txtUnitNumber, 3) & " " & Right(txtUnitNumber, 3)

End Sub

After I use the Add, Clear or Update commands I want the userform to remain on the screen and be ready for the next action. However when I use these command buttons , the cursor appears in position 2 of the textbox.

My code in all three instances the code includes the instruction Call UserForm_Initialize.

Can someone suggest some code that will clear the userform and set the focus in the "txtUnitNumber" textbox so the cursor appears in position 1. I am sure that this has something to do with the re-formatting code (as Above)

All suggestions are most welcome
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
add this line
VBA Code:
txtUnitNumber.SelStart = 0
 
Upvote 0
Try

VBA Code:
txtUnitNumber.Text = Replace(txtUnitNumber.Text, " ", vbNullString)
txtUnitNumber.SetFocus
 
Upvote 0
Pleease ignore my earlier reply. I misread your request

I suspect your code is being triggered again and placing an unwanted space in the textbox

Consider using a condition to prevent that from happening
VBA Code:
Private Sub txtUnitNumber_AfterUpdate()
If Len(txtUnitNumber.Text) = 6 And InStr(txtUnitNumber.Text, " ") = 0 Then
    txtUnitNumber.Text = Left(txtUnitNumber, 3) & " " & Right(txtUnitNumber, 3)
End If
End Sub
 
Upvote 0
How about

VBA Code:
txtUnitNumber.Text = Format(Val(Replace(tstUnitNumber.Text, " ", vbNullString)), "00 00")
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top