VBA- Delete Rows Above and Below Based on Criteria

aeroguy1

New Member
Joined
Mar 16, 2015
Messages
24
Hello,

I am trying to find a code to delete rows based on criteria in column A. So based on a cell in column A, the code will delete one row above, the row in question, and 25 rows below the row of a cell in column A that matches criteria "none". These rows change numbers based on other rows that are deleted above them in another code.

So basically: If cell in column A contains "none", delete that row, the row above, and 25 rows below.

Thank you!
 
Last edited:

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

Hope this might help you along the way.

Code:
Sub DeleteRows()
Dim last As Long
Dim i As Long
With ActiveSheet
    last = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = last To 3 Step -1 '3 assumes you have a header row
    If .Cells(i, 1).Value = "none" Then
        .Cells(i - 1, 1).Resize(27, 1).EntireRow.Delete
    End If
Next i
End With

End Sub

Hope it helps

Dave
 
Upvote 0
Hi....Please edit macro to do following :

I would like to delete all records older than or equal to year 2010 from a customer database.
Each customer record consists of 5 rows, the date is in Col.D, 2nd row. of 5 rows.
Date cell format is (e.g.) 08/14/2004
How do I modify your macro to delete - the date row, 1 row above, and 3 rows below?

Thanks
AK
 
Upvote 0
Sorry,

made a mistake with answering that question.


I got the same issue but unfortunatelly I can not get it to work.

I like to find in Column A if the word "*Saldenliste" is found delete including the found row and 7 Rows Above and 6 Rows below that entry all rows.

there is only one instance of that word in that column I do habe a header but that would not affect the "String I am searching for"



VBA Code:
Sub DeleteRows()
Dim last As Long
Dim i As Long
With ActiveSheet
    last = .Cells(.rows.Count, 1).End(xlUp).Row
For i = last To 1 Step -1 'Changed that from the original code!!!
    If .Cells(i, 1).Value Like "*Saldenliste" Then
        .Cells(i - 7, 1).Resize(6, 1).EntireRow.Delete
    End If
Next i
End With

Can someone please help me with this..

Many Thanks
 
Upvote 0
Sorry,

made a mistake with answering that question.


I got the same issue but unfortunatelly I can not get it to work.

I like to find in Column A if the word "*Saldenliste" is found delete including the found row and 7 Rows Above and 6 Rows below that entry all rows.

there is only one instance of that word in that column I do habe a header but that would not affect the "String I am searching for"



VBA Code:
Sub DeleteRows()
Dim last As Long
Dim i As Long
With ActiveSheet
    last = .Cells(.rows.Count, 1).End(xlUp).Row
For i = last To 1 Step -1 'Changed that from the original code!!!
    If .Cells(i, 1).Value Like "*Saldenliste" Then
        .Cells(i - 7, 1).Resize(6, 1).EntireRow.Delete
    End If
Next i
End With

Can someone please help me with this..

Many Thanks
 
Upvote 0
Sorry,

got it working!

The Resize had to be changed!

All good

So no need to help ggg :)
 
Upvote 0

Forum statistics

Threads
1,223,649
Messages
6,173,585
Members
452,522
Latest member
saeedfiroozei

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