VBA/Excel Formula to insert rows

RichTom44

New Member
Joined
Oct 23, 2023
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello,

Is there a formula in Excel or VBA for inserting rows automatically based on the given qty? I have attached a sample the first column is the Name of the items and the second column is the number of qty so, I need to insert a row based on the number of qty. If the number of qty is 5, I want to insert 4 rows.
 

Attachments

  • Screenshot 2024-07-21 142232.png
    Screenshot 2024-07-21 142232.png
    3.1 KB · Views: 32

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Assuming the QTY column is column C and that heading is in row 1, try this macro.

VBA Code:
Sub Insert_Rows()
  Dim r As Long, rws As Long
  
  Application.ScreenUpdating = False
  For r = Range("C" & Rows.Count).End(xlUp).Row To 2 Step -1
    rws = Cells(r, "C").Value
    If rws > 1 Then Rows(r + 1).Resize(rws - 1).Insert
  Next r
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Assuming the QTY column is column C and that heading is in row 1, try this macro.

VBA Code:
Sub Insert_Rows()
  Dim r As Long, rws As Long
 
  Application.ScreenUpdating = False
  For r = Range("C" & Rows.Count).End(xlUp).Row To 2 Step -1
    rws = Cells(r, "C").Value
    If rws > 1 Then Rows(r + 1).Resize(rws - 1).Insert
  Next r
  Application.ScreenUpdating = True
End Sub
Hi Sir, you are a lifesaver. Thank you so much It works perfectly.
 
Upvote 0

Forum statistics

Threads
1,225,754
Messages
6,186,826
Members
453,377
Latest member
JoyousOne

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