VBA loop creating specified number of rows and repeating text

ktgoodlund

New Member
Joined
Jun 22, 2022
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
I need a VBA loop that will create the number of rows and repeat the text in column A, for example (animal in A, quantity of rows in B):

Cat 4
Dog 3
Horse 2

Desired result:

Cat
Cat
Cat
Cat
Dog
Dog
Dog
Horse
Horse

For possibly hundreds of lines.

Thanks!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How about this?

ExpandRange.xlsm
ABC
1Cat 4Cat
2Dog 3Cat
3Horse 2Cat
4Cat
5Dog
6Dog
7Dog
8Horse
9Horse
Sheet10


VBA Code:
Sub AREP()
Dim r As Range:         Set r = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim SP() As String

With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(AR)
        SP = Split(AR(i, 1), " ")
        For j = 1 To SP(1)
            .Add SP(0)
        Next j
    Next i
    Set r = r.Offset(, 2).Resize(.Count)
    r.Value2 = Application.Transpose(.ToArray)
End With
End Sub
 
Upvote 0
I missed that you had the values separated into 2 columns.

VBA Code:
Sub AREP()
Dim r As Range:         Set r = Range("A1:B" & Range("A" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2

With CreateObject("System.Collections.ArrayList")
    For i = 1 To UBound(AR)
        For j = 1 To AR(i, 2)
            .Add AR(i, 1)
        Next j
    Next i
    r.Offset(, 3).Resize(.Count, 1).Value2 = Application.Transpose(.ToArray)
End With
End Sub
 
Upvote 0
Welcome to the Board!

Here is another way:
VBA Code:
Sub MyInsertRows()

    Dim lr As Long
    Dim r As Long
    Dim n
    
    Application.ScreenUpdating = False
    
'   Find last row with data in column A
    lr = Cells(Rows.Count, "A").End(xlUp).Row
    
'   Loop through all rows backwards
    For r = lr To 1 Step -1
'       Get number of rows to insert from column B
        n = Cells(r, "B")
'       Insert rows, if necessary
        If n > 1 Then
'           Insert blank rows
            Cells(r + 1, "A").Resize(n - 1, 1).EntireRow.Insert
'           Populate new rows
            Cells(r, "A").Resize(n, 1).Value = Cells(r, "A").Value
        End If
    Next r
    
'   Clear all of column B
    Columns("B:B").ClearContents

    Application.ScreenUpdating = True

    MsgBox "Macro complete!"

End Sub
 
Upvote 0
I know this won't help the OP since version is 2016, but here's the formula way I came up with to do this.

Book1 (version 1).xlsm
ABCD
1Cat4Cat
2Dog3Cat
3Horse2Cat
4Cat
5Dog
6Dog
7Dog
8Horse
9Horse
Sheet8
Cell Formulas
RangeFormula
D1:D9D1=LET(n,B1:B3,INDEX(A1:A3,MATCH(SEQUENCE(SUM(n)),(MMULT(N(ROW(n)>=TRANSPOSE(ROW(n))),n)-INDEX(n,1)+1)+SEQUENCE(COUNT(n),,0),1)))
Dynamic array formulas.
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,384
Members
448,956
Latest member
JPav

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