Remove Spaces in the Middle of Textbox VBA

amoverton2

Board Regular
Joined
May 13, 2021
Messages
65
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

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
7,912
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Hi,
try

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

Dave
 
Upvote 0
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
81,661
Office Version
  1. 365
Platform
  1. Windows
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

Gokhan Aycan

Active Member
Joined
Aug 8, 2021
Messages
458
Office Version
  1. 365
Platform
  1. Windows
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,186,807
Messages
5,959,919
Members
438,453
Latest member
NRG909

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
Top