How to clear cell contents based on condition in Excel?
Sometimes you may need to clear cell contents based on conditions in Excel, such as clearing the cell contents if the cell contains specific value, clearing the cell contents if there are blank cells in another column, or clearing cell contents with specific background color, etc. This article will introduce some tricks for solving these tasks quickly and easily.
Clear Cell Contents Based on cell value with VBA code
Clear entire rows if value is greater or less than a specific value with VBA code
Clear cell or row contents based on cell value with a handy feature
Clear entire rows if another cell is blank with VBA code
Clear cell contents based on background color with VBA code
Clear Cell Contents Based on cell value with VBA code
For example, you have the following data range, now, you want to clear the cell contents that are “Hoodie”, the below VBA code can do you a favor.
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: Clear cell contents if cell equals a specific value
Sub ClearContents()
'Updateby ExtendOffice
Dim xRng As Range
Dim xCell As Range
Dim xText As String
Set xRng = Application.Range("A2:A12")
xText = "Hoodie" 'Change this text to your own
For Each xCell In xRng
If xCell.Value = xText Then
xCell.clearContents
End If
Next xCell
End Sub
3. Then, press F5 key to run this code, and all cells with the text “Hoodie” are cleared at once, see screenshot:
Clear entire rows if value is greater or less than a specific value with VBA code
Supposing, you have the below data range, to clear entire row contents if the value in column D is greater or less than 300. Here, I will create a VBA code to complete it.
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: Clear entire rows if cell value is greater or less than a specific value
Sub ClearRowInValue()
'Updateby ExtendOffice
Dim xRg As Range
Dim xStrAddress As String
Dim xStrValue As Integer
Dim xCell As Range
Dim xRowRg As Range
Dim xF As Integer
Dim xBol As Boolean
xStrAddress = "D2:D12" 'Change cell range
xStrValue = 300 'change the value
Set xRg = Range(xStrAddress)
For xF = xRg.Rows.Count To 1 Step -1
Set xRowRg = xRg.Rows.Item(xF)
xBol = False
For Each xCell In xRowRg.Cells
If Application.IsNumber(xCell.Value) Then
If xCell.Value > xStrValue Then
xBol = True
Exit For
End If
End If
Next
If xBol Then
xRowRg.EntireRow.clearContents
End If
Next
End Sub
3. Then press F5 key to run this code, and all the row contents are cleared if the value in column D is greater than 300, see screenshot:
Clear cell or row contents based on cell value with a handy feature
If you have Kutools for Excel, with its Select Specific Cells feature, you can select the cells or rows based on the specific value, and then delete the cell contents at once.
After installing Kutools for Excel, please do as this:
1. Select the data range with values that you want to clear cell or row contents based on, and then, click Kutools > Select > Select Specific Cells, see screenshot:
2. In the opened Select Specific Cells dialog box, do the following operations:
- Click Cell or Entire row that you want to select;
- Specify the criteria under the Specific type section as you need. Here, I want to select the rows whose value in column D is greater than 300.
3. Then click Ok button, and all the rows with the value greater than 300 are selected. Now, you just need to press Delete key on the keyboard to delete the cell contents, see screenshot:

Clear rows data in a range if contains blank cell with VBA code
In this case, if the cell in the data range contains any blank, I want to clear the rows data from the range as below screenshot shown.
To finish this task, please apply the following VBA code:
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and then, paste the following code in the Module Window.
VBA code: Clear rows data if contains blank cell
Sub ClearContents()
'Updateby ExtendOffice
Dim xcell As Range
Set xrng = Application.Range("A2:D12")
For Each xcell In xrng
If xcell.Value = "" Then
Intersect(xcell.EntireRow, xrng).ClearContents
End If
Next
End Sub
3. Then, press F5 key to run this code, and the rows data with blank cells are cleared at once in the specific range as you need.
Clear cell contents based on background color with VBA code
To clear the cells which are filled with a specific background color, you can use the following VBA code.
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and then, paste the following code in the Module Window.
VBA code: Clear cell contents based on background color
Sub Clearcontents()
'Updateby ExtendOffice
Dim xcell As Range
Dim xrng As Range
Set xrng = Application.Range("A2:D12")
For Each xcell In xrng
If xcell.Interior.Color = RGB(252, 228, 214) Then
xcell.Clearcontents
End If
Next
End Sub
3. And then, press F5 key to run this code, and the cell contents with the specified background color are cleared immediately. See screenshot:
The Best Office Productivity Tools
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
- Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
- Merge Cells/Rows/Columns and Keeping Data; Split Cells Content; Combine Duplicate Rows and Sum/Average... Prevent Duplicate Cells; Compare Ranges...
- Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
- Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
- Favorite and Quickly Insert Formulas, Ranges, Charts and Pictures; Encrypt Cells with password; Create Mailing List and send emails...
- Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
- Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
- Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
- Pivot Table Grouping by week number, day of week and more... Show Unlocked, Locked Cells by different colors; Highlight Cells That Have Formula/Name...

- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
