Inserting New Rows based on cell value, and copying data to the new rows

gi_cho

New Member
Joined
May 26, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello!

I have an issue where I'm trying to add rows based on a cell value in column 2, and then copy that row's contents to those new rows. I found the VBA code below on another thread, but I have not been able to figure out one small issue.

The cell value in column 2 should be the TOTAL number of rows, but because the code doesn't account for the original row, there is one too many for each iteration.

For example, Business ABC should have 10 copies, and thus, has entered a 10 in column 2. I will run the code and it will ADD 10 rows below the original row. Now there are 11 rows of data for Business ABC.

Thanks in advance!



VBA Code:
Sub Inert_rows()
  Dim r As Long
  
  For r = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
    With Cells(r, 2)
      If IsNumeric(.Value) And Not IsEmpty(.Value) Then
        Rows(r + 1).Resize(.Value).Insert
        Range(Replace("A#:H#", "#", r)).Copy Destination:=Range("A" & r + 1).Resize(.Value)
      End If
    End With
  Next r
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If you try this will just copy the cell value and not the original cell

VBA Code:
Sub Inert_rows()
  Dim r As Long
  
  For r = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
    With Cells(r, 2)
      If IsNumeric(.Value) And Not IsEmpty(.Value) Then
        Rows(r + 1).Resize(.Value).Insert
        Range(Replace("A#:H#", "#", r)).Copy Destination:=Range("A" & r - 1).Resize(.Value)
      End If
    End With
  Next r
End Sub
 
Upvote 0
I have a somewhat similar request except I'd like to know if there's a non-VBA solution such as Excel formulas or Power Query to accomplish the task.
Does anyone know the answer?
I can post an Excel file for specifics but perhaps the answer.
Essentially, it's appending new rows of data based on cell values in another sheet.
I can create a new post as well if that's best.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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