MACRO to clear the contents (but preserve formatting) of all cells of a specific color???

apurcell

New Member
Joined
Jan 14, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello,
I would like to create a macro that will clear the contents of all cells within my worksheet that are of a specific color. My Range is A1:AN400. I want to preserve the formatting, just delete the numbers within the cell.
The RGB is
Red 171
Green 255
Blue 197
Hex: #ABFFC5
I would like to have a button for this macro so that I click it, and it clears all the data within the cells that are this color.

Thank you!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Is the cell color set manually?

VBA Code:
Sub clearcells()

Dim myrange As Range

Set myrange = Range("A1:AN400")

For Each cell In myrange
If cell.Interior.Color = RGB(171, 255, 197) Then
cell.ClearContents
End If
Next cell

End Sub
 
Upvote 0
I am using the same formula but it is not working.

Sub ClearCells()

Dim myrange As Range

Set myrange = Range("A1:AN2000")

For Each cell In myrange
If cell.Interior.Color = 19 Then
cell.ClearContents
End If
Next cell

End Sub
 
Upvote 0
Your Interior.Color being 19 looks more like ColorIndex rather than Color.

What happens with

VBA Code:
Sub ClearCells()

    Dim myrange As Range, cell As Range

    Set myrange = Range("A1:AN2000")

    For Each cell In myrange
        If cell.Interior.ColorIndex = 19 Then
            cell.ClearContents
        End If
    Next cell

End Sub
 
Upvote 0
You can also do this without using a loop (probably will be faster for larger ranges). Here I'll assume that 19 was a ColorIndex value like @MARK858 did...
VBA Code:
Sub ClearCells()
  Application.FindFormat.Clear
  Application.FindFormat.Interior.ColorIndex = 19
  Range("A1:AN2000").Replace "", "", , , , , True, False
  Application.FindFormat.Clear
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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