I am new completely new to coding in excel (2013) VB and I need to write some code to do the following:
I have a 2013 workbook with multiple tabs. I need to delete values (could be hard coded numbers, categorical data [eg, "Yes/No"], or formulas) from only SOME of these tabs (let's say tabs X and Y) based upon cell fill color .
1) First, how do I obtain the color index or, more specifically the color index property (I might not be using the exact correct terminology here)? I assume I need to get this exact number from a function and then plug this number into some sub to delete the conditioned values.
2) Then, how do I use that value to write a sub (?) that will delete all the values in that specific cell color for tabs X an Y?
Any help would be much appreciated.
Thanks.
A cell's color property is held in the cell's interior
object. To get the color from cell B3
in Tab 'X' you could do:
Dim cellColor as Long
cellColor = Sheets("X").Range("B3").Interior.Color
Now the cell's color is held in variable cellColor
.
This color property is a little oddball though. It's a numeric representation of the RGB, taking the form: (R*256^2 + G*256 + B)
Kind of like a bitmask, but at the byte level. Check out the function in the second answer here for a great way to get at these values if you need them.
Really your best bet is to paint a cell, then run a macro to tell you what the color code is.
Pretending like you want to delete everything in Sheet X
Range A1:Z5000
that are colored red (65535), you could do something like:
Dim checkCell as Range 'cells are range objects
For each checkCell in Sheet("X").Range("A1:Z5000").Cells 'loop through all the cells assigning the cell in each iteration to variable checkCell
If checkCell.Interior.Color = 65535 Then 'is it red?
checkCell.ClearContents 'null out the value
End If
Next checkCell
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.