Remove Spaces in the Middle of Textbox VBA

amoverton2

Board Regular
Joined
May 13, 2021
Messages
77
Office Version
  1. 2016
Platform
  1. Windows
Still learning VBA...

I have userform with a textbox to enter LAST,FIRST NAME. I want no spaces between the last name and first name so it matches with other data in the workbook for other things.

Examples: Good: JORDAN,MICHAEL, Bad: JORDAN, MICHAEL.

I am looking for a VBA code that if a person does enter JORDAN, MICHAEL, an event (AfterUpdate or Exit) removes the space between last and first name. I figured out how to convert it to all uppercase if someone puts it in lowercase but now I need to remove any spaces. Everything I've seen is either beginning or end of the text string, nothing in the middle. If there is a thread out there please point me there and I'll close this one up quickly.

Thanks for your time.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi,
try

VBA Code:
Private Sub TextBox1_AfterUpdate()
     Me.TextBox1.Value = Replace(Me.TextBox1.Value, " ", "")
End Sub

Dave
 
Upvote 0
Solution
Another option in-case you can have names like van Vleuten, Annemiek
VBA Code:
Private Sub TextBox1_AfterUpdate()
     Me.TextBox1.Value = Replace(Me.TextBox1.Value, ", ", ",")
End Sub
 
Upvote 0
What if the user enters more than 1 space in there? :p

Maybe a for next loop with count of spaces that executes the above code would be better. Code not tested.

Edit: Added the trim part.

VBA Code:
Private Sub TextBox1_AfterUpdate()

    Dim i as Integer
    Dim str as String
    Dim Count as Integer

    str = Trim (Me.TextBox1.Value)
    Count = Len (str) - Len (Replace (str, " ", ""))

    For i = 1 to Count
        str = Replace (str, ", ", ",")
    Next i

    Me.TextBox1.Value = str

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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