VBA to delete rows with conditions

Sufiyan97

Well-known Member
Joined
Apr 12, 2019
Messages
1,538
Office Version
  1. 365
  2. 2013
Platform
  1. Windows
I want to delete rows from below data
For example Column A has dates and B has names so for each date in column A I want to delete entire row if column B has more than four cells
in below data For first date want to delete row 6 and 7 for becuase they are rows 5 and 6 after first date
For second date I want to delete row 12 becuase it's 5th number after second date

Data
Book1
ABCD
1DateNameNumber
21/1/2022A1
3B
4C
5D
6E
7F
81/2/2022A2
9B
10C
11D
12E
131/3/2022A3
14B
15C
16D
171/4/2022A4
18B
19C
20D
21E
22F
231/5/2022A5
24
Sheet1



Expected Result

Book1
FGH
1DateNameNumber
21/1/2022A1
3B
4C
5D
61/2/2022A2
7B
8C
9D
101/3/2022A3
11B
12C
13D
141/4/2022A4
15B
16C
17D
181/5/2022A5
19
Sheet1
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
How about
VBA Code:
Sub Sufiyan()
   Dim rng As Range
   
   For Each rng In Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlBlanks).Areas
      If rng.Count > 3 Then rng.Offset(3).Resize(rng.Count - 3).EntireRow.Delete
   Next rng
End Sub
 
Upvote 0
Solution
Thank you fluff that works Perfect
Can we force code to look for only dates in column A and ignore all other text?
 
Upvote 0
It's not looking for dates, it's looking for blank cells.
 
Upvote 0
Ok,
So can we edit to see Date in column A and then 3 blank cells below that date (to keep those cells one with date and 3 blanks below that date)
and delete everything except above?
 
Upvote 0
So can we edit to see Date in column A and then 3 blank cells below that date (to keep those cells one with date and 3 blanks below that date)
That's what it does.
 
Upvote 0
I meant like this, if possible

Data
Book1
ABC
1DateNameNumber
21/1/2022A1
3B
4C
5D
6Any text other than dateE
7Any text other than dateF
81/2/2022A2
9B
10C
11Any text other than dateD
12Any text other than dateE
131/3/2022A3
14B
15Any text other than dateC
16Any text other than dateD
171/4/2022A4
18B
19C
20D
21E
22Any text other than dateF
231/5/2022A5
Sheet1


Expected Result


Book1
FGH
1DateNameNumber
21/1/2022A1
3B
4C
5D
61/2/2022A2
7B
8C
91/3/2022A3
10B
111/4/2022A4
12B
13C
14D
15E
161/5/2022A5
Sheet1
 
Upvote 0
As long as the dates are real date try
VBA Code:
Sub Sufiyan()
   Range("A2:A" & Range("B" & Rows.Count).End(xlUp).Row).SpecialCells(xlConstants, xlTextValues).EntireRow.Delete
End Sub
 
Upvote 0
Hello Fluff

Both of the solutions works for different situations

Thank you very much!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,645
Messages
6,120,711
Members
448,984
Latest member
foxpro

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