基于另一个单元格中的日期的条件格式Excel

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

我查看了条件格式化的所有内容,但没有触及我的问题。

我试图根据另一个单元格中的日期以及相关单元格中的文本突出显示列中的单元格。我已经能够将这两个部分分别作为基本的条件格式化进行,但不确定如何使用公式将其全部工作。

A1有文本,D1有日期。如果日期是今天且文本是2或3,我希望使用条件格式将单元格着色。

这是我尝试进行条件格式化的公式,但没有结果: -

=IF(AND($D1=TODAY(), OR(A1="3", A1="2")))

基本上,如果D1中的日期是今天且A1是2或3,则应用条件格式。看起来很简单,但我只能将它作为单独的部分工作

提前致谢

excel conditional-formatting
3个回答
1
投票

要么使用

=IF(AND($D1=TODAY(), OR($A$1=3, $A$1=2)),TRUE,FALSE)

要么

=AND($D1=TODAY(), OR($A$1=3, $A$1=2))

1
投票

Just 'Imagine' IF in Conditional Formatting

  • Excel中的每个日期实际上都是一个数字,例如对于27.01.2019,它是43492。您可以将包含日期的单元格格式化为数字,并自行查看。
  • 当有时间和日期时,还有一个小数部分,例如对于27.01.2019 14:52:17,这个数字大约是43492.6196,这与43492不同。

因此,您必须使用INT函数将数字向下舍入到最接近的整数,从而得到以下条件格式公式:

=AND(INT(D1)=TODAY(),OR(A1="FA_Win_3",A1="FA_Win_2"))

用于例如对于细胞E1

  • 所以公式背后的逻辑可以解释为:(只想象IF):IF单元格D1的向下舍入值等于TODAY的值(日期)AND单元格A1中的值是EITHER FA_Win_3 OR FA_Win_2,DO应用格式( ,否则不)。

0
投票

我不在电脑前,但你可以试试这个:

=AND(TEXT($B2,"dd/mm/yyyy")=TEXT(TODAY(),"dd/mm/yyyy"), OR(A1="FA_Win_3", A1="FA_Win_2"))

如果用户粘贴单元格并弄乱条件格式时遇到问题,可以将此soubroutine添加到工作表中。 要添加它: - 按F11 - 双击工作表名称 - 复制粘贴代码 - 阅读代码中的注释并根据您的需求进行调整 - 将工作簿保存为启用宏

Private Sub Worksheet_Change(ByVal Target As Range)

    ' This method has a drawback and is that the undo feature in this sheet no longer works (if you can live with it, no problem)
    ' Here is an option to bring it back: https://www.jkp-ads.com/Articles/UndoWithVBA04.asp

    ' Define variables
    Dim targetRange As Range
    Dim formulaEval As String

    ' Define the Range where they paste the date. This is the range that receives the conditional format
    Set targetRange = Range("B2:B70")

    ' Define the formula evaluated by the conditional format (replace ; for ,)
    formulaEval = "=AND(TEXT(" B2 ",""dd/mm/yyyy"")=TEXT(TODAY(),""dd/mm/yyyy""), OR(A" 2 "=""FA_Win_3"", A" 2 "=""FA_Win_2""))"

    If Not Intersect(Target, targetRange) Is Nothing Then
        With Target
            .FormatConditions.Add Type:=xlExpression, Formula1:=formulaEval
            .FormatConditions(.FormatConditions.Count).SetFirstPriority

            ' This is where the format applied is defined (you can record a macro and replace the code here)
            With .FormatConditions(1).Interior
                .PatternColorIndex = xlAutomatic
                .Color = RGB(0, 176, 80)
                .TintAndShade = 0
                .Font.Color = RGB(255, 255, 255)
                .Font.Bold = True
            End With
            .FormatConditions(1).StopIfTrue = False
        End With
    End If

End Sub

如果有帮助,请标记这个答案

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