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.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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
 
Upvote 0
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!
 
Upvote 0
Here's another way:

=IF(LEN(TRIM(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",255)),255)))>3,REPLACE(A1,LEN(A1)-2,0,"-"),A1)
 
Upvote 0
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).
 
Upvote 0
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),"")
 
Upvote 0
here's another way

Code:
=IFERROR(MID(A1,1,FIND("-",A1,5)+3)&IFERROR("-"&RIGHT(A1,LEN(A1)-FIND("-",A1,5)-3),""),A1)
 
Upvote 0
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:
Upvote 0
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).
 
Upvote 0

Forum statistics

Threads
1,218,746
Messages
6,144,254
Members
450,532
Latest member
riccofk08

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
Back
Top