Professional Documents
Culture Documents
La coleccin FormatConditions
Primero debemos recordar que un mismo rango de celdas en Excel
puede estar sujeto a varias reglas de formato condicional al mismo
tiempo, as que para guardar esa lista de formatos condicionales se
cre la coleccin FormatConditions en VBA la cual enumera todas las
reglas de formato condicional aplicadas en un rango.
Para comprender el funcionamiento de esta coleccin haremos un
ejemplo. Considera la siguiente lista de nmeros en el rango A1:A10
donde he aplicado una regla de formato condicional que resalta en
color rojo los valores mayores a 750.
Private
Sub
CommandButton1_Click()
MsgBox Range("A1:A10").FormatConditions.Count
End
Sub
La propiedad FormatCondition.Interior.Color
Cada regla de formato condicional almacena el estilo que aplicar a
las celdas que cumplan con las condiciones establecidas y
especficamente el color de relleno se almacena en la propiedad
Interior.Color. Para demostrar el valor de esta propiedad agregar un
nuevo botn de comando con el siguiente cdigo:
Private
Sub
CommandButton2_Click()
1
For i = 1 To Range("A1:A10").FormatConditions.Count
2
MsgBox "Regla " & i & vbLf & _
3
"Color: " &
4
Range("A1:A10").FormatConditions(i).Interior.Color
5
Next i
6
End
Sub
Function
COLORFC(Celda As
Range) As
Long
ReglaActiva As
Boolean
i = 1 To
Celda.FormatConditions.Count
Celda.FormatConditions(i)
Case
.Operator
xlBetween:
Evaluate(.Formula1) _
And
Case
xlNotBetween:
Evaluate(.Formula1) _
Or
Case
xlEqual:
ReglaActiva = Evaluate(.Formula1) =
Celda.Value
Case
xlNotEqual:
2
0
2
1
2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
Celda.Value
Case
xlGreater:
Evaluate(.Formula1)
Case
xlLess:
Evaluate(.Formula1)
Case
Evaluate(.Formula1)
Case
xlLessEqual:
Evaluate(.Formula1)
End
Select
Application.ScreenUpdating = False
Celda.Select
ReglaActiva = Evaluate(.Formula1)
Range(ActiveCell.Address).Select
Application.ScreenUpdating = True
End
If
ReglaActiva Then
COLORFC = .Interior.Color
Exit
If
End
With
Next
End
Function
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
End
Function
Function
SUMARPORCOLORFC(CeldaColor As
Range, Rango As
Range) As
Double
Dim
Celda As
Range
Dim
Total As
Double
Dim
Color As
Long
Color = COLORFC(CeldaColor)
For
Each
If
Celda In
Rango.Cells
End
Next
If
Celda
SUMARPORCOLORFC = Total
End
Function
Function
CONTARPORCOLORFC(CeldaColor As
Integer
Dim
Celda As
Range
Dim
Total As
Integer
Dim
Color As
Long
Color = COLORFC(CeldaColor)
For
Each
Celda In
Rango.Cells
Range, Rango As
Range) As
1
1
1
2
1
3
1
4
1
5
1
6
1
7
If
End
Next
If
Celda
CONTARPORCOLORFC = Total
End
Function