Insert # of Rows after last data

jtmeunier

New Member
Joined
Nov 15, 2013
Messages
32
Good Day,

I am trying to find the last row with data in it, then insert a number of rows.
The rows inserted will be different for the first page, then similar for every page after that. Basically if the data goes to more than one page I want to put in X rows so the table is filled on th second page, just to fill the page before printing.

The number of rows to insert will be different all the time, some times it doesn't need to insert any because the data fits on one page, other times it will need to insert a few.

Here is the code to date.
Any help is appreciated.

Code:
Sub InsertBlankRows()
Dim LastRow As Range
    Dim rng As Range
    Dim CountTrue As Long
    
    Set rng = Sheets("Checklist").Range("Work")
    CountTrue = Application.WorksheetFunction.CountIf(rng, "True")
    Page2 = CountTrue - 27
    InsertRowDiff = 27 - Page2
     
    
    With Sheets("Quote")
        LastRow = Range("C" & Rows.Count).End(xlUp).row
        Range("C:C" & LastRow).Resize(InsertRowDiff, 1).EntireRow.Insert
     End With
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
One quick observation from the last line of code inside the With statement! Range("C:C" & LastRow) looks wrong to me. Shouldn't it be Range("C1:C" & LastRow)?
 
Upvote 0
Correct Thank-you,

This is where I am know with the code.
Still having issues.

Code:
Sub InsertBlankRows()    Dim rng As Range
    Dim CountTrue As Long
    
    Set rng = Sheets("Checklist").Range("Work")
    CountTrue = Application.WorksheetFunction.CountIf(rng, "True")
    Page2 = CountTrue - 27
    InsertRowDiff = 1
    
    If CountTrue = ≤27 Then
        
    ElseIf CountTrue = ≤54 Then
        With ActiveSheet
        Range("C" & Rows.Count).End(xlUp).Resize(InsertRowDiff, 1).EntireRow.Insert
        End With
    End If
    
End Sub
 
Upvote 0
Getting Closer

Syntax error above corrected,


Code:
Sub InsertBlankRows()    Dim rng As Range
    Dim CountTrue As Long
    
    Set rng = Sheets("Checklist").Range("Work")
    CountTrue = Application.WorksheetFunction.CountIf(rng, "True")
    Page2 = CountTrue - 27
    InsertRowDiff = 1
    
    If CountTrue <= 27 Then
    Exit Sub
    
    ElseIf CountTrue <= 54 Then
        With ActiveSheet
        Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(InsertRowDiff).EntireRow.Insert
        End With
    End If
    
End Sub
 
Upvote 0
Getting Closer

Syntax error above corrected,


Code:
Sub InsertBlankRows()    Dim rng As Range
    Dim CountTrue As Long
    
    Set rng = Sheets("Checklist").Range("Work")
    CountTrue = Application.WorksheetFunction.CountIf(rng, "True")
    Page2 = CountTrue - 27
    InsertRowDiff = 1
    
    If CountTrue <= 27 Then
    Exit Sub
    
    ElseIf CountTrue <= 54 Then
        With ActiveSheet
        Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(InsertRowDiff).EntireRow.Insert
        End With
    End If
    
End Sub

Sorry JT,

I'm having difficulty working out what you are trying to achieve.

It looks to me that you are trying to set something up for printing (or other output) and this is based on 27 lines per page. (This code assumes a maximum of two pages - if there are more than 54 lines, it will do nothing).

What are you trying to achieve with this line of code?
Range("C" & Rows.Count).End(xlUp).Offset(1).Resize(InsertRowDiff).EntireRow.Insert
Where are you trying to insert ONE row?

If, in fact, you want to enter a blank row at position 27, amend this line of code as follows:
Range("C1:C" & Rows.Count).End(xlUp).Offset(27).Resize(InsertRowDiff).EntireRow.Insert

(You also need to take the Dim statement for rng off the Sub row!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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