VBA to delete range of cells that don’t contain certain text

SJ1981

New Member
Joined
Mar 19, 2019
Messages
4
Hello I have a cell range C5:F34
Each row relates to same record with a different column heading C:F. I would like to be able to clear all cells except for the rows that have “Not Arrived” or “DCase” in column F.
I have made a start with the below however it only deletes the contents of cell F & not C:E aswell? I’m also not sure how to add “DCase” into the criteriacell.Value section, can only get it to work with one or the other.
many thanks for any help that can be given.

TEST Macro
'
Dim criteriarange As Range

Dim criteriacell As Range

Set criteriarange = Range("F5:F34")

For Each criteriacell In criteriarange

If Not criteriacell.Value = "Not Arrived" Then

criteriacell.ClearContents

End If

Next criteriacell

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
How about
Code:
Sub SJ1981()
   With ActiveSheet
      .Range("C4:F34").AutoFilter 4, "Not Arrived", xlOr, "DCase"
      .AutoFilter.Range.Offset(1).Value = ""
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Thank you - this very nearly does the right thing.... it’s the wrong way round though it clears not arrived & DCase rather than everything but these..... so presumably I just need to change the “ “ references to the other options? Thanks
 
Upvote 0
I tried changing it to all options except Not arrived & DCase but there was an error as too many arguments?
 
Upvote 0
In that case how about
Code:
Sub SJ1981()
   With ActiveSheet
      .Range("C4:F34").AutoFilter 4, "<>Not Arrived", xlAnd, "<>DCase"
      .AutoFilter.Range.Offset(1).Value = ""
      .AutoFilterMode = False
   End With
End Sub
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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