Delete cell content in entire workbook if background color is yellow

Ankethaton2

New Member
Joined
Feb 23, 2009
Messages
6
I am looking for a vba code that would delete the content of a cell based on its background color.

My workbook has several worksheets in which cells have a light yellow background if the user can input in them.

Now I created a button which would reset the entire model, ie delete the content that the user placed in the yellow cells.

My code is as follows, but does not seem to work:

Sub reset()
Dim c As Range
For Each c In ThisWorkbook
If c.Interior.ColorIndex = 36 Then c.ClearContents
Next c
End Sub

Thanks for any help on this.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try this on a copy of your workbook (untested)

Code:
Sub reset()
Dim c As Range
For Each ws In Sheets
ws.Activate
For Each c in ActiveSheet.UsedRange
If c.Interior.ColorIndex = 36 Then c.ClearContents
End If
Next c
Next ws
End Sub
 
Upvote 0
Thanks. The error message disappears, but there still seems to be something not working. The text "c.Clearcontents" is highlighted in yellow in the debugger. Any ideas? Thanks for your help so far! Below what I have now as code:

Sub reset()
Dim c As Range
For Each ws In Sheets
ws.Activate
For Each c In ActiveSheet.UsedRange
If c.Interior.ColorIndex = 36 Then c.ClearContents
Next c
Next ws
End Sub
 
Upvote 0
Actually, the error message is "runtime error 1004, cannot change part of a merged cell".

I have indeed cells that are merged and have content. could this prevent the "c.clearcontents" command?

Would there be a way around this?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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