Macro to add the number of blank rows blow data as it shows

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,
I have a list of data and In column O I have a number, will always be at least 1
I want to insert below each line of data the number of row for that number

So Lets say O10 = the number 10 I want to insert 10 rows below so from 11, but for all the data in every row,
I'm thinking it would be easiest to start at the last row and work up but i dont know how to do this?
please help if you can

Thank you
Tony
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I'm thinking it would be easiest to start at the last row and work up but i dont know how to do this?

Try this (just change the wsSrc variable to the tab name that has the data if it's not Sheet1):

VBA Code:
Option Explicit
Sub Macro1()

    Dim wsSrc As Worksheet
    Dim lngRowFrom As Long, lngRowTo As Long, lngRow As Long
    
    Application.ScreenUpdating = False
    
    Set wsSrc = ThisWorkbook.Sheets("Sheet1") '<-Sheet name containing data. Change to suit if necessary.
    lngRowFrom = 1
    lngRowTo = wsSrc.Cells(Rows.Count, "O").End(xlUp).Row
    
    For lngRow = lngRowTo To lngRowFrom Step -1
        If Val(wsSrc.Range("O" & lngRow)) > 0 Then
            wsSrc.Rows(lngRow + 1 & ":" & lngRow + Val(wsSrc.Range("O" & lngRow))).Insert
        End If
    Next lngRow
    
    Application.ScreenUpdating = True    

End Sub

Regards,

Robert
 
Upvote 0
Solution
Try this too :unsure:
Your data before
Data Before.JPG


Run macro
VBA Code:
Sub Insert_Rows()
    Dim RowNos As Range
    For Each RowNos In Range("O2:O6")
        If RowNos.Value > 0 And RowNos.Offset(1).Value > 0 Then
            Dim iCount As Long
            Dim i As Long
            iCount = RowNos.Value
            For i = 1 To iCount
                RowNos.Offset(1).EntireRow.Insert
            Next i
        End If
    Next
End Sub

Data after
Data After.JPG
 
Upvote 0
Thanks Trevor and Sahak,
Both ideas worked great so thank you,
Tony
 
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,476
Members
448,967
Latest member
visheshkotha

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