You are here:

Excel/Excel/Expand a hyphenated series into a comma-separated series

Advertisement


Question
QUESTION: Tom;

In your answer to somebody else, you had this example:
Sub Master()
Dim r As Range, cell As Range
Set r = Selection
For Each cell In Selection
  If Len(Trim(cell)) > 0 Then
  cell.Select
  ABC
  End If
Next
End Sub

Sub ABC()
Dim s As String, s1 As String, s2 As String
Dim s3 As String, s4 As String, v As Variant, v1 As Variant
Dim i As Long, j As Long, k As Long, ub As Long, lb As Long
Dim sp As String, cnt As Long, s6 As String
Dim s5 As String, base As String
s5 = ""
s = Replace(ActiveCell.Value, " ,", ",")
If Len(Trim(ActiveCell)) = 0 Then Exit Sub
v = Split(s, ",")
j = 1
For i = LBound(v) To UBound(v)
 s1 = v(i)
 If InStr(1, s1, "-", vbTextCompare) = 0 Then
    s5 = s5 & s1 & ","
    j = j + 1
 Else
    v1 = Split(s1, "-")
    s1 = v1(LBound(v1))
    s2 = v1(UBound(v1))
    s4 = ""
    For k = Len(s1) To 1 Step -1
        s3 = Mid(s1, k, 1)
        If IsNumeric(s3) Then
         s4 = s3 & s4
        End If
    Next k
    lb = CInt(s4)
    s4 = ""
    For k = Len(s2) To 1 Step -1
        s3 = Mid(s2, k, 1)
        If IsNumeric(s3) Then
         s4 = s3 & s4
        End If
    Next k
    ub = CInt(s4)
    base = Replace(s2, s4, "")
    cnt = Len(s2) - Len(base)
    sp = Replace(Space(cnt), " ", "0")
    For k = lb To ub
        s6 = Format(k, sp)
        Do While Left(s6, 1) = "0"
         s6 = Right(s6, Len(s6) - 1)
        Loop
        s5 = s5 & base & s6 & ","
        j = j + 1
    Next k
 End If
Next i
If InStr(1, s5, ",", vbTextCompare) > 0 Then
 s5 = Left(s5, Len(s5) - 1)
 s5 = Replace(s5, ",", ", ")
End If
ActiveCell = Application.Trim(s5)
End Sub

---------------
But when I try this data,
D1,D2-7
I get back this
D1, 2, 3, 4, 5, 6, 7

and if I have this;
D1 D2-7
It gives me back a blank line.

This would be a typical data pattern for me:
C3 C5 C7-17 C19-21 C23-24 C26 C28-36 C38-43 C45-47 C50 C52-55

Thanks, Jeff

ANSWER: Jeff,

Select cells in a single column and run this macro.  It will place the results in the cell to the right of each selected cell.

for you test string
C3 C5 C7-17 C19-21 C23-24 C26 C28-36 C38-43 C45-47 C50 C52-55

it produced:
C3,C5,C7,C8,C9,C10,C11,C12,C13,C14,C15,C16,C17,C19,C20,C21,C23,C24,C26,C28,C29,C30,C31,C32,C33,C34,C35,C36,C38,C39,C40,C41,C42,C43,C45,C46,C47,C50,C52,C53,C54,C55

It assumes the format will include a single leading character as you show.

Test it on a copy of your data.


Sub abc()
Dim r As Range, cell As Range, sh1 As Worksheet
Dim s As String, v As Variant, i As Long
Dim rw As Long, s1 As Variant
Set r = Worksheets("POM15-19").Range("D3:D269")
Worksheets.Add After:=Worksheets(Worksheets.Count)
Set sh1 = ActiveSheet

rw = 2
For Each cell In r
 s = cell.Offset(0, 1)
 If Len(Trim(s)) > 0 Then
  s = Replace(s, " ", "")
  v = Split(s, ",")
 
   For i = LBound(v) To UBound(v)
    If Len(v(i)) > 6 Then
      If Left(v(i), 3) <> "TOM" Then
      s1 = " " & Mid(v(i), 7, 255)
      v(i) = Left(v(i), 6)
      Else
        s1 = ""
      End If
    Else
      s1 = ""
    End If
    sh1.Cells(rw, 1) = cell.Row
    sh1.Cells(rw, 2) = cell & s1
    sh1.Cells(rw, 3) = v(i)
    rw = rw + 1
   Next
 End If
Next

End Sub

--
Regards,
Tom Ogilvy


---------- FOLLOW-UP ----------

QUESTION: Tom;

Thank you for the prompt reply! When I run the macro, I get an error on this line

Set r = Worksheets("POM15-19").Range("D3:D269")

Runtime error '9'
Subscript out of range...

Please let me know what I am doing wrong?

Thanks, Jeff

Answer
Jeff,

Sorry - I pasted in the wrong macro.  I had a bunch of workbooks open at the time.  Here is the correct macro:

Sub abc()
Dim lb As Long, ub As Long, i As Long, k As Long
Dim cell As Range, s As String, s1 As String, ss As String
Dim ldr As String, tail As String, v As Variant, v1 As Variant
For Each cell In Selection
 s = cell.Text
 ss = ""
 If Len(Trim(cell)) > 0 Then
    v = Split(s, " ")
    For i = LBound(v) To UBound(v)
      s1 = Trim(v(i))
      If Len(s1) > 0 Then
        If InStr(1, s1, "-", vbTextCompare) Then
         ldr = Left(s1, 1)
         tail = Right(s1, Len(s1) - 1)
         v1 = Split(tail, "-")
         ub = v1(UBound(v1))
         lb = v1(LBound(v1))
         For k = lb To ub
         ss = ss & ldr & k & ","
         Next k
        Else
         ss = ss & s1 & ","
        End If
      End If
    Next i
   cell.Offset(0, 1).Value = Left(ss, Len(ss) - 1)
 End If
Next cell
End Sub

Select a range of cells in a single column an run the macro.  The results will be placed in the cell to the right of each cell selected.
Test it on a copy of your data.  Again, sorry for the confusion.
--
Regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.