Add specific character

sohamkhatri

New Member
Joined
Mar 20, 2023
Messages
1
Office Version
  1. 2013
Platform
  1. Windows
Hello Team,

I want a help to find a excel formula for my problem.

I have a data something like below-

ABCDEF
ABSFGHFDS
DGUVHINGYIHH
FTYJJJ
FTYBJKFGY


I have a data like this where I have some some words which have letter multiple of 3

So I want to convert this data something like this

ABC-DEF
ABS-FGH-FDS
DGU-VHI-NGY-IHH
FTY-JJJ
FTY-BJK-FGY


I want that after every third letter or character "-" will be added between them.


Please help me in it.

Thank you
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Here's one idea:

Code:
Function ReGroup(str As String)
  Dim str2 As String
 
  str2 = ""
  While Len(str) >= 3
  str2 = str2 & Left(str, 3) & "-"
  str = Right(str, Len(str) - 3)
  Wend
   ReGroup = Left(str2, Len(str2) - 1)
 
End Function

Use it in a cell like B2, where your original string is in A2: =ReGroup(A2)
 
Last edited:
Upvote 0
This was my VBA approach. I know that the OP is on XL2013, but there is a formula solution as well.

SOHA
GHI
8OriginalDynamic ArrayVBA
9ABCDEFABC-DEFABC-DEF
10ABSFGHFDSABS-FGH-FDSABS-FGH-FDS
11DGUVHINGYIHHDGU-VHI-NGY-IHHDGU-VHI-NGY-IHH
12FTYJJJFTY-JJJFTY-JJJ
13FTYBJKFGYZFTY-BJK-FGY-ZFTY-BJK-FGY-Z
Data
Cell Formulas
RangeFormula
H9:H13H9=HYPHENATE(G9:G13,3)
I9:I13I9=htt(G9)
Dynamic array formulas.



VBA Code:
Function HTT(s As String)
With Application.WorksheetFunction
    Dim n As Integer:   n = .RoundUp(Len(s) / 3, 0) - 1
    
    For i = 1 To n
        s = Application.WorksheetFunction.Replace(s, i * 4, 0, "-")
    Next i
End With
    
HTT = s
End Function

HYPHENATE
Excel Formula:
=LAMBDA(
    text,chunk_size,
    MAP(text,
        LAMBDA(t,
            TEXTJOIN("-",,MID(t,SEQUENCE(ROUNDUP(LEN(t)/chunk_size,0),,,chunk_size),chunk_size))
        )
    )
)
 
Upvote 0
Another more flexible function might be:

Code:
Function InsertDash(s As String, intv As Long)
    If intv < 1 Then Exit Function

    Dim i As Long, result As String

    For i = 1 To Len(s) Step intv
        On Error Resume Next
        r = r & Left(s, intv) & "-"
        s = Mid(s, intv + 1, Len(s) - intv)
    Next i

    InsertDash = Left(r, Len(r) - 1)
End Function

Used as: =InsertDash(A2,3) for every 3 characters
 
Upvote 0
An option with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    out = Table.TransformColumns(Source,{{"Column1", each  
            List.Accumulate({1..Text.Length(_)/3 -1}, "", (s,c)=> 
                if s = "" then Text.Insert(_, c*3, "-") else Text.Insert(s, c*3 + (c-1), "-")), type text}})
in
    out


Book1
ABC
1Column1Column1
2ABCDEFABC-DEF
3ABSFGHFDSABS-FGH-FDS
4DGUVHINGYIHHDGU-VHI-NGY-IHH
5FTYJJJFTY-JJJ
6FTYBJKFGYFTY-BJK-FGY
Sheet1
 
Upvote 0
And here a Regex function

VBA Code:
Function jec(cell As String) As String
 With CreateObject("vbscript.regexp")
   .Global = True
   .Pattern = "([A-Z]{3}(?!$))+?"
   jec = .Replace(cell, "$1-")
 End With
End Function
 
Upvote 0
Another recursive option.

VBA Code:
Function XH(s As String, Optional res As String)
Dim n As Integer:   n = Len(s)
res = res & IIf(n <= 3, s, Left(s, 3) & "-")
If n > 2 Then XH Right(s, n - 3), res
XH = res
End Function
 
  • Like
Reactions: JEC
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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