Remove all but first word in Textbox

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. 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?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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)...

TextBox1.Value = Left(TextBox1.Value & " ", InStr(TextBox1.Value, " ") - 1)
 
Upvote 0
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
HTH. Dave
 
Upvote 0
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
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...
Code:
[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]
 
Last edited:
Upvote 0
Guys, can I ask another question please?

I'm now trying to achieve the same on a worksheet and need it to apply to all cells between C2:C30. However, some of those cells will only contain 1 word so is there a way I can achieve it without it throwing an error if there is only 1 word anyway, i.e. get it to ignore cells with only 1 word?
 
Upvote 0
Try:
Code:
Sub Test()
    Dim rng As Range
    For Each rng In Range("C2:C30")
        If InStr(1, rng, " ") > 0 Then
            rng = Split(rng)(0)
        End If
    Next rng
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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