Ricardo Garcia
Ricardo Garcia

Reputation: 5

Filling out inserted cell with text

This code is running perfectly fine on inserting blank rows based on a cell value, but now I need to also fill out those new rows in range("E") saying "False". Not sure how to make this work.

Sub Procedure1()
Dim i As Integer
Dim LastRow As Long

LastRow = Sheets("Sheet1").Cells(Rows.Count, "F").End(xlUp).Row

For i = LastRow To 2 Step -1

    a = Sheets("Sheet1").Cells(i, 8).Value
        For j = 1 To a
        
        Sheets("Sheet1").Rows(i + 1).Select
        Selection.Insert Shift:=xlDown
        
    Next
    
Next

Sheets("Sheet1").Cells(i, 1).Select

End Sub

Any thoughts?

Upvotes: 0

Views: 42

Answers (1)

SJR
SJR

Reputation: 23081

You can avoid the inner loop using Resize.

And you can generally avoid Select.

Sub Procedure1()

Dim i As Long, j As Long
Dim LastRow As Long
Dim a

With Sheets("Sheet1")
    LastRow = .Cells(Rows.Count, "F").End(xlUp).Row
    For i = LastRow To 2 Step -1
        a = .Cells(i, 8).Value 'best to check this is a number before going further
        .Rows(i + 1).Resize(a).Insert Shift:=xlDown
        .Range("E" & i + 1).Resize(a).Value = "False"
    Next
End With

End Sub

Upvotes: 2

Related Questions