How to count if dash in string has n spaces?

Magnatolia

Board Regular
Joined
Jan 19, 2012
Messages
79
Hi everyone,

Is it possible to count the number of dashes in a string that do not have space on both or just one side? The string length will be inconsistent so cannot use number of words. e.g. "This-document" would be counted, whereas "This - Document" would not.

Thanks!
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,615
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Is this what you want?
<b></b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>A</th><th>B</th><th>C</th><th>D</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">1</td><td style="color: #333333;;">This-document</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;">1</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style="color: #333333;;">This - Document</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;">0</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style="color: #333333;;">This--document</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;">2</td></tr></tbody></table><p style="width:4.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet1</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: rgb(255,255,255)" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: rgb(255,255,255);border-collapse: collapse; border-color: rgb(187,187,187)"><thead><tr style=" background-color: rgb(218,231,245);color: rgb(22,17,32)"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">D1</th><td style="text-align:left">=LEN(<font color="Blue">A1</font>)-LEN(<font color="Blue">IF(<font color="Red">ISERROR(<font color="Green">SEARCH(<font color="Purple">" - ",A1</font>)</font>),SUBSTITUTE(<font color="Green">A1,"-",""</font>),A1</font>)</font>)</td></tr><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">D2</th><td style="text-align:left">=LEN(<font color="Blue">A2</font>)-LEN(<font color="Blue">IF(<font color="Red">ISERROR(<font color="Green">SEARCH(<font color="Purple">" - ",A2</font>)</font>),SUBSTITUTE(<font color="Green">A2,"-",""</font>),A2</font>)</font>)</td></tr><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">D3</th><td style="text-align:left">=LEN(<font color="Blue">A3</font>)-LEN(<font color="Blue">IF(<font color="Red">ISERROR(<font color="Green">SEARCH(<font color="Purple">" - ",A3</font>)</font>),SUBSTITUTE(<font color="Green">A3,"-",""</font>),A3</font>)</font>)</td></tr></tbody></table></td></tr></table><br />
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,313
Office Version
  1. 2010
Platform
  1. Windows
Is this what you want?
ABCD
1This-document1
2This - Document0
3This--document2

<colgroup><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
D1=LEN(A1)-LEN(IF(ISERROR(SEARCH(" - ",A1)),SUBSTITUTE(A1,"-",""),A1))
D2=LEN(A2)-LEN(IF(ISERROR(SEARCH(" - ",A2)),SUBSTITUTE(A2,"-",""),A2))
D3=LEN(A3)-LEN(IF(ISERROR(SEARCH(" - ",A3)),SUBSTITUTE(A3,"-",""),A3))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Scott, your formulas do not take into account the red highlighted part below of the OP's question...

"....in a string that do not have space on both or just one side?"
 
Last edited:

Magnatolia

Board Regular
Joined
Jan 19, 2012
Messages
79
Is this what you want?
ABCD
1This-document1
2This - Document0
3This--document2

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1

Worksheet Formulas
CellFormula
D1=LEN(A1)-LEN(IF(ISERROR(SEARCH(" - ",A1)),SUBSTITUTE(A1,"-",""),A1))
D2=LEN(A2)-LEN(IF(ISERROR(SEARCH(" - ",A2)),SUBSTITUTE(A2,"-",""),A2))
D3=LEN(A3)-LEN(IF(ISERROR(SEARCH(" - ",A3)),SUBSTITUTE(A3,"-",""),A3))

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Thanks! This is awesome, however I think this only works if there is one dash. What would I need to change if I wanted to check any dash in the string? e.g. TEAM - DOCUMENT TYPE -TITLE

Thanks!
 

Akuini

Well-known Member
Joined
Feb 1, 2016
Messages
2,575
Office Version
  1. 365
Platform
  1. Windows
Maybe this:
<b></b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>A</th><th>B</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">1</td><td style=";">A-B -CC- D-E - F</td><td style="text-align: right;;">2</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style=";">A-B -C-C- D-E - F</td><td style="text-align: right;;">3</td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style=";">asd</td><td style="text-align: right;;">0</td></tr></tbody></table><p style="width:4,8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet4</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: rgb(255,255,255)" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: rgb(255,255,255);border-collapse: collapse; border-color: rgb(187,187,187)"><thead><tr style=" background-color: rgb(218,231,245);color: rgb(22,17,32)"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">B1</th><td style="text-align:left">=LEN(<font color="Blue">SUBSTITUTE(<font color="Red">SUBSTITUTE(<font color="Green">A1,"- ",""</font>)," -",""</font>)</font>)-LEN(<font color="Blue">SUBSTITUTE(<font color="Red">SUBSTITUTE(<font color="Green">SUBSTITUTE(<font color="Purple">A1,"- ",""</font>)," -",""</font>),"-",""</font>)</font>)</td></tr><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">B2</th><td style="text-align:left">=LEN(<font color="Blue">SUBSTITUTE(<font color="Red">SUBSTITUTE(<font color="Green">A2,"- ",""</font>)," -",""</font>)</font>)-LEN(<font color="Blue">SUBSTITUTE(<font color="Red">SUBSTITUTE(<font color="Green">SUBSTITUTE(<font color="Purple">A2,"- ",""</font>)," -",""</font>),"-",""</font>)</font>)</td></tr><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">B3</th><td style="text-align:left">=LEN(<font color="Blue">SUBSTITUTE(<font color="Red">SUBSTITUTE(<font color="Green">A3,"- ",""</font>)," -",""</font>)</font>)-LEN(<font color="Blue">SUBSTITUTE(<font color="Red">SUBSTITUTE(<font color="Green">SUBSTITUTE(<font color="Purple">A3,"- ",""</font>)," -",""</font>),"-",""</font>)</font>)</td></tr></tbody></table></td></tr></table><br />
 

Watch MrExcel Video

Forum statistics

Threads
1,109,028
Messages
5,526,335
Members
409,696
Latest member
EERS

This Week's Hot Topics

Top