Adding Rows & Copying Data in Row Above

Wilconcl51

New Member
Joined
Oct 10, 2023
Messages
26
Office Version
  1. 2016
Platform
  1. Windows
I'm using Excel On Line
My spreadsheet has 2 columns. Colum A = Name ; Column B= Number
I'm using code to add rows below based on the Number in Column B. eg B = 2 then 2 rows inserted; B=3 then 3 rows inserted.
Code
Sub InsertRows()
Dim i As Long, lRow As Long
With ActiveSheet

lRow = .Cells(.Rows.Count, 2).End(xlUp).Row
For i = lRow To 1 Step -1
If IsNumeric(Cells(i,2)) Then
.Rows(i + 1).Resize(Cells(i, 11)).Insert
End If
Next
End With
End Sub

How can I change code to insert Rows and Copy Name in Row above to inserted Rows

Many thanks

 
No worries - try this instead:

VBA Code:
Option Explicit
Sub InsertRows_V4()
    Dim s As String, i As Long, j As Long, k As Long, n As Long, a, b
    a = Range("A1:B" & Cells(Rows.Count, "A").End(xlUp).Row)
    n = WorksheetFunction.Sum(Columns("B:B"))
    
    If n > 0 Then
        j = 1
        ReDim b(1 To UBound(a, 1) + n, 1 To 2)
        For i = 1 To UBound(a, 1)
            s = a(i, 1)
            b(j, 1) = s: b(j, 2) = a(i, 2)
            If IsNumeric(a(i, 2)) And a(i, 2) > 1 Then
                k = a(i, 2)
                For n = 1 To k
                    b(j, 1) = a(i, 1): j = j + 1
                Next n
            Else
                j = j + 1
            End If
        Next i
        Range("A1").Resize(UBound(b, 1), 2) = b
    End If
End Sub
 
Upvote 0
Solution

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
No worries - try this instead:

VBA Code:
Option Explicit
Sub InsertRows_V4()
    Dim s As String, i As Long, j As Long, k As Long, n As Long, a, b
    a = Range("A1:B" & Cells(Rows.Count, "A").End(xlUp).Row)
    n = WorksheetFunction.Sum(Columns("B:B"))
   
    If n > 0 Then
        j = 1
        ReDim b(1 To UBound(a, 1) + n, 1 To 2)
        For i = 1 To UBound(a, 1)
            s = a(i, 1)
            b(j, 1) = s: b(j, 2) = a(i, 2)
            If IsNumeric(a(i, 2)) And a(i, 2) > 1 Then
                k = a(i, 2)
                For n = 1 To k
                    b(j, 1) = a(i, 1): j = j + 1
                Next n
            Else
                j = j + 1
            End If
        Next i
        Range("A1").Resize(UBound(b, 1), 2) = b
    End If
End Sub
Perfect.
Sorry for stuffing you around
Garry
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,332
Members
453,032
Latest member
Pauh

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