PDA

View Full Version : Help with a macro



Kindyr
10-20-2019, 02:06 PM
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

Logit
10-20-2019, 03:49 PM
I reduced your existing macro to :



Option Explicit


Sub AddDashes1()
Dim Cell As Range
Dim sTemp As String
Dim C As Integer


For Each Cell In Selection
sTemp = ""
For C = 1 To Len(Cell)
sTemp = sTemp + Mid(Cell, C, 1) + "-"
Next
Cell.Value = Left(sTemp, Len(sTemp) - 1)
Next
End Sub


Select the cell and click the button.

Kindyr
10-20-2019, 04:43 PM
This is for Word, not Excel. This has it looking for a Cell to edit. I'm working in Word and need to be able to just highlight a word and add dashes between the letter.


I reduced your existing macro to :



Option Explicit


Sub AddDashes1()
Dim Cell As Range
Dim sTemp As String
Dim C As Integer


For Each Cell In Selection
sTemp = ""
For C = 1 To Len(Cell)
sTemp = sTemp + Mid(Cell, C, 1) + "-"
Next
Cell.Value = Left(sTemp, Len(sTemp) - 1)
Next
End Sub


Select the cell and click the button.

Logit
10-20-2019, 07:21 PM
.
Admittedly WORD is not my forte. Apologies for not realizing you were working in WORD.

Having said that, here is a short work around to obtain the results you are seeking :



Sub CapDashNames()
Dim sTemp As String
Dim sName As String
Dim J As Integer


Application.ScreenUpdating = False


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

Selection = LCase(Selection.Range.Text)


Application.ScreenUpdating = True


End Sub

gmayor
10-20-2019, 08:30 PM
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.
Try the following. Put the cursor in the word and run the macro


Sub Macro1()Dim orng As Range
Dim i As Integer
Set orng = Selection.Words(1)
orng.MoveEndWhile Chr(32) & Chr(45), wdBackward
For i = orng.Characters.Count - 1 To 1 Step -1
orng.Characters(i).InsertAfter Chr(45)
Next i
Set orng = Nothing
End Sub

Kindyr
10-21-2019, 07:11 AM
Oh! This is almost entirely perfect! The next step is to figure out how to make the hyphens non-breaking so they do not wrap at the end of a line. In side of word, I can use Shift-Ctl-HYPHEN to make a non-breaking hyphen. Is there a way to make the hyphen in this macro non-breaking?

gmaxey
10-21-2019, 07:18 AM
I think Graham is packed up for the day. Change to:
orng.Characters(i).InsertAfter Chr(30)

Kindyr
10-21-2019, 07:24 AM
Thank you Greg, that was perfect. I had thought Chr(30) would return a 30 instead of a non-breaking hyphen. This is exactly what I wanted.