问题
I have used conditional formatting to shade cells red in a saved range named "Loops_1000" that have duplicate values. For discussion sake the range is A1:Z100
. So now I have maybe 100 cells with various tag data in them (e.g. C-03012034
). The formatting is General. I want to search the Range and find all the red colored cells (background red = 3 I believe) and return the data in them to a single empty column (AA) in ascending order.
I looked up one solution on this site utilizing Name Manager to make a function called CellColor
. It looks like this:
=GET.CELL(63,INDIRECT("rc",FALSE))
Then in a single cell I used the following:
=IF(CellColor=3,"EXISTING","NOT USED") as a test.
It returns "NOT USED" and did not return "Existing" ever (even when I tried various colors. For a simple test I colored and empty cell with Red=3 off the palette and put "=CellColor"
in the cell. So my red cell returned the value "3".
I need more help to search the range, find all the red cells and give me the value in each red cell and return those values in a single column in ascending order (or I can just sort after I get the data to simplify the function).
So the bigger question is can a macro or VBA interpret the cell background color for Conditional Formatting = Duplicate values??
回答1:
I'm sure there is a more efficient way to code this but this might work for you. This assumes that row 1 has headers:
Sub Macro1()
'
' Macro1 Macro
'
'clears out the destination
Range("B1:B100").ClearContents
'filters by color
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$A$2:$A$100").AutoFilter Field:=1, Criteria1:=RGB(255, 0, _
0), Operator:=xlFilterFontColor
'copies the visible area
Range("A2:A100").Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy
'pastes into next column starting below the header
Range("B2").Select
ActiveSheet.Paste
'removes filer
Selection.AutoFilter
End Sub
回答2:
So the bigger question is can a macro or VBA interpret the cell background color for Conditional Formatting = Duplicate values??
Sometimes, but it is almost always not worth the effort. It is usually much simpler to make a determination based upon the original condition that would turn the rule on or off as opposed to checking if a cell 'was red' or not. Some difficulties:
- In order to determine if the cell is red due to a CF rule, you must determine if the CF rule will turn the cell red and then if the current state of the CF rule is ON.
- A cell can have multiple CF rules. Each would have to be checked to see if one of the formatting choices is
.Interior.Color = 3
. The.StopIfTrue
state also comes into effect to see if further CF rules should be examined. - There are many different types of rules so each determination would have to have to have subsets of code for each type of rule.
- Formula based CF rules use R1C1 style formula as they pertain to the cell in the top-left of the Applies to: range.
Application.Evaluate
takes A1 cell references so you need toApplication.ConvertFormula
from R1C1 to A1RelativeTo:=
the cell being examined, not the first cell in the Applies to:m. - Some types of CF rules (
.AddUniqueValues DupeUnique:=xlDuplicate
among them) have no property or method that I know of where the state of the CF can be determined on any given cell.
As you can see, this can quickly become a rabbit-hole when all you really want is to generate a list of duplicates. Here is a short macro that will locate, count and collate the duplicates in Range("Loops_1000")
.
Sub Dupes_in_Loops_1000()
Dim c As Range
With ActiveSheet
.Columns("AA").ClearContents
.Cells(1, "AA").Resize(1, 2) = Array("dupes", "nmbr")
For Each c In Range("Loops_1000")
If Application.CountIf(Range("Loops_1000"), c.Value) > 1 _
And Not CBool(Application.CountIf(.Columns("AA"), c.Value)) Then
.Cells(Rows.Count, "AA").End(xlUp).Offset(1, 0) = c.Value
.Cells(Rows.Count, "AA").End(xlUp).Offset(0, 1) = _
Application.CountIf(Range("Loops_1000"), c.Value)
End If
Next c
With .Columns("AA")
.Cells.Sort key1:=.Columns(1), order1:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
End With
End Sub
With its heavy use of Application.CountIf
that might not be the most elegant code but it is much more efficient that evaluating each cell for a CF rule that turns itself red.
来源:https://stackoverflow.com/questions/27068595/evaluating-data-in-conditionally-formatted-cells-by-color-to-return-data-in-a-co