简体   繁体   English

根据公式中单元格中的值有条件地格式化 Excel 工作表选项卡

[英]Conditional Formatting an Excel Sheet Tab Based on Value in a Cell From a Formula

I am not a VBA code writer by any stretch of the imagination, but I was trying to dabble in the following:无论如何,我都不是 VBA 代码编写者,但我试图涉足以下内容:

The goal: Make a sheet tab color turn green when cell B3 = Complete or it turns yellow if cell B3 = Open.目标:使工作表标签颜色在单元格 B3 = 完成时变为绿色,或者在单元格 B3 = 打开时变为黄色。

The "problem": Cell B3 has a formula:=IF(SUM(B6:B8)=0,"Complete","Open"). “问题”:单元格 B3 有一个公式:=IF(SUM(B6:B8)=0,"Complete","Open")。

I tried using the code below (found it from a search) and modified it a little to what I needed.我尝试使用下面的代码(从搜索中找到它)并根据我的需要对其进行了一些修改。 Left the "Case Else" section in it just not to mess with the code too much and figured only the first 2 conditions were ever going to be met anyway.将“Case Else”部分留在其中只是为了不要过多地混淆代码并且认为无论如何只会满足前两个条件。

It works, but only when you manually type "Complete" or "Open" in cell B3 as the value.它有效,但仅当您在单元格 B3 中手动键入“完成”或“打开”作为值时。 I want to keep the formula, but the code sees the formula and not the value of the formula.我想保留公式,但代码看到的是公式而不是公式的值。 After much reading and trying to understand VBA, maybe I need Worksheet_Calculate() somewhere?经过大量阅读并试图理解 VBA,也许我在某处需要 Worksheet_Calculate()?

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice
    If Target.Address = "$B$3" Then
        Select Case Target.Value`your text`
        Case "Open"
            Me.Tab.Color = vbYellow
        Case "Complete"
            Me.Tab.Color = vbGreen
        Case Else
            Me.Tab.Color = vbBlue
        End Select
    End If
End Sub
        

As far as I know, you can't do this.据我所知,你不能这样做。

The Worksheet_Calculate event IS fired when the cell with the formula updates but there is no way to check if the event was triggered by the cell you are interested in or something else entirely.当包含公式的单元格更新时会触发 Worksheet_Calculate 事件,但无法检查该事件是由您感兴趣的单元格触发的还是完全由其他内容触发的。

What you might be able to do instead, is look into what caused the cell to update in the first place and check for that.相反,您可以做的是首先调查导致单元格更新的原因并进行检查。

What @TimWilliams might be hinting at with his comment is that you could do something like this: @TimWilliams 的评论可能暗示你可以这样做:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim checkRange As Range
    Set checkRange = Range("B6:B8")
    
    If Not Application.Intersect(checkRange, Target) Is Nothing Then
        If range("B3").Value = "Complete" Then
            Me.Tab.Color = vbGreen
        Else
            Me.Tab.Color = vbYellow
        End If
        
    End If
End Sub

But this will only work when the cells in B6-B8 range are changed manually.但这只有在手动更改 B6-B8 范围内的单元格时才有效。

You could also just do:你也可以这样做:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("B3").Value = "Complete" Then
        Me.Tab.Color = vbGreen
    Else
        Me.Tab.Color = vbYellow
    End If
End Sub

This will update the tab color every time anything changes on the sheet, not just B3 or B6:B8.这将在每次工作表上发生任何更改时更新选项卡颜色,而不仅仅是 B3 或 B6:B8。

You could do this:你可以这样做:

Private Sub Worksheet_Calculate()
    With Me.Range("B3").DisplayFormat
        If .Interior.ColorIndex = xlNone Then
            Me.Tab.ColorIndex = xlColorIndexNone
        Else
            Me.Tab.Color = .Interior.Color
        End If
    End With
End Sub

The tab color will follow the cell's fill color (or clear if there is no CF-applied fill)选项卡颜色将遵循单元格的填充颜色(如果没有应用 CF 的填充,则清除)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM