format initials

wCJanssen

New Member
Joined
Feb 22, 2009
Messages
24
Hi,

I'm looking for a macro that inserts a dot after each character in a userform textbox, in order to format initials. Thanks in advance.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
This will put you on the right track:

Code:
Function withdots(sText As String) As String

    For i = 1 To Len(sText)
        withdots = withdots & Mid(sText, i, 1) & IIf(Mid(sText, i, 1) <> " ", ".", "")
    Next

End Function

Sub f()

    MsgBox withdots("mister excel")

End Sub
 
Upvote 0
You should be able to do it with the TextBox Change event:

Code:
Private Sub TextBox1_Change()
Application.EnableEvents = False
    If Right(TextBox1, 1) <> "." Then TextBox1 = TextBox1 & "."
Application.EnableEvents = True
End Sub
 
Upvote 0
Hello Hotpepper,

Application.EnableEvents does not work on userforms, only in worksheets.
 
Upvote 0
Thanks, I was not aware of that, but it can also be used in Workbook code.

At any rate, you can use the TextBox Change event to insert the periods.

Code:
Private Sub TextBox1_Change()
If Right(TextBox1, 1) <> "." Then TextBox1 = TextBox1 & "."
End Sub
 
Last edited:
Upvote 0
The only thing that'd annoy me, is that backspace does not work since it generates also a change event... Selecting letters and deleting is possible, though.

Better to use the KeyDown event?
 
Upvote 0
KeyUp:

Code:
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode > 64 And KeyCode < 90 Then TextBox1 = UCase(TextBox1) & "."
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,513
Messages
6,114,064
Members
448,545
Latest member
kj9

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