formula to count number of characters after a specified character

buddigars

New Member
Joined
Sep 19, 2014
Messages
1
COLUMN A
COUMN B
TX-ABC-12345
TX-ABC-123-45
TX-AB-135690
TX-AB-135-690
PP-BC-1234
PP-BC-123-4
AB-DEF-01
AB-DEF-01
AB-DEF
AB-DE-F

<tbody>
</tbody>
hi forum members,

I have a data as above in column A. I want the target data as column B.
My requirement is to count the number of characters after last hyphen. If the characters are >=4 then add a hyphen after 3 characters and display the total string.

please help me with a formula.
 

Some videos you may like

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

Pup Denab

Active Member
Joined
May 12, 2013
Messages
299
HI Buddigars
Welcome to the forum

=IF(RIGHT(IFERROR(IF(LEN(A2)-FIND("-",A2,4)>=4,LEFT(A2,FIND("-",A2,4))&MID(A2,FIND("-",A2,4)+1,3),A2)&"-"&MID(A2,LEN(LEFT(A2,FIND("-",A2,4))&MID(A2,FIND("-",A2,4)+1,3))+1,99),A2),1)="-",IFERROR(IF(LEN(A2)-FIND("-",A2,4)>=4,LEFT(A2,FIND("-",A2,4))&MID(A2,FIND("-",A2,4)+1,3),A2)&MID(A2,LEN(LEFT(A2,FIND("-",A2,4))&MID(A2,FIND("-",A2,4)+1,3))+1,99),A2),IFERROR(IF(LEN(A2)-FIND("-",A2,4)>=4,LEFT(A2,FIND("-",A2,4))&MID(A2,FIND("-",A2,4)+1,3),A2)&"-"&MID(A2,LEN(LEFT(A2,FIND("-",A2,4))&MID(A2,FIND("-",A2,4)+1,3))+1,99),A2))

This formula in B2 will do as you ask, however your AB-DEF to AB-DE-F is wrong by you criteria, I have corrected as you requested it should be AB-DEF AB-DEF
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,406
As Pup has already pointed out, it looks like the example for your last text string is not correct. If so, here's another way...

=IF(LEN(MID(A1,FIND("^^",SUBSTITUTE(A1,"-","^^",LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))+1,LEN(A1)))>=4,REPLACE(A1,FIND("^^",SUBSTITUTE(A1,"-","^^",LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))))+4,0,"-"),A1)

Hope this helps!
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Here's another way:

=IF(LEN(TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",255)),255)))>3,REPLACE(A1,LEN(A1)-2,0,"-"),A1)
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,681
Office Version
  1. 2010
Platform
  1. Windows

ADVERTISEMENT

Here's another way:

=IF(LEN(TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",255)),255)))>3,REPLACE(A1,LEN(A1)-2,0,"-"),A1)
I don't think that works correctly Scott (check what your formula returns for the first and third ones compared to what the OP said was wanted).
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,681
Office Version
  1. 2010
Platform
  1. Windows
Here is another formula that appears to work correctly...

=LEFT(A1,FIND("-",A1&"-",FIND("-",A1)+1)+3)&IF(LEN(A1)>FIND("-",A1&"-",FIND("-",A1)+1)+3,"-"&MID(A1,FIND("-",A1&"-",FIND("-",A1)+1)+4,9),"")
 

ttdk1

Board Regular
Joined
May 21, 2014
Messages
189

ADVERTISEMENT

here's another way

Code:
=IFERROR(MID(A1,1,FIND("-",A1,5)+3)&IFERROR("-"&RIGHT(A1,LEN(A1)-FIND("-",A1,5)-3),""),A1)
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,681
Office Version
  1. 2010
Platform
  1. Windows
here's another way

Code:
=IFERROR(MID(A1,1,FIND("-",A1,5)+3)&IFERROR("-"&RIGHT(A1,LEN(A1)-FIND("-",A1,5)-3),""),A1)
Here is where more information about what the OP's data looks like would have been helpful... your formula will fail if the character count before the second dash is 4-3, 3-4 or larger.

EDIT NOTE: My concern turns out to be the case if you check out the cross-posted link Barry posted. As a matter of fact, the dash count is not even consistent!
 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,681
Office Version
  1. 2010
Platform
  1. Windows
Cross-posted here
@Barry,

Sigh! Why do people keep doing this to us.


@buddigars,

Thank you so much for wasting our time here by, first, not telling us you cross-posted your question (so we could not check out other submissions before developing our own) and, second, for not using the same example data (you posted examples with more dashes and longer text sections than you did here).
 

Watch MrExcel Video

Forum statistics

Threads
1,122,566
Messages
5,596,898
Members
414,109
Latest member
Coops01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top