[[20211214231057]] 『VBAによる強調表示』(U_U) ページの最後に飛ぶ

[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]

 

『VBAによる強調表示』(U_U)

お世話になります。
設定したボタンによって数値データを取得し、その値の上下によってセルの色を変更したいと思っています。
具体的には、「設定」ボタンでA1セルにある数値を取得、その値から+10以上になれば緑色、ー10以下で赤色に変化するよう強調表示の設定をする、といった具合です。
A1の値は別のツールによって変動しているので、任意のタイミングで基準値を設定したいです。

マクロの記録から引っ張ってみましたがVBAについてほとんど存じ上げていないので、アドバイスいただけたらと思います。よろしくお願いします。

Option Explicit
Sub Color()
'
' 強調表示 Macro
'

    Dim s As String
    s Range("A1").Value

'

    Range("A1").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
        Formula1:="=s + 10"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16752384
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13561798
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False

'

    Range("A1").Select
    Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
        Formula1:="=s - 10"
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub

< 使用 アプリ:2021、使用 OS:Windows10 >


 条件付き書式を設定したい範囲はどこになるのでしょうか?
(QS) 2021/12/15(水) 00:26

別のツールにより自動的にA1セルの値が変動する、
設定ボタンを押したタイミングのA1セルの値を基準に
A1セルの値が+10以上、-10以下のときにA1セルに色がつく

という理解で良いですか?
(きまぐれおじさん) 2021/12/15(水) 02:02


 Sub Sample()
    With Range("A1")
    'A1セルについて
        If Not IsNumeric(.Value) Then Exit Sub
        '数値でなかったら終了
        .FormatConditions.Delete
        '既存の条件付き書式を削除
        With .FormatConditions.Add(Type:=xlCellValue, Operator:=xlGreaterEqual, Formula1:="=" & .Value + 10)
        '条件付き書式を追加(セルの値がいまのA1セルの値+10以上なら)
            With .Font
            'フォントを以下の設定に変更
                .Color = -16752384
                .TintAndShade = 0
            End With
            With .Interior
            '背景を以下の設定に変更
                .PatternColorIndex = xlAutomatic
                .Color = 13561798
                .TintAndShade = 0
            End With
        End With
        With .FormatConditions.Add(Type:=xlCellValue, Operator:=xlLessEqual, Formula1:="=" & .Value - 10)
        '条件付き書式を追加(セルの値がいまのA1セルの値-10以下なら)
            With .Font
            'フォントを以下の設定に変更
                .Color = -16383844
                .TintAndShade = 0
            End With
            With .Interior
            '背景を以下の設定に変更
                .PatternColorIndex = xlAutomatic
                .Color = 13551615
                .TintAndShade = 0
            End With
        End With
    End With
 End Sub

(きまぐれおじさん) 2021/12/15(水) 08:30


>QS様
色を変更したいのは、データのあるA1セルです。

>きまぐれおじさん様
ご丁寧にありがとうございます!
参考にさせていただいたところ、順調に稼働しています。
助かりました。
(U_U) 2021/12/15(水) 12:59


コメント返信:

[ 一覧(最新更新順) ]


YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki. Modified by kazu.