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: 30

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
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,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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