/ Excel VBAを使用して、さまざまなルールに基づいて別のブックに条件付き書式を適用する - Excel、VBA、書式、条件付き

Excel VBAを使用して、異なる規則に基づいて別のブックに条件付き書式を適用する - Excel、VBA、書式設定、条件付き

私は大規模なデータベースを持っており、私はvbaを実行していますデータベース全体からのサンプルデータのみを含むさまざまなワークブックを作成するためのスクリプト。これはうまく機能しています。すべての関連エントリを取得するためにマトリックスを使用してから、マトリックス全体を新しいワークブックの定義済み範囲に貼り付けます(セルごとに1つのワークシートから別のワークシートにコピーしません)。私の問題は、条件付き書式に2つの規則を追加する必要があることです。

私はそのようなものを探しています:

 Application.Worksheets("Database").Cells(k, ColumnOfInterest).Select

With Selection
.FormatConditions.Delete
.FormulaR1C1 = "=RC[-3] =""A"""
.FormatConditions.Add Type:=xlExpression, Formula1:=.FormulaR1C1Local
.FormatConditions(1).Interior.ColorIndex = 6
.Formula = ""

End With

With Selection
.FormatConditions.Delete
.FormulaR1C1 = "=RC[0] ="""""
.FormatConditions.Add Type:=xlExpression, Formula1:=.FormulaR1C1Local
.FormatConditions(1).Interior.ColorIndex = 5
.Formula = ""

End With

言い換えれば、ユーザーが「A」を選択したとき目的のセルが空の場合、カラーコードは5になります。残念ながら、このコードは機能せず、条件付きのルールを1つだけ作成します。フォーマット

回答:

回答№1は2

.FormatConditions.Deleteは前のフォーマット条件を削除します。 2番目の部分で.FormatConditions.Deleteを削除し、.FormatConditions(1)を.FormatConditions(2)に変更すれば、うまくいくはずです。

Application.Worksheets("Database").Cells(k, ColumnOfInterest).Select

With Selection

.FormatConditions.Delete

.FormulaR1C1 = "=RC[-3] =""A"""
.FormatConditions.Add Type:=xlExpression, Formula1:=.FormulaR1C1Local
.FormatConditions(1).Interior.ColorIndex = 6
.Formula = ""

.FormulaR1C1 = "=RC[0] ="""""
.FormatConditions.Add Type:=xlExpression, Formula1:=.FormulaR1C1Local
.FormatConditions(2).Interior.ColorIndex = 5
.Formula = ""

End With