**Please Note: **
This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. If you are using an earlier version (Excel 2003 or earlier), *this tip may not work for you*. For a version of this tip written specifically for earlier versions of Excel, click here: Adding Dashes between Letters.

Scott wonders how he can make Excel automatically add a dash between every letter in a given cell. As an example, if cell A1 contains "house", Scott would like to convert it to "h-o-u-s-e".

This can be done with a formula, but it quickly becomes unwieldy. For instance, the following formula can be used to put dashes between the letters of whatever you type into cell A1:

=CHOOSE(LEN(A1),A1,LEFT(A1,1) & "-" & RIGHT(A1,1), LEFT(A1,1) & "-" & MID(A1,2,1) & "-" & RIGHT(A1,1), LEFT(A1,1) & "-" & MID(A1,2,1) & "-" & MID(A1,3,1) & "-" & RIGHT(A1,1),LEFT(A1,1) & "-" & MID(A1,2,1) & "-" & MID(A1,3,1) & "-" & MID(A1,4,1) & "-" & RIGHT(A1,1), LEFT(A1,1) & "-" & MID(A1,2,1) & "-" & MID(A1,3,1) & "-" & MID(A1,4,1) & "-" & MID(A1,5,1) & "-" & RIGHT(A1,1))

This particular example of a formula will only work on text up to six characters in length. Thus, it would work properly for "house", but not for "household". The formula could be lengthened but, again, it would quickly become very long.

A better approach is to use a macro to do the conversion. If you want to insert the dashes right into the cell, you could use a macro such as this:

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

This macro is designed to be used on a selected range of cells. Just select the cells you want to convert, and then run the macro. The dashes are added between each letter in the cells.

If you prefer to not modify the original cell values, you could create a user-defined function that would do the job:

Function AddDashes2(Src As String) As String Dim sTemp As String Dim C As Integer Application.Volatile sTemp = "" For C = 1 To Len(Src) sTemp = sTemp & Mid(Src, C, 1) & "-" Next AddDashes2 = Left(sTemp, Len(sTemp) - 1) End Function

To use this function, you would use the following in your worksheet:

=AddDashes2(A1)

If you want to make sure that the function is a bit more robust, you could modify it so that it handles multiple words. In such an instance you would not want it to treat a space as a "dashable letter." For example, you would want the routine to add dashes to "one two" so it came out as "o-n-e t-w-o" instead of "o-n-e- -t-w-o". The following variation on the function will do the trick:

Function AddDashes3(Src As String) As String Dim sTemp As String Dim C As Integer Application.Volatile sTemp = "" For C = 1 To Len(Src) sTemp = sTemp & Mid(Src, C, 1) If Mid(Src, C, 1) <> " " And Mid(Src, C + 1, 1) <> " " And C < Len(Src) Then sTemp = sTemp & "-" End If Next AddDashes3 = sTemp End Function

*Note:*

If you would like to know how to use the macros described on this page (or on any other page on the *ExcelTips* sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

*ExcelTips* is your source for cost-effective Microsoft Excel training.
This tip (9634) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: **Adding Dashes between Letters**.

**Excel Smarts for Beginners!** Featuring the friendly and trusted *For Dummies* style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out *Excel 2013 For Dummies* today!

When processing data in a worksheet, you may have a need to know what the smallest (lowest) even value in a range is. You ...

Discover MoreAdding row numbers to a column of your worksheet is easy; you just need to use a formula to do it. Here's a quick look at ...

Discover MoreSometimes it can be confusing to figure out the source of an error that is displayed in your worksheet. Excel provides a ...

Discover More**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

2019-11-25 13:47:20

JMJ

Aren't you an APL programmer :-) ?

The APL programmer's theorem states that "for each working program written in APL, there is a shorter one with exactly the same functionalities"

Of course, it's a joke (albeit...), but I agree with you, and quoting A. Pope : "There is a certain majesty in simplicity which is far above all the quaintness of wit" !

2019-11-20 13:52:55

Willy Vanhaelen

Correection

Result: m y t i t l e

2019-11-20 13:51:34

Willy Vanhaelen

Usage: =SeparateText("my title"," ")

Result: m y t i t l e

2019-11-20 12:49:19

Willy Vanhaelen

Excellent idea. If you use a space as separator you can even use this macro as a title widener with the advantage that between words you get 3 spaces wich is more clear.

2019-11-19 18:25:41

I do like your short function. I woulf add a variable for the separator and this would address JC's concerns about maintaining it.

Function SeparateText(ByVal ref, Separator As String) As String

SeparateText = Replace(StrConv(ref, vbUnicode), Chr$(0), Separator, 1, Len(ref) - 1)

End Function

=SeparateText(A1,"-")

=SeparateText(A1," ")

2019-11-19 14:11:52

Willy Vanhaelen

For me it feels like simpler because it's shorter and more concise. But you are right, Allen's code is the traditional way to do it and easier to understand. In a way it is the "teacher's" approach.

But it is always interesting to show that a problem can mostly be solved in several ways. And generally, with Leonardo da Vinci's saying "Simplicity is the ultimate sophistication" in mind, I prefer the shorter and more concise ones :-)

2019-11-18 09:15:59

JC

2019-11-17 10:42:16

Willy Vanhaelen

Function AddDashes2(Src As String) As String

AddDashes2 = Replace(StrConv(Src, vbUnicode), Chr$(0), "-", 1, Len(Src) - 1)

End Function

Here is how it works, the variable Src (cell referred to) is TEST as an example:

StrConv(Src, vbUnicode) -> StrConv("TEST", vbUnicode)

Converts TEST to unicode so each letter takes 2 bytes instead of one.

The first byte is the ascii code of the letter, the second byte is Null or Chr$(0).

The result in the example is T_E_S_T_ (the underscores represent null or Chr$(0)

This last character is used as find argument for the Replace function:

Replace(StrConv(Src, vbUnicode), Chr$(0), "-", 1, Len(Src) - 1)

-> Replace("T_E_S_T_", Chr$(0), "-", 1, Len(Src) - 1)

The count argument in Replace is Len(Src) - 1 to eliminate the unwanted last dash.

Finaly T-E-S-T is returned as result.

This tip's first macro can also be simplified a lot:

Sub AddDashes1()

Dim cell As Range

For Each cell In Selection

cell = Replace(StrConv(cell, vbUnicode), Chr$(0), "-", 1, Len(cell) - 1)

Next cell

End Sub

2019-11-16 13:09:40

Willy Vanhaelen

Function AddDashes2(S As String) As String

AddDashes2=Left(Join(Split(StrConv(S,vbUnicode),Chr$(0)),"-"),Len(S)*2-1)

End Function

Got a version of Excel that uses the
ribbon interface (Excel 2007 or later)?
**This site is for you!** If you
use an earlier version of Excel, visit
our *ExcelTips* site focusing on the menu interface.

**FREE SERVICE:** Get tips like this every week in *ExcelTips,* a free productivity newsletter. Enter your address and click "Subscribe."

Copyright © 2020 Sharon Parq Associates, Inc.

## Comments