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

 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi
Try this
VBA 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, 2)).Insert
                .Cells(i, 1).Copy .Cells(i + 1, 1).Resize(Cells(i, 2))
            End If
        Next
    End With
End Sub
 
Upvote 0
Hi
Try this
VBA 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, 2)).Insert
                .Cells(i, 1).Copy .Cells(i + 1, 1).Resize(Cells(i, 2))
            End If
        Next
    End With
End Sub
Thank you.

Works really well on my test data using 12 rows . Not so good when I substantially increased the size of the sample data to 120 rows. Not sure why yet.

Garry
 
Upvote 0
Not sure how this works with the online version, but please try the following with a copy of your workbook.

VBA Code:
Option Explicit
Sub InsertRows()
    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) > 0 Then
                k = a(i, 2)
                For n = 1 To k + 1
                    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
Not sure how this works with the online version, but please try the following with a copy of your workbook.

VBA Code:
Option Explicit
Sub InsertRows()
    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) > 0 Then
                k = a(i, 2)
                For n = 1 To k + 1
                    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
Thank you.
If I wanted to insert Rows only when Number in Column B is great than 1 what would I need to change

Garry
 
Upvote 0
Easy:

VBA Code:
Option Explicit
Sub InsertRows_V2()
    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) > 0 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
I think this might be better:

VBA Code:
Option Explicit
Sub InsertRows_V3()
    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 + 1
                    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
I think this might be better:

VBA Code:
Option Explicit
Sub InsertRows_V3()
    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 + 1
                    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
Hi kevin9999,

Excellent. Works perfectly
Thank you so much

Garry
 
Upvote 0
Happy to help, and thanks for the feedback 👍
Hi kevin9999,

If I had a brain I would be dangerous.

Your code works perfectly as I said. My logical is not working.
When Column B shows 2 then I only want to add one extra Row to have two rows, when number is 3 then I only need 2 additional rows inserted

The rows to insert really needs to be 1 less that the Number in Column B.

Sorry

Garry
 
Upvote 0

Forum statistics

Threads
1,224,847
Messages
6,181,326
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