Consulting

Results 1 to 20 of 20

Thread: Solved: variable ProperCase

  1. #1

    Solved: variable ProperCase

    Is it possible to capitalize the first letter, but still be able to captilize any other letter I choose to capitalzie?

    i.e. McDonald

  2. #2
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Assuming
    Cell (A2) has "mcdonald" in it
    [VBA] =PROPER(LEFT(A2,2))&PROPER(RIGHT(A2,6))[/VBA]
    I am a Newbie, soon to be a Guru

  3. #3
    can this be done in VBA?

  4. #4
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    [VBA]Function PCase(Rn As Range, i As Integer, j As Integer) As String
    Dim Txt As String
    Txt = Rn.Value
    PCase = WorksheetFunction.Proper(Left(Txt, i)) & WorksheetFunction.Proper(right(Txt, j))

    End Function
    [/VBA]

    You can use this function in a cell as = PCase(A2,2,6). The order of the arguments will be left then right
    I am a Newbie, soon to be a Guru

  5. #5
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    [vba]
    Sub propercase()
    Dim strValue As String
    strValue = "mcdonald"
    strValue = WorksheetFunction.Proper(Left(strValue, 2)) & WorksheetFunction.Proper(Right(strValue, 6))
    Debug.Print strValue
    End Sub
    [/vba]

    Mine was just an example of how nepotist went all the way and gave you a nice UDF.

  6. #6
    Hi guys, this doesn't work in a userform textbox. is there an alternative code for a userform textbox?

  7. #7
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Try using the after update event of the text box to reformat it, are you trying to use the value of the textbox and save it somewhere??
    I am a Newbie, soon to be a Guru

  8. #8
    I'm only trying to do this in a textbox.

  9. #9
    VBAX Mentor
    Joined
    Aug 2008
    Posts
    323
    Location
    Then Use after update event
    I am a Newbie, soon to be a Guru

  10. #10
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Try

    [VBA]Private Sub TextBox1_Change()
    With TextBox1
    .Text = UCase(Left(.Text, 1)) & Mid(.Text, 2)
    End With
    End Sub[/VBA]

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If it is just Mcs and Macs you wish to capitalise then this should do it. However not all suffixes are capitalised. Macdonald is also valid.
    [VBA]Option Explicit
    Option Compare Text

    Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Dim t
    t = TextBox1
    If Left(t, 2) = "mc" Then
    t = UCase(Left(t, 1)) & "c" & UCase(Mid(t, 3, 1)) & Right(t, Len(t) - 3)
    ElseIf Left(t, 3) = "mac" Then
    t = UCase(Left(t, 1)) & "ac" & UCase(Mid(t, 4, 1)) & Right(t, Len(t) - 4)
    Else
    t = UCase(Left(t, 1)) & Right(t, Len(t) - 1)
    End If

    TextBox1 = t
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  12. #12
    VBAX Expert
    Joined
    Aug 2007
    Location
    Windermere, FL, a 'burb in the greater Orlando metro area.
    Posts
    567
    Location
    So, what about our Celtic cousins with O' names?

    Slainté,
    Ron
    Windermere, FL

  13. #13
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    [VBA]Option Explicit
    Option Compare Text

    Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    Dim t
    t = TextBox1
    If Left(t, 2) = "mc" Then
    t = UCase(Left(t, 1)) & "c" & UCase(Mid(t, 3, 1)) & Right(t, Len(t) - 3)
    ElseIf Left(t, 3) = "mac" Then
    t = UCase(Left(t, 1)) & "ac" & UCase(Mid(t, 4, 1)) & Right(t, Len(t) - 4)
    ElseIf Left(t,2) = "o'" then
    t = UCase(Left(t, 2)) & UCase(Mid(t, 3, 1)) & Right(t, Len(t) - 3)
    Else
    t = UCase(Left(t, 1)) & Right(t, Len(t) - 1)
    End If

    TextBox1 = t
    End Sub
    [/VBA]
    There ya go.

  14. #14
    Hi everyone,

    Thank you everyone for the suggestion, but Mikerickson code acomplish what I'm looking for with the exception that if there is a space between word in a sentence, it does not continue to capitalize the first letter.

    for example: My Name Is McDonald McPherson

    [vba]
    With txtAirport
    .Text = UCase(Left(.Text, 1)) & Mid(.Text, 2)
    End With
    [/vba]
    The other codes does the same thing.

  15. #15
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Picky Picky. You didn't say it needed to deal with multiple words.
    [VBA]
    Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    Dim arr
    Dim x
    Dim t
    arr = Split(TextBox1, " ")

    For x = 0 To UBound(arr)


    If Left(arr(x), 2) = "mc" Then
    arr(x) = UCase(Left(arr(x), 1)) & "c" & UCase(Mid(arr(x), 3, 1)) & Right(arr(x), Len(arr(x)) - 3)
    ElseIf Left(arr(x), 3) = "mac" Then
    arr(x) = UCase(Left(arr(x), 1)) & "ac" & UCase(Mid(arr(x), 4, 1)) & Right(arr(x), Len(arr(x)) - 4)
    ElseIf Left(arr(x), 2) = "o'" Then
    arr(x) = UCase(Left(arr(x), 2)) & UCase(Mid(arr(x), 3, 1)) & Right(arr(x), Len(arr(x)) - 3)
    Else
    arr(x) = UCase(Left(arr(x), 1)) & Right(arr(x), Len(arr(x)) - 1)
    End If
    Next
    For x = 0 To UBound(arr)
    t = t & " " & arr(x)
    Next
    TextBox1 = Trim(t)
    End Sub[/VBA]

  16. #16
    Please don't be offended, as I do really appreciate all your help.

    Out curiosity, I've tried to use this in the change event but it errors on this part of the code when i click the space bar. Is it possible to use this in the change event?

    [VBA]arr(x) = UCase(Left(arr(x), 1)) & Right(arr(x), Len(arr(x)) - 1)
    [/VBA]

  17. #17
    VBAX Mentor
    Joined
    Feb 2009
    Posts
    493
    Location
    Not really as change calls the procedure on every key stroke and the procedure is looking for something after mc so after you type m it runs then c it runs and errors out.

    The before update event should work for you anyway.

  18. #18
    ok...thank you

  19. #19
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    This is a multi-word variation on my previous
    [VBA]Private Sub TextBox1_Change()
    Static tbChangeDisabled As Boolean
    Dim Words As Variant, i As Long
    If tbChangeDisabled Then Exit Sub

    tbChangeDisabled = True
    With TextBox1
    Words = Split(.Text)
    .Text = vbNullString

    For i = 0 To UBound(Words)
    .Text = LTrim(.Text & " " & UCase(Left(Words(i), 1)) & Mid(Words(i), 2))
    Next i
    End With
    tbChangeDisabled = False
    End Sub[/VBA]

  20. #20
    PERFECT!!!! thank you, Mikerickson

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •