Trim to Remove Space at the end from a Userform Text Box

menor59

Well-known Member
Joined
Oct 3, 2008
Messages
574
Office Version
  1. 2021
Platform
  1. Windows
Hello all...I have the following code, which works Great, assigned to a Text box on my userform...

VBA Code:
Private Sub txtClientDoor_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    txtClientDoor.Value = StrConv(txtClientDoor.Value, vbProperCase)
End Sub


This keeps people from doing:

The QuisK BRown FOX juMPs

To

The Quick Brown Fox Jumps

But as usual Users keep putting a Space at the end..

I need to remove that extra SPACE at the end of the Value so that:

The Quick Brown Fox Jumps_

"_" = Space bar strike

to

The Quick Brown Fox Jumps

PLease
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You can use the RTrim function to remove trailing spaces . . .

VBA Code:
Private Sub txtClientDoor_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    txtClientDoor.Value = StrConv(RTrim(txtClientDoor.Value), vbProperCase)
End Sub

By the way, the Trim function removes both leading and trailing spaces. So you may want to use Trim instead of RTrim, just in case.

Hope this helps!
 
Upvote 0
You can use the RTrim function to remove trailing spaces . . .

VBA Code:
Private Sub txtClientDoor_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    txtClientDoor.Value = StrConv(RTrim(txtClientDoor.Value), vbProperCase)
End Sub

By the way, the Trim function removes both leading and trailing spaces. So you may want to use Trim instead of RTrim, just in case.

Hope this helps!
Thats actually better....Also!!! Thank you!
 
Upvote 0
Question


CAn it be Modified to Remove double, triple spaces Between TEXT also??

so that:

The (2 spaces Here) quick (4 SPACES here) Brown fox

returns

The Quick brown fox??

Basically keep 1 space between text?
 
Upvote 0
Try this...
VBA Code:
txtClientDoor.Value = StrConv(Application.Trim(txtClientDoor.Value), vbProperCase)
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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