Written by Allen Wyatt (last updated January 24, 2022)
This tip applies to Word 2007, 2010, 2013, and 2016
Lorraine often works with transcripts of interviews. Interviewers will ask interviewees to spell out their names. When she types these up, she needs to insert dashes between letters to show they are being spelled, as in J-O-H-N D-O-E. Lorraine wonders if there is a way to select the name and then run a macro to capitalize the name and insert the requisite dashes between letters.
Macros are very well suited for doing this type of text processing. In fact, there are probably a dozen or so ways you could approach the problem in your macro. The following is a rather simple way to do it:
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
Basically, the macro steps through whatever you've selected and adds a dash after each alphabetic character. If that dash is then followed by a non-alphabetic character, then the dash is removed. (That way you don't, for example, end up with a dash before or after a space.)
In order to use the macro, simply select the name you want to modify, and then run the macro.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1354) applies to Microsoft Word 2007, 2010, 2013, and 2016.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
Have you ever noticed how Word can decide to add extra spaces when you paste information into your document? This is part ...
Discover MoreDrag-and-drop editing is a handy feature when you love to use the mouse. There are two ways you can move text using the ...
Discover MoreThe traditional way to insert symbols into a document is to use the Symbol dialog box. This tip looks at ways other than ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-08-22 20:41:01
Maria
I would be interested to know how to create a similar macro, but with only first letter of name capitalised, and using non-breaking hyphens between the letters to ensure the hyphenated name doesn't break at the end of a line.
2020-06-27 01:52:23
Kamalnadh
Wow. This helped me a lot. Thank you. Also, can I have the option of putting hyphens in the middle of the words that can be put together? I'm searching for shortcut for this from so long and I couldn't find anything anywhere. Like, I want to put hyphens in the middle of selected words because I keep getting them always in my transcripts. EX: "one-size-fits-all solution"; state-of-the-art machinery; I-don't-care attitude; and more like that. I'm trying to find a shortcut so that I can select the words that I want to put dashes in middle and then use the shortcut and that it can put hyphens in the middle of three or more words that I selected. Can you help me on this?
Thanks in advance.
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments