Copy row of cells X times based on number in a column

tominabox1

New Member
Joined
Oct 24, 2014
Messages
6
I am completely new to VB so bear with me please! I sometimes have a spreadsheet with data like this:

QTY Ref Des
1 U5
5 C1,4,8-10
3 R10,23,48
1 L2

And I need to expand those "combined" lists so each "ref des" has its own row like this:

QTY Ref Des
1 U5
1 C1
1 C4
1 C8
... and so on.

There is additional data in the spreadsheet rows, these are just the 2 columns that need to be modified during the operation, the rest can just be duplicated/copied.

Any ideas? Thanks
 
I know there is a lot of code, but I believe it will do what you want. Copy/paste the following into a module and run only the ExpandRefDes macro (it will call the other code as it needs to)...
Code:
Sub ExpandRefDes()
  Dim Cell As Range
  For Each Cell In Range("H2:H" & Cells(Rows.Count, "H").End(xlUp).Row)
    Cell.Value = ExpandedSeries(Replace(Cell.Value, ",", "," & Left(Cell.Value, 1)), ",")
  Next
  RedistributeData
  Range("J2:J" & Cells(Rows.Count, "J").End(xlUp).Row).Value = 1
End Sub

Function ExpandedSeries(ByVal S As String, Optional Delimiter As String = ", ") As Variant
  Dim X As Long, Y As Long, Z As Long
  Dim Letter As String, Numbers() As String, Parts() As String
  S = Chr$(1) & Replace(Replace(Replace(Replace(Application.Trim(Replace(S, ",", _
      " ")), " -", "-"), "- ", "-"), " ", " " & Chr$(1)), "-", "-" & Chr$(1))
  Parts = Split(S)
  For X = 0 To UBound(Parts)
    If Parts(X) Like "*-*" Then
      For Z = 1 To InStr(Parts(X), "-") - 1
        If IsNumeric(Mid(Parts(X), Z, 1)) And Mid$(Parts(X), Z, 1) <> "0" Then
          Letter = Left(Parts(X), Z + (Left(Parts(X), 1) Like "[A-Za-z" & Chr$(1) & "]"))
          Exit For
        End If
      Next
      Numbers = Split(Replace(Parts(X), Letter, ""), "-")
      If Not Numbers(1) Like "*[!0-9" & Chr$(1) & "]*" Then Numbers(1) = Val(Replace(Numbers(1), Chr$(1), "0"))
      On Error GoTo SomethingIsNotRight
      For Z = Numbers(0) To Numbers(1) Step Sgn(-(CDbl(Numbers(1)) > CDbl(Numbers(0))) - 0.5)
        ExpandedSeries = ExpandedSeries & Delimiter & Letter & Z
      Next
    Else
      ExpandedSeries = ExpandedSeries & Delimiter & Parts(X)
    End If
  Next
  ExpandedSeries = Replace(Mid(ExpandedSeries, Len(Delimiter) + 1), Chr$(1), "")
  Exit Function
SomethingIsNotRight:
  ExpandedSeries = CVErr(xlErrValue)
End Function

Sub RedistributeData()
  Dim X As Long, LastRow As Long, A As Range, Table As Range, Data() As String
  Const Delimiter As String = ","
  Const DelimitedColumn As String = "H"
  Const TableColumns As String = "A:O"
  Const StartRow As Long = 2
  Application.ScreenUpdating = False
  LastRow = Columns(TableColumns).Find(What:="*", SearchOrder:=xlRows, _
            SearchDirection:=xlPrevious, LookIn:=xlFormulas).Row
  For X = LastRow To StartRow Step -1
    Data = Split(Cells(X, DelimitedColumn), Delimiter)
    If UBound(Data) > 0 Then
      Intersect(Rows(X + 1), Columns(TableColumns)).Resize(UBound(Data)).Insert xlShiftDown
    End If
    If Len(Cells(X, DelimitedColumn)) Then
      Cells(X, DelimitedColumn).Resize(UBound(Data) + 1) = WorksheetFunction.Transpose(Data)
    End If
  Next
  LastRow = Cells(Rows.Count, DelimitedColumn).End(xlUp).Row
  On Error Resume Next
  Set Table = Intersect(Columns(TableColumns), Rows(StartRow).Resize(LastRow - StartRow + 1))
  If Err.Number = 0 Then
    Table.SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    Columns(DelimitedColumn).SpecialCells(xlFormulas).Clear
    Table.Value = Table.Value
  End If
  On Error GoTo 0
  Application.ScreenUpdating = True
End Sub

Note: The last two routines above were taken (the last one modified slightly for your layout) from my mini-blog here...

Generalized Series Expansions (e.g. AB5-AB9 becomes AB5, AB6, AB7, AB8, AB9)

Redistribute a Delimited Column Of Data into Separate Rows (Keeping Other Data As Is)
 
Last edited:
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Wow, awesome!!!!!!! Thank you Rick and others, This makes my job soooo much easier.

THANK YOU THANK YOU THANK YOU!
 
Upvote 0
I've just seen Rick's given you an answer, No matter use for reference as required !!!!
Try this:- Change sheet names where shown in code !!!
Code:
[COLOR=Navy]Sub[/COLOR] MG29Oct20
[COLOR=Navy]Dim[/COLOR] Ray [COLOR=Navy]As[/COLOR] Variant, Sp [COLOR=Navy]As[/COLOR] Variant, Ac [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] MyRay(), n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] nn [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range
[COLOR=Navy]Dim[/COLOR] Aph [COLOR=Navy]As[/COLOR] [COLOR=Navy]String,[/COLOR] c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]


With Sheets("Sheet1") '[COLOR=Green][B]Change to Data sheet Name[/B][/COLOR]
    [COLOR=Navy]Set[/COLOR] Rng = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
[COLOR=Navy]End[/COLOR] With
    [COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
        Aph = Left(Dn.Offset(, 7).Value, 1)
        Ray = Split(Mid(Dn.Offset(, 7).Value, 2), ",")
        [COLOR=Navy]For[/COLOR] n = 0 To UBound(Ray)
            [COLOR=Navy]If[/COLOR] InStr(Ray(n), "-") > 0 [COLOR=Navy]Then[/COLOR]
                Sp = Split(Ray(n), "-")
                [COLOR=Navy]For[/COLOR] nn = Val(Sp(LBound(Sp))) To Val(Sp(UBound(Sp)))
                    c = c + 1
                    ReDim Preserve MyRay(1 To 15, 1 To c)
                    [COLOR=Navy]For[/COLOR] Ac = 1 To 15
                        [COLOR=Navy]If[/COLOR] Ac = 10 [COLOR=Navy]Then[/COLOR]
                            MyRay(10, c) = 1
                        [COLOR=Navy]ElseIf[/COLOR] Ac = 8 [COLOR=Navy]Then[/COLOR]
                            MyRay(8, c) = Aph & nn
                        [COLOR=Navy]Else[/COLOR]
                            MyRay(Ac, c) = Dn(, Ac)
                        [COLOR=Navy]End[/COLOR] If
                   [COLOR=Navy]Next[/COLOR] Ac
                [COLOR=Navy]Next[/COLOR] nn
            [COLOR=Navy]Else[/COLOR]
                    c = c + 1
                    ReDim Preserve MyRay(1 To 15, 1 To c)
                      [COLOR=Navy]For[/COLOR] Ac = 1 To 15
                        [COLOR=Navy]If[/COLOR] Ac = 10 [COLOR=Navy]Then[/COLOR]
                            MyRay(10, c) = 1
                        [COLOR=Navy]ElseIf[/COLOR] Ac = 8 [COLOR=Navy]Then[/COLOR]
                            MyRay(8, c) = Aph & Ray(n)
                        [COLOR=Navy]Else[/COLOR]
                            MyRay(Ac, c) = Dn(, Ac)
                        [COLOR=Navy]End[/COLOR] If
                    [COLOR=Navy]Next[/COLOR] Ac
            [COLOR=Navy]End[/COLOR] If
        [COLOR=Navy]Next[/COLOR] n
    [COLOR=Navy]Next[/COLOR] Dn
With Sheets("Sheet2") '[COLOR=Green][B]Change to Results sheet name[/B][/COLOR]
    .Range("A1").Resize(, 15).Value = Sheets("sheet1").Range("A1").Resize(, 15).Value '[COLOR=Green][B]Change to Data sheet name[/B][/COLOR]
    .Range("A2").Resize(c, 15) = Application.Transpose(MyRay)
[COLOR=Navy]End[/COLOR] [COLOR=Navy]With[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,578
Messages
6,125,642
Members
449,245
Latest member
PatrickL

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