• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Inserting Multiple Commas

brightyoyo

New Member
Hi, I am looking to get this information

C3 C8-10 C19-20 C22 C41-42 C49 C58-59 C61-62 C76-77 C79

to look like this

C3,C8,C9,C10,C19,C20,C22,C41,C42,C49,C58,C59,C61,C62,C76,C77,C79

I have multiple cells will similiar data, but with different letters


Thank You
 
Brightyoyo


Please note I deleted your second posting of this question.


Narayan, posted an answer which is below:

[pre]
Code:
"Hi ,

If you use a helper column , then the formula from an earlier question can be used to get the left-most alpha
characters ; then you can use a formula such as the following :

=SUBSTITUTE(SUBSTITUTE(A2,"-",",C")," ",",")

What this will do is replace the hyphens with the character "C" ; instead of a fixed character C , use the
left-most alpha character(s) here ; the outer SUBSTITUTE replaces the blanks with commas.

Narayan "
[/pre]
 
Thanks Narayan it works, but there is another issue. Some of the dashes span more than one number. Like J2-5, I need it to be J2,J3,J4,J5
 
Hi ,


I think that may require VBA , since it means the input string has to be parsed ; I am not sure that just formulae will be able to do this. Hope someone else can come up with one.


Narayan
 
Hi @brightyoyo,

Could you describe all of your requirements together? Describing your requirements in pieces in your previous post caused us to provide solutions that were incomplete.


Besides having data such as J2-5 that needs to be expanded, what other requirements do you have in your data?


Please consider copying/pasting some of that sample data.


Cheers,

Sajan.
 
This is a sample of my data. I need it to have commas in between the letter number combo and either the missing numbers in inserted for the dashes or if there are no additional numbers than a comma.

C46 C103

J6

J1

CR1-3 CR7 CR9 CR12 CR32

CR4-5 CR8 CR11 CR14

CR31

CR28

CR13

CR15

CR36

CR38

CR33-34

CR16-17

CR35

CR21 CR24 CR27 CR37 CR40-41

CR6 CR10 CR30

CR25

CR19

E1-2 E7-8

J2-5

F1-3

U1

U23
 
Hi brightyoyo,


Try:


Code:
=SUBSTITUTE(A1," ",",")


Resulting output:


C46,C103

J6

J1

CR1-3,CR7,CR9,CR12,CR32

CR4-5,CR8,CR11,CR14

CR31

CR28

CR13

CR15

CR36

CR38

CR33-34

CR16-17

CR35

CR21,CR24,CR27,CR37,CR40-41

CR6,CR10,CR30

CR25

CR19

E1-2,E7-8

J2-5

F1-3

U1

U23


Regards,
 
That is good, but I need to get rid of the dashes with commas or get rid of the dash and fill in the missing numbers.

E1-2,E7-8 should be E1,E2,E7,E8

J2-5 should be J2,J3,J4,J5


The dashes are showing sequential numbers.
 
Hi brightyoyo,


I think then Narayan was right in his opinion, you need a macro for that.


Regards,

Faseeh
 
Here's one way. Not elegant but should work.


Paste this code in a module and then run the code. It assumes that the values are in column A starting from Row 1.

[pre]
Code:
Option Explicit
Public Sub SplitContent()
Dim r As Range
Dim vA As Variant
Dim strValue As String
Dim i As Long

'Adjust Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row) to suit your requirements
'Currently it loops through column A row 1 to last row
For Each r In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)

'Case where there are one or multiple values but they don't have dashes
If InStr(Trim(r.Value), "-") = 0 Then
r.Offset(0, 1).Value = CStr(Join(Split(r.Value, " "), ", "))

Else

'Case where there are one or multiple values that have dashes
vA = Split(r.Value, " ")
strValue = vbNullString

For i = LBound(vA) To UBound(vA)
If InStr(vA(i), "-") = 0 Then
strValue = strValue & " " & vA(i)
Else
strValue = strValue & " " & strReturn(vA(i))
End If
Next i

r.Offset(0, 1).Value = Replace(Trim(strValue), " ", ", ")

End If

Next r

End Sub
Private Function strReturn(strInput As Variant) As String
Dim strPref As String
Dim i As Long

'Find out prefix that we need to repeat
strPref = vbNullString
For i = 1 To Len(strInput)
If Not IsNumeric(Mid(strInput, i, 1)) Then
strPref = strPref & Mid(strInput, i, 1)
Else
Exit For
End If
Next i

strInput = Replace(Replace(strInput, strPref, ""), "-", ":")
strReturn = strPref & Join(Application.Transpose(Evaluate("ROW(" & strInput & ")")), " " & strPref)

End Function
[/pre]
 
Hi, brightyoyo!


Wanna a long and boring VBA solution? If so, give a look at this file:

https://dl.dropboxusercontent.com/u/60558749/Inserting%20Multiple%20Commas%20%28for%20brightyoyo%20at%20chandoo.org%29.xlsm


Asumming OriginalList and ConvertedList are two named ranges with seldom the original and converted values, this code does the job:

-----

[pre]
Code:
Option Explicit

Sub ConvertExpandingNormalizing()
' constants
Const ksWSOriginal = "Hoja1"
Const ksOriginal = "OriginalList"
Const ksWSConverted = "Hoja1"
Const ksConverted = "ConvertedList"
Const ksDash = "-"
Const ksComma = ","
Const ksDigits = "0123456789"
' declarations
Dim rngO As Range, rngC As Range
Dim sOriginal As String, sConverted As String
Dim sPrefix As String, lFrom As Long, lTo As Long
Dim I As Long, J As Long, K As Long, L As Long, M As Long
Dim A As String, B As String, C As String
' start
Set rngO = Worksheets(ksWSOriginal).Range(ksOriginal)
Set rngC = Worksheets(ksWSConverted).Range(ksConverted)
With rngC
Range(.Rows(1), .Rows(.Rows.Count)).ClearContents
End With
' process
With rngO
For I = 1 To .Rows.Count
' input
sOriginal = .Cells(I, 1).Value & Space(1)
sConverted = ""
sPrefix = ""
lFrom = 0
lTo = 0
J = 0
Do
' chunk
K = InStr(J + 1, sOriginal, Space(1))
A = Mid(sOriginal, J + 1, K - J - 1)
L = InStr(A, ksDash)
' 1 or 2 values?
If L = 0 Then B = A Else B = Left(A, L - 1)
' 1st value
For M = 1 To Len(B)
C = Mid(B, M, 1)
If InStr(ksDigits, C) > 0 Then Exit For
Next M
If M <= Len(B) Then
sPrefix = Left(B, M - 1)
lFrom = Val(Right(B, Len(B) - M + 1))
Else
MsgBox "Value off format at row " & I & " : " & _
.Cells(I, 1).Value, _
vbApplicationModal + vbOKOnly, "Warning"
End If
' 2nd value?
If L = 0 Then lTo = lFrom Else lTo = Val(Right(A, Len(A) - L))
' build
For L = lFrom To lTo
If sConverted <> "" Then sConverted = sConverted & ksComma
sConverted = sConverted & sPrefix & L
Next L
' cycle
J = K
Loop Until J = Len(sOriginal)
' output
rngC.Cells(I, 1).Value = Trim(sConverted)
Next I
End With
' end
Worksheets(ksWSConverted).Activate
rngC.Cells(1, 1).Select
Set rngC = Nothing
Set rngO = Nothing
Beep
End Sub
[/pre]
-----


Just advise if any issue.


Regards!
 
Hi, brightyoyo!

Glad you solved it whichever suggestion you've tried. Thanks for your feedback and welcome back whenever needed or wanted.

Regards!
 
Back
Top