2017-01-19 5 views
0

非常に頻繁に、書かれた内容に応じてテキストの色を書式設定するために、常に同じ範囲ではなく、Excelワークシートに条件付き書式設定規則を作成する必要があります。Excelでテキスト値の条件付き書式設定マクロを作成する

最も一般的な状況は、「有効」の緑色で太字の「有効ではない」赤色と太字のテキストを含む範囲内のすべてのセルを回転させることです。

開発者タブのレコードマクロ機能を使用してこのマクロを作成しようとしましたが、機能しませんでした。コードは空白でした。

私はVBAに関する知識がないので、誰かが私にこのマクロの作成を助けることができるかどうか疑問に思っていました。

定義:

  • 決まっ範囲はありません、それは選択された範囲をキャプチャする必要があります。
  • テキストは、有効でない場合は緑色で太字で、有効でない場合は赤色で太字になります。
  • 1枚のみです。

[解決]

Sub EffectiveNot() 
' 
' EffectiveNot Macro 
' 
    Dim rStart As Range 
    Set rStart = Selection 
    Selection.FormatConditions.Add Type:=xlTextString, String:="Effective", _ 
     TextOperator:=xlContains 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    With Selection.FormatConditions(1).Font 
     .Bold = True 
     .Italic = False 
     .Color = -11489280 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 
    Selection.FormatConditions.Add Type:=xlTextString, String:="Not effective", _ 
     TextOperator:=xlContains 
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
    With Selection.FormatConditions(1).Font 
     .Bold = True 
     .Italic = False 
     .Color = -16776961 
     .TintAndShade = 0 
    End With 
    Selection.FormatConditions(1).StopIfTrue = False 
End Sub 

答えて

1

あなたThisWorkbook -Moduleを確認し、任意のチャンス?マクロレコーダーは毎日新しい空モジュールを追加して実行し、そこにコードをダンプします。

これは基本的にはマクロレコーダーとなります。 Selectionをあなたの使用に適した範囲オブジェクトに交換してください。

Option Explicit 

Sub format() 

    With Selection 
     With .FormatConditions 
      .Delete 

      With .Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""Not Effective""") 
       With .Font 
        .Color = vbRed 
        .Bold = True 
       End With 
       .StopIfTrue = False 
      End With 

      With .Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""Effective""") 
       With .Font 
        .Color = vbGreen 
        .Bold = True 
       End With 
       .StopIfTrue = False 
      End With 
     End With 
    End With 

End Sub 
関連する問題