根据相应的范围或标准有条件地格式化特定单元格(热图样式)

问题描述 投票:0回答:1

我有一系列代表不同结果的价值观。在该范围内,有一个值(每行一个),我试图将其作为应用颜色着色(热图;渐变 RBY)的目标。标准是$I4+0.5=BJ$3:BP$3。再次强调,只能有 1 场比赛。

我通过 Chat GPT 尝试了很多不同的场景,但运气为零。

下图是我手动单击单元格并应用热图的结果。这是我想要的最终目标,但显然有大量数据每天都在变化,我不想手动执行这些操作。

此外,我添加了一个辅助列/范围( =$I4#+0.5=BQ$3:BW$3 ),其中“TRUE”是与条件的匹配。不确定是否需要,但这是我尝试解决的场景之一。我在这里不知所措。非常感谢任何帮助。

这是我正在使用的代码,但这并不针对特定单元格,也不是条件格式,它正在更改内部颜色

此代码正确地将热图配色方案应用于 T4:Z1101 中的所有单元格

    Sub ApplyHeatMapColorScale()
        Dim ws As Worksheet
        Dim dataRange As Range
    
        ' Set the worksheet
        Set ws = ThisWorkbook.Sheets("Full Analysis")
    
        ' Set the range for your data
        Set dataRange = ws.Range("T4:Z1101")
    
        ' Clear existing formatting
        dataRange.Interior.Color = RGB(255, 255, 255) ' Set default color
    
        ' Apply the specified heat map color scale to the data range
        With dataRange.FormatConditions.AddColorScale(ColorScaleType:=3)
            ' Dark Red
            .ColorScaleCriteria(1).Type = xlConditionValueLowestValue
            .ColorScaleCriteria(1).FormatColor.Color = RGB(226, 0, 0)
    
            ' Light Yellow
            .ColorScaleCriteria(2).Type = xlConditionValuePercentile
            .ColorScaleCriteria(2).FormatColor.Color = RGB(255, 235, 132)
    
            ' Dark Green
            .ColorScaleCriteria(3).Type = xlConditionValueHighestValue
            .ColorScaleCriteria(3).FormatColor.Color = RGB(0, 176, 80)
        End With
    End Sub

此代码检查是否能够正确识别辅助范围中的 TRUE FALSE

    Sub TestConditionIdentification()
        Dim ws As Worksheet
        Dim conditionRange As Range
        Dim cell As Range
        Dim i As Long
    
        ' Set the worksheet
        Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to the name of your sheet
    
        ' Set the range for the condition (TRUE/FALSE)
        Set conditionRange = ws.Range("AA4:AG1101")
    
        ' Loop through each cell in the condition range
        For Each cell In conditionRange
            ' Check if the value is TRUE
            If cell.Value = True Then
                ' Display a message box for each TRUE cell
                MsgBox "Cell " & cell.Address & " is TRUE"
            End If
        Next cell
    End Sub

只是很难将两者放在一起。

非常感谢!!!

excel vba heatmap conditional-formatting
1个回答
0
投票
  • Helper
    范围不是必需的。
  • 数据愤怒应用了两个条件格式规则
Option Explicit

Sub ApplyHeatMapColorScale()
    Dim ws As Worksheet
    Dim dataRange As Range
    Dim lastRow As Long, i As Long
    Dim oFC As FormatCondition
    Set ws = ThisWorkbook.Sheets("Full Analysis")
'    Set ws = Sheet2 ' for testing
    ' Remove existing fc
    With ws.Cells
        For i = .FormatConditions.Count To 1 Step -1
            .FormatConditions(i).Delete
        Next
    End With
    ' Set the range for your data
    lastRow = ws.Cells(ws.Rows.Count, "BJ").End(xlUp).Row
    Set dataRange = ws.Range("BJ4:BP" & lastRow)
    ' Apply the specified heat map color scale to the data range
    With dataRange.FormatConditions
        With .AddColorScale(ColorScaleType:=3)
            ' Dark Red
            .ColorScaleCriteria(1).Type = xlConditionValueLowestValue
            .ColorScaleCriteria(1).FormatColor.Color = RGB(226, 0, 0)
            ' Light Yellow
            .ColorScaleCriteria(2).Type = xlConditionValuePercentile
            .ColorScaleCriteria(2).FormatColor.Color = RGB(255, 235, 132)
            ' Dark Green
            .ColorScaleCriteria(3).Type = xlConditionValueHighestValue
            .ColorScaleCriteria(3).FormatColor.Color = RGB(0, 176, 80)
        End With
        ' Reset format for irrelated cells
        .Add Type:=xlExpression, Formula1:="=NOT($I4+0.5=BJ$3)"
    End With
    dataRange.FormatConditions(dataRange.FormatConditions.Count).SetFirstPriority
    dataRange.FormatConditions(1).StopIfTrue = True
End Sub

微软文档:

FormatCondition.StopIfTrue 属性 (Excel)

FormatCondition.SetFirstPriority 方法 (Excel)

Range.FormatConditions 属性 (Excel)

© www.soinside.com 2019 - 2024. All rights reserved.