2

私のプロジェクトでは、次のようなセル値の条件に基づいてセルの背景色をフォーマットするコードがあります。

//if value is 1 than colorIndex is 3;
Excel.FormatCondition condition = colorRange.FormatConditions.Add(
    Type: Excel.XlFormatConditionType.xlTextString, 
    Operator: Excel.XlFormatConditionOperator.xlEqual,
    Formula1: "=1");
condition5.Interior.ColorIndex = 3;

この条件は正常に機能しますが、次のコードの条件は機能しません。

//if value is Red Color than colorIndex is 3;
Excel.FormatCondition condition = colorRange.FormatConditions.Add(
    Type: Excel.XlFormatConditionType.xlTextString, 
    Operator: Excel.XlFormatConditionOperator.xlEqual,
    Formula1: "=Red Color");
condition5.Interior.ColorIndex = 3;

表示されるエラー メッセージは「パラメーターが正しくありません」です。

私は何を間違っていますか?

4

2 に答える 2

1

エラーは、渡している Formula1 パラメータが式であるという事実に起因すると思います。これは、Excel セルに手動で書き込む数式のように記述する必要があることを意味します。これは、パラメーターとして「赤色」を記述すると、Excel はそれを数式に変換しようとしますが、構文が間違っているため、エラーが発生することを意味します。

"=Red" (文字列の色の部分なし) だけを入力しようとすると、エラーは発生しませんが、セルの値とセルの値を比較するため、とにかく機能しません。赤という名前のセルに含まれています(ワークシートにそのようなセルはないと思います)。

解決策は、次のように文字列を " で囲むことです。

Excel.FormatCondition condition = colorRange.FormatConditions.Add(
    Type: Excel.XlFormatConditionType.xlTextString, 
    Operator: Excel.XlFormatConditionOperator.xlEqual,
    Formula1: "=\"Red Color\"");
于 2012-06-01T05:47:51.423 に答える