2

セルの値が他の列の別のセルよりも大きい場合、セルの色を変更する必要があります。たとえば、G6 > D6 の値であり、このルールは列全体に適用する必要があります。

formatConditions を使用していくつかのコードを実装しましたが、結果はあまり正しくありません。

 Set rngCell = Cells(6, 7)

 Set objCF = rngCell.FormatConditions.Add _
            (Type:=xlCellValue, Operator:=xlGreater, Formula1:=rngCell.offset(, -3))
'set formats for new CF
With objCF
    .Font.ColorIndex = 26
    .Interior.ColorIndex = 19
End With 

このコードを使用すると、結果のルールは次のようになります: セル値 > 18 (18 は D6 のセル値)

しかし、私が欲しいのは次のようなルールです: セル値 > $D6

誰でも助けることができますか?

4

3 に答える 3

1

これが私が使用した方法です (マクロ レコーダーを使用して簡単に作成および変更できます)。書式設定は 7 列目 ("G") に適用されます。式は一目瞭然です。式は文字列であるため、列/行を動的に連結できることに注意してください。

Dim r As Range

Set r = Sheet1.Columns(7)
r.FormatConditions.Add Type:=xlExpression, Formula1:="=$G1>$D1"
r.FormatConditions(r.FormatConditions.Count).SetFirstPriority
With r.FormatConditions(1)
    .Interior.PatternColorIndex = xlAutomatic
    .Interior.ColorIndex = 19
    .Font.ColorIndex = 26
End With
r.FormatConditions(1).StopIfTrue = False

set r = nothing
于 2012-04-26T17:00:22.680 に答える
0

そのセルだけの場合は、これを試してください

Set rngCell = Cells(6, 7)

Set objCF = rngCell.FormatConditions.Add(Type:=xlExpression, _
                                         Operator:=xlGreater, _
                                         Formula1:="=" & rngCell.Address & ">" & rngCell.Offset(, -3).Address)
'set formats for new CF
With objCF
    .Font.ColorIndex = 26
    .Interior.ColorIndex = 19
End With

列全体で実行している場合は、これを試してください

Set rngCell = Cells(1, 7)

Set objCF = Columns("G:G").FormatConditions.Add(Type:=xlExpression, _
                                         Operator:=xlGreater, _
                                         Formula1:="=" & Replace(rngCell.Address, "$", "") & _
                                         ">" & Replace(rngCell.Offset(, -3).Address, "$", ""))
'set formats for new CF
With objCF
    .Font.ColorIndex = 26
    .Interior.ColorIndex = 19
End With
于 2012-04-26T05:52:13.693 に答える
0

ご意見をお寄せいただきありがとうございます。質問を正しく説明していない可能性があります。私が望むのは、列 G の 1 つのセルの色のみを変更することです。たとえば、値 $G$9 > $D$9 の場合、セル G9 の書式を変更するだけで済みます。頭の行)。

今、私は1つの解決策を考え出しました.現在、それはうまくいきます.

Dim r As Range
Set r = Cells(5, 7)
r.FormatConditions.Delete
r.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, Formula1:="=$D5"
r.FormatConditions(r.FormatConditions.Count).SetFirstPriority
With r.FormatConditions(1)
    .Interior.PatternColorIndex = xlAutomatic
    .Interior.ColorIndex = 19
    .Font.ColorIndex = 26
End With
r.FormatConditions(1).StopIfTrue = False

r.Copy
Range("G5:G" & lastRowNum).PasteSpecial xlPasteFormats
于 2012-04-27T04:43:06.317 に答える