Results 1 to 8 of 8

Thread: Add dashes between letters

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Newbie
    Joined
    Oct 2019
    Posts
    4
    Location

    Add dashes between letters

    I am trying to make macro to add dashes between all the letters in a word. I want to have names be spelled out with dashes between them. I do not want the name to be automatically upper case. I have a found a macro online that is similar to what I want to do and have attempted to edit it to make it do what want it to do. Every time I edit it, the If statement starting on line 17 throws a break error.


    Sub CapDashNames()
        Dim sTemp As String
        Dim sName As String
        Dim J As Integer
        sTemp = UCase(Selection.Range.Text)   ' Make all uppercase
        If Len(sTemp) > 1 Then
            sName = ""
            For J = 1 To Len(sTemp) - 1
                ' Add new character to name
                sName = sName & Mid(sTemp, J, 1)
                If Mid(sTemp, J, 1) >= "A" And Mid(sTemp, J, 1) <= "Z" Then
                    ' Add a dash if character was a letter
                    sName = sName & "-"
                Else
                    ' Character added was not a letter
                    If Mid(sName, Len(sName) - 1, 1) = "-" Then
                        ' If there is a dash just before non-letter,
                        ' get rid of it
                        sName = Left(sName, Len(sName) - 2)
                        sName = sName & Mid(sTemp, J, 1)
                    End If
                End If
            Next J
            ' Add final character
            sName = sName & Right(sTemp, 1)
            Selection = sName
        End If
    End Sub
    Last edited by Aussiebear; 07-08-2025 at 06:29 PM.

Posting Permissions

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