y sakudaさん
返信ありがとうございます。
コードを投稿いたします。
Targetに判定したいセルを設定して以下のサブを呼び出し,
myColorにて設定された背景色を取得しようとしています。
よろしくお願いいたします。
'*---------------------------------------------------------------*
'* 処理名:getConditionalFormatColor
'* 概 要:条件付書式にて変更されるセル背景色の取得処理
'*
'* 引数1:対象セル(IN)
'* 引数2:カラー (OUT)
'*---------------------------------------------------------------*
Sub getConditionalFormatColor(ByVal Target As Range, myColor As Variant)
Dim i As Long
Dim myColor2 As Variant
myColor = myColor2
With Target
.Activate
If .Count > 1 Then Exit Sub
If .FormatConditions.Count = 0 Then Exit Sub
Dim formula As Variant
If Application.Version <= 11 Then
' Excel2003以前
For i = 1 To .FormatConditions.Count
formula = .FormatConditions(i).Formula1
If Evaluate(formula) Then
myColor = .FormatConditions(i).Interior.Color
Exit For
End If
Next
Else
' 2007以降では条件付き書式の数式を取得するFormula1では条件付き書式ルールの
' 数式を取得してしまい、実際に実行されている数式を取得することができない。
' 条件付き書式ルールの適用範囲を強制的に変更することにより対応。
For i = 1 To .FormatConditions.Count
Dim sRange1 As Range
Dim sRange2 As Range
Set sRange1 = .FormatConditions(i).AppliesTo
Set sRange2 = Target
.FormatConditions(i).ModifyAppliesToRange sRange2
formula = .FormatConditions(i).Formula1
.FormatConditions(i).ModifyAppliesToRange sRange1
If Evaluate(formula) Then
myColor = .FormatConditions(i).Interior.Color
Exit For
End If
Next
End If
If myColor = 16711935 Then
MsgBox "条件付き書式でピンクです。"
ElseIf myColor = 16777215 Then
MsgBox "条件付き書式で白です。"
ElseIf Not IsEmpty(myColor) Then
MsgBox "条件付書式でピンク・白以外の色です。"
Else
MsgBox "自動=条件付き書式で色が変わっていません。"
End If
End With
End Sub