2 つの VBA サブルーチンを作成しました。
1) 条件付き書式を設定する (オプションとして、演算子、式 1 および式 2 を使用)
Sub setConditionalFormatting(sheetName As String, cellRange As String, CFcellColor As String, CFfontColor As String, CFtype As XlFormatConditionType, Optional CFoperator As Variant, Optional CFformula1 As Variant, Optional CFformula2 As Variant)
On Error GoTo Errhandler
Dim sheet As Worksheet
Dim cell As range
Set sheet = Sheets(sheetName)
sheet.Select
Set cell = range(cellRange)
cell.Select
'user defined sub to print string in a file
Call OutputString("Setting Conditional Formatting...")
With cell.FormatConditions.Add( _
Type:=CFtype, _
Operator:=CFoperator, _
Formula1:=CFformula1, _
Formula2:=CFformula2)
.Interior.color = CFcellColor
.Font.color = CFfontColor
End With
Call OutputString("Conditional Formatting successfully applied")
Exit Sub
Errhandler:
'a sub for error handling task
Call ErrorHandler(Err)
Exit Sub
End Sub
2) シートで条件付き書式 (CF) を確認し、各 CF の属性を印刷するには:
Sub checkConditionalFormattingsOnSheet(sheetName As String, rng As String)
On Error GoTo Errhandler
Dim cellRange As range
Dim i As Integer
Dim temp As Variant
Sheets(sheetName).Select
Set cellRange = range(rng)
cellRange.Select
If cellRange.FormatConditions.Count > 0 Then
Call OutputString("Conditional formatting (CF) in sheet " + sheetName + ":")
For i = 1 To cellRange.FormatConditions.Count
Call OutputString(CStr(i) + ") Conditional Formatting-")
Call OutputString("Interior Color: " + CStr(cellRange.FormatConditions(i).Interior.color))
Call OutputString("Font Color: " + CStr(cellRange.FormatConditions(i).Font.color))
Call OutputString("CF Type: " + CStr(cellRange.FormatConditions(i).Type))
If IsMissing(cellRange.FormatConditions(i).Operator) Then
Call OutputString("CF Operator: Not Applicable")
Else
Call OutputString("CF Operator: " + CStr(cellRange.FormatConditions(i).Operator))
End If
Call OutputString("Formula1: " + CStr(cellRange.FormatConditions(i).Formula1))
If IsMissing(cellRange.FormatConditions(i).Formula2) Then
Call OutputString("CF Formula2: Not Applicable")
Else
Call OutputString("Formula2: " + CStr(cellRange.FormatConditions(i).Formula2))
End If
Next i
ElseIf cellRange.FormatConditions.Count = 0 Then
Call OutputString("No conditional formatting found in sheet " + sheetName)
End If
Exit Sub
Errhandler:
Call ErrorHandler(Err)
Exit Sub
End Sub
ここで、条件付き書式を設定したい場合は、関数呼び出しを行うことにより、「値が 2 より大きいセルのセルは RGB(198, 239, 206) で色付けされ、フォントは RGB(255, 255, 0) である必要があります」とします。
'PS: I am not parameterizing Optional value- Formula2 here
Call setConditionalFormatting( "MyWrkSheet", "C5:N13", RGB(198, 239, 206), RGB(255, 255, 0), xlCellValue, xlGreater, "=2")
If IsMissing(cellRange.FormatConditions(i).Formula2)
checkConditionalFormattingsOnSheetでエラーが発生しています:
エラー: アプリケーション定義またはオブジェクト定義のエラー HelpContext: 1000095、ErrorId: 1004
「Is Nothing」、「IsNull()」、Formula2 のパラメーターをそれぞれ Nothing および Null として渡すなど、他のオプションを試しましたが、うまくいきませんでした。
事前にお時間をいただき、ありがとうございます。:)