Insert Row Logic in VBA Macro

chapcrap

New Member
Joined
Oct 2, 2015
Messages
16
So, I've been working with different ideas for a couple of days and can't get it. I've taken too much time off from writing macros and don't know what I'm doing anymore! This should be easy...

So, almost daily, I am going to have a different data set. I want to filter on a specific item (255000) in that data set in a specific column (E). Then I want to insert a new row above each of the filtered rows. There is some other formatting I'm going to add as well, but this is part that's tripping me up. Just adding the rows in the correct places.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi Chapcrap,

Is this the result you're looking for after the data is filtered?

Filter and Insert Row Above Visible Rows.PNG


All the best,
Matt
 
Upvote 0
This is probably the easiest way to get the roww with the criteria and then insert the blank rows.
Code:
Sub t()
Dim i As Long, adr As String
With ActiveSheet
    For i = .Cells(Rows.Count, "E").End(xlUp).Row To 2 Step -1
        If .Cells(i, "E") = 255000 Then
            Rows(i).Insert
        End If
    Next
End With
End Sub

It does not use a filter.
 
Upvote 0
You can filter the range of cells on column E and then select only those filtered entire rows with the criteria item. Then right click and select 'Insert Row'.
 
Upvote 0
This is probably the easiest way to get the roww with the criteria and then insert the blank rows.
Code:
Sub t()
Dim i As Long, adr As String
With ActiveSheet
    For i = .Cells(Rows.Count, "E").End(xlUp).Row To 2 Step -1
        If .Cells(i, "E") = 255000 Then
            Rows(i).Insert
        End If
    Next
End With
End Sub

It does not use a filter.
This works. Also, once you reminded me about the visible cells only, this also works:

VBA Code:
Sub InsertRow()
'
'
    AutoFilter Field:=5, Criteria1:="255000"
    Range("E1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.EntireRow.Insert
    Range("E1").Select
    Selection.EntireRow.Delete
End Sub
 
Upvote 0
Thanks for the feedback.
regards, JLG
 
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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