Delete rows if a group does not contain a colored cell

farmerjack190

New Member
Joined
May 17, 2016
Messages
11
I have a sheet which we use to aid a our warehouse movements when new stock arrives (ZFLOOR) location. What I am needing to do is to have a script that can remove a block of items if "ZFLOOR" is not contained.
In this example I need to remove rows 7-10, 12-13, 15-16 and preferably the blank row below to keep formatting etc but need to keep data in rows 21-22 in this case. Obviously "ZFLOOR" will change each time but always in Column P so rows to be deleted will be subject to a group not containing ZFLOOR or a Green conditional formatted cell.

Have no idea where to start, any help appreciated.
 

Attachments

  • 2021-11-22.png
    2021-11-22.png
    44.2 KB · Views: 12

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
It's hard for me to understand because you are referring to row numbers but your image does not show row numbers.
And including if conditional formatting makes it harder.
 
Upvote 0
It's hard for me to understand because you are referring to row numbers but your image does not show row numbers.
And including if conditional formatting makes it harder.
The row numbers were only referenced to this example (I did realize I cut them I just counted the rows down in the example) as it could be any combination of rows required to be removed. The conditional format isn't necessary as I only use that as we do it visually and manually, if it were automated the conditional format I would remove. FYI the C/format only highlights where it sees ZFLOOR.
 
Upvote 0
Are you saying you want to delete all rows that do not have "ZFLOOR" in column P?
 
Upvote 0
Are you saying you want to delete all rows that do not have "ZFLOOR" in column P?
Where there is nested rows which may be of 2 or more rows and does NOT contain ZFLOOR in any of that nested group, then remove, if ZFLOOR is in a nested group, keep all adjoining rows. continue for all nested groups down.
 
Upvote 0
@farmerjack190
Try this:
VBA Code:
Sub farmerjack190()

Dim c As Range, f As Range, i As Long
Application.ScreenUpdating = False
Set f = Range("P2", Cells(Rows.Count, "P").End(xlUp)).SpecialCells(xlCellTypeConstants)
For i = f.Areas.Count To 1 Step -1
    Set c = f.Areas(i).Find(What:="ZFLOOR", LookIn:=xlValues, lookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
        If Not c Is Nothing Then f.Areas(i).EntireRow.Delete
Next
Application.ScreenUpdating = True

End Sub

Example:
BEFORE:
Book1
OPQR
1
2Owen3ZManuel
3Giovanni2OAdrien
4Jeremiah1FMario
5
6Clayton6XJoe
7MarvinZFLOORJPaxton
8
9
10Ariel12WPatrick
11Iker10UBoone
12Blaise11BZein
13
14
15RexZFLOORNKellen
16Ruben17MClyde
17Aydin18KMark
18
Sheet1


AFTER:
Book1
OPQR
1
2Owen3ZManuel
3Giovanni2OAdrien
4Jeremiah1FMario
5
6
7
8Ariel12WPatrick
9Iker10UBoone
10Blaise11BZein
11
12
13
Sheet1
 
Upvote 0
@farmerjack190
Try this:
VBA Code:
Sub farmerjack190()

Dim c As Range, f As Range, i As Long
Application.ScreenUpdating = False
Set f = Range("P2", Cells(Rows.Count, "P").End(xlUp)).SpecialCells(xlCellTypeConstants)
For i = f.Areas.Count To 1 Step -1
    Set c = f.Areas(i).Find(What:="ZFLOOR", LookIn:=xlValues, lookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
        If Not c Is Nothing Then f.Areas(i).EntireRow.Delete
Next
Application.ScreenUpdating = True

End Sub

Example:
BEFORE:
Book1
OPQR
1
2Owen3ZManuel
3Giovanni2OAdrien
4Jeremiah1FMario
5
6Clayton6XJoe
7MarvinZFLOORJPaxton
8
9
10Ariel12WPatrick
11Iker10UBoone
12Blaise11BZein
13
14
15RexZFLOORNKellen
16Ruben17MClyde
17Aydin18KMark
18
Sheet1


AFTER:
Book1
OPQR
1
2Owen3ZManuel
3Giovanni2OAdrien
4Jeremiah1FMario
5
6
7
8Ariel12WPatrick
9Iker10UBoone
10Blaise11BZein
11
12
13
Sheet1
Hi, thanks for this but this code deletes the rows I want to keep and leaves the ones I would like removed, hence the macro works perfect but can you reverse the selection to be deleted so it keeps the rows sectioned with ZFLOOR? I really really appreciate your help.
 
Upvote 0
Sorry, misunderstood the criteria, try this one:

Rich (BB code):
Sub farmerjack190_2()

Dim c As Range, f As Range, i As Long
Application.ScreenUpdating = False
Set f = Range("P2", Cells(Rows.Count, "P").End(xlUp)).SpecialCells(xlCellTypeConstants)
For i = f.Areas.Count To 1 Step -1
    Set c = f.Areas(i).Find(What:="ZFLOOR", LookIn:=xlValues, lookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True, SearchFormat:=False)
        If c Is Nothing Then f.Areas(i).EntireRow.Delete
Next
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,011
Messages
6,122,680
Members
449,091
Latest member
peppernaut

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