sharky12345
Well-known Member
- Joined
- Aug 5, 2010
- Messages
- 3,422
- Office Version
- 2016
- Platform
- Windows
Can someone show me how to remove all words, characters and extra spaces from a Userform textbox with the exception of the first word?
Assuming your TextBox is named TextBox1, then this line of code (not sure where you want to execute it from) will change whatever text is in the TextBox to only the first word (assuming words are separated from each other by spaces)...Can someone show me how to remove all words, characters and extra spaces from a Userform textbox with the exception of the first word?
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim a As Variant
a = Split(TextBox1.Text, " ")
TextBox1.Text = a(0)
End Sub
You do not have to declare a variable to receive the array from the Split function in order to pull an element from it, you can do that directly from the array Split is creating. Also, the space delimiter is the default if omitted from the Split function. Give this, the following should do what your posted code does...Maybe...
Code:Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean) Dim a As Variant a = Split(TextBox1.Text, " ") TextBox1.Text = a(0) End Sub
[table="width: 500"]
[tr]
[td]Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox1.Text = Split(TextBox1.Text)(0)
End Sub[/td]
[/tr]
[/table]