Remove Spaces in the Middle of Textbox VBA

amoverton2

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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

dmt32

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

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

Dave
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
67,824
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
 

Gokhan Aycan

Active Member
Joined
Aug 8, 2021
Messages
414
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
 
Learn Excel from Bill Jelen

Understanding data is crucial, and the easiest place to start is with Microsoft Excel.

Forum statistics

Threads
1,151,573
Messages
5,765,182
Members
425,266
Latest member
CPAgirl

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