VBA- Delete a specific row with text+delete the row before+ delete the row after

tzav

New Member
Joined
Sep 5, 2022
Messages
16
Office Version
  1. 2010
Platform
  1. Windows
Hi,

I need to delete with VBA every row that contains the value "00/00/0000"+ one row before and one row after (3 rows in total).
the values are in column m.

For example:
row 1
row 2
row 3- 00/00/0000
row 4
row 5
row 6- 00/00/0000
row 7
row 8
etc...

In this example, the result needs to be that only rows 1+8 stay.

Thanks
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Perhaps this for values in column 1

VBA Code:
Sub DeleteFormat()

    Dim i  As Long
    
    For i = Cells(Rows.Count, 1).End(xlUp).Row To 1 Step -1
        If Cells(i, 1) = Format(Cells(i, 1), "##/##/####") Then
            Cells(i - 1, 1).Resize(3).EntireRow.Delete
        End If
    Next
    
End Sub
 
Upvote 0
What columns are we looking in? Will it always be column A?
Where exactly does this value of "00/00/0000" show up? Is it always at the end of the string?
Are we looking for ANY date, or just literally "00/00/0000"?
 
Upvote 0
I am sorry, I missed that you wrote that the values were in Column M. Please try this instead...

VBA Code:
Sub DeleteFormat()

    Dim i  As Long
    
    For i = Cells(Rows.Count, 13).End(xlUp).Row To 1 Step -1
        If Cells(i, 13) = Format(Cells(i, 13), "##/##/####") Then
            Cells(i - 1, 13).Resize(3).EntireRow.Delete
        End If
    Next
    
End Sub
 
Upvote 0
I am sorry, I missed that you wrote that the values were in Column M. Please try this instead...

VBA Code:
Sub DeleteFormat()

    Dim i  As Long
   
    For i = Cells(Rows.Count, 13).End(xlUp).Row To 1 Step -1
        If Cells(i, 13) = Format(Cells(i, 13), "##/##/####") Then
            Cells(i - 1, 13).Resize(3).EntireRow.Delete
        End If
    Next
   
End Sub
Thanks but when I tried, it deleted all the cells in the column.
 
Upvote 0
Perhaps you need to show us a better example of your data. This code worked on the sample you provided in your Post #1.
 
Upvote 0
What columns are we looking in? Will it always be column A?
Where exactly does this value of "00/00/0000" show up? Is it always at the end of the string?
Are we looking for ANY date, or just literally "00/00/0000"?
Column m.
The string only includes these figures, nothing before or after in the same cell.
Not any date. Other dates should stay. Only literally 00/00/0000. Delete this row+row before+ row after (3 rows), every time it's on the list.
 
Upvote 0
Updating igold's code like this should do what you want:
VBA Code:
Sub DeleteZeroDates()

    Dim i  As Long
    
    For i = Cells(Rows.Count, 13).End(xlUp).Row To 1 Step -1
        If Right(Trim(Cells(i, 13)), 10) = "00/00/0000" Then
            Cells(i - 1, 13).Resize(3).EntireRow.Delete
        End If
    Next
    
End Sub
 
Upvote 0
Solution
Perhaps you need to show us a better example of your data. This code worked on the sample you provided in your Post #1.
A screen print is attached. The red cells need to be deleted, and the green need to stay.
 

Attachments

  • screen print 7.9.22.png
    screen print 7.9.22.png
    4.5 KB · Views: 4
Upvote 0
According to Post #9, the code that @Joe4 posted in Post #8, should work...
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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