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.
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,944
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
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,886
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
 

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,944
Hello Hotpepper,

Application.EnableEvents does not work on userforms, only in worksheets.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,886
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:

wigi

Well-known Member
Joined
Jul 10, 2006
Messages
7,944
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?
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,886
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
 

Forum statistics

Threads
1,082,176
Messages
5,363,583
Members
400,752
Latest member
HamerTime7119

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top