Expand each row n number of times.

Cubist

Well-known Member
Joined
Oct 5, 2023
Messages
2,139
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I want to expand each row of my data n number of times. In the example, each item# gets repeated 3 times.
Book1
ABCDEFGH
1Num to repeat3
2Result
3Item#ProductDescriptionItem#ProductDescription
41Product ADesc A1Product ADesc A
52Product BDesc B1Product ADesc A
63Product CDesc C1Product ADesc A
74Product DDesc D2Product BDesc B
85Product EDesc E2Product BDesc B
92Product BDesc B
103Product CDesc C
113Product CDesc C
123Product CDesc C
134Product DDesc D
144Product DDesc D
154Product DDesc D
165Product EDesc E
175Product EDesc E
185Product EDesc E
19
Sheet2
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about
Excel Formula:
=HSTACK(TOCOL(IF(SEQUENCE(,B1),A4:A8)),TOCOL(IF(SEQUENCE(,B1),B4:B8)),TOCOL(IF(SEQUENCE(,B1),C4:C8)))
 
Upvote 0
Hi Fluff,

This works for the sample data. Sometimes I have more columns, in theory, I could repeat the TOCOL that many times, but wondering if there's a more concise syntax.
 
Upvote 0
How about
Excel Formula:
=LET(d,FILTER(A4:C100,A4:A100<>""),r,ROWS(d),s,SEQUENCE(r*B1,,0),INDEX(d,INT(s/B1)+1,SEQUENCE(,COLUMNS(d))))
 
Upvote 0
Solution
I've been playing around and getting close, but the Result isn't as expected. I have the steps laid out below easier to follow.
Edit: Posted before seeing your reply #4.

Book1 (version 2).xlsb
ABCDEFGH
1Num to repeat3
2
3Item#ProductDescriptionTextJoin By "|"Expand with TOCOLTextBefore *Result
41Product ADesc A1|Product A|Desc A1|Product A|Desc A*11|Product A|Desc A1
52Product BDesc B2|Product B|Desc B1|Product A|Desc A*21|Product A|Desc A1
63Product CDesc C3|Product C|Desc C1|Product A|Desc A*31|Product A|Desc A1
74Product DDesc D4|Product D|Desc D2|Product B|Desc B*12|Product B|Desc B2
85Product EDesc E5|Product E|Desc E2|Product B|Desc B*22|Product B|Desc B2
92|Product B|Desc B*32|Product B|Desc B2
103|Product C|Desc C*13|Product C|Desc C3
113|Product C|Desc C*23|Product C|Desc C3
123|Product C|Desc C*33|Product C|Desc C3
134|Product D|Desc D*14|Product D|Desc D4
144|Product D|Desc D*24|Product D|Desc D4
154|Product D|Desc D*34|Product D|Desc D4
165|Product E|Desc E*15|Product E|Desc E5
175|Product E|Desc E*25|Product E|Desc E5
185|Product E|Desc E*35|Product E|Desc E5
19
Sheet1
Cell Formulas
RangeFormula
F4:F18F4=(TOCOL(E4:E8&"*"&SEQUENCE(,B1)))
G4:G18G4=TEXTBEFORE(F4#,"*")
H4:H18H4=TEXTSPLIT(G4#,"|")
E4:E8E4=TEXTJOIN("|",TRUE,A4:C4)
Dynamic array formulas.
 
Upvote 0
How about
Excel Formula:
=LET(d,FILTER(A4:C100,A4:A100<>""),r,ROWS(d),s,SEQUENCE(r*B1,,0),INDEX(d,INT(s/B1)+1,SEQUENCE(,COLUMNS(d))))
This works!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
Or a VBA option that allows you to select the data and the destination cell.

VBA Code:
Private Sub subRepeatRows()
Dim rng As Range
Dim i As Integer
Dim ii As Integer
Dim intRow As Integer
Dim rngDest As Range
Dim r As Range

  ActiveWorkbook.Save

  On Error Resume Next
  
  Set rng = Application.InputBox( _
    Title:="Range selection.", _
    Prompt:="Select rows to repeat.", _
    Type:=8)
  
  On Error GoTo 0
  
  If rng Is Nothing Then
    Exit Sub
  End If
  
  On Error Resume Next
  
  Set rngDest = Application.InputBox( _
    Title:="Cell selection.", _
    Prompt:="Select destination cell (top left).", _
    Type:=8)
  
  On Error GoTo 0

  If rngDest Is Nothing Then
    Exit Sub
  End If
  
  intRow = rngDest.Row
 
  For Each r In rng.Columns(1).Cells
    For ii = 0 To 2
      Range(Cells(intRow + ii, rngDest.Column), Cells(intRow + ii, _
        rngDest.Column)).Resize(1, 3).Value = r.Resize(1, 3).Value
    Next ii
    intRow = intRow + 3
  Next r
  
End Sub
 
Upvote 0
Just for my learning purposes, why didn't the TEXTSPLIT(G4#,"|") in column H in post #5 spill to the right for each row? It only returned the column item#.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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