简体   繁体   English

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

[英]Conditional Formatting Excel Based On Date In Another Cell

I looked through everything re conditional formatting, but nothing touched on my issue. 我浏览了所有有关条件格式的内容,但没有涉及到我的问题。

I am trying to highlight cells in a column based on the date in another cell, as well as the text in the cell in question. 我试图根据另一个单元格中的日期以及相关单元格中的文本突出显示列中的单元格。 I have been able to do both parts separately as basic conditional formatting, but not sure how to have it all work as one using a formula. 我已经能够分别将这两个部分作为基本的条件格式进行处理,但是不确定如何使用公式将它们作为一个整体来工作。

A1 has the text and D1 has the date. A1有文本, D1有日期。 If the date is today and the text is either 2 or 3, I want the cell to be coloured using conditional formatting. 如果日期是今天,并且文本是2或3,我希望使用条件格式为单元格着色。

Here is the formula I tried for conditional formatting, but with no result:- 这是我尝试用于条件格式的公式,但没有结果:

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

Basically, if the date in D1 is today and A1 is either a 2 or 3, then apply the conditional formatting. 基本上,如果D1的日期是今天,而A1是2或3,则应用条件格式。 Seems simple, but I can only get it working as separate parts 看起来很简单,但我只能将其作为单独的部分工作

Thanks in advance 提前致谢

Either use 无论使用

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

Or 要么

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

Just 'Imagine' IF in Conditional Formatting 只是“想象”条件格式中频

  • Every date in Excel is actually a number, eg for 27.01.2019 it is 43492 . Excel中的每个日期实际上是一个数字,例如,对于27.01.2019它是43492 You can format a cell containing a date, as number, and see for yourself. 您可以将包含日期的单元格设置为数字格式,然后自己查看。
  • When there is time with a date, there is additionally a decimal part, eg for 27.01.2019 14:52:17 the number would be approximately 43492.6196 , which is different than 43492 . 当有日期的时间时,还有一个小数部分,例如27.01.2019 14:52:17该数字大约为43492.6196 ,与43492不同。

Therefore you have to use the INT function to round the number down to the nearest integer resulting in the following conditional formatting formula : 因此,您必须使用INT函数将数字四舍五入到最接近的整数,从而导致以下条件格式公式

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

used eg for cell E1 . 用于例如单元格E1

  • So the logic behind the formula could be interpreted: (just imagine the IF ): IF the rounded down value of cell D1 is equal to TODAY 's value (date) AND the value in cell A1 is EITHER FA_Win_3 OR FA_Win_2 , DO apply the formatting (, or else don't ). 因此,公式背后的逻辑可以被解释:(想像的IF ): IF单元的向下舍入值D1等于TODAY的值(日期) AND在单元中的值A1EITHER FA_Win_3 OR FA_Win_2 ,DO应用格式化(否则格式化)。

I'm not in front a computer, but you can try this: 我不在电脑前,但是您可以尝试以下操作:

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

If you have trouble with users pasting over the cells and messing up with the conditional format, you can add this soubroutine to the worksheet. 如果您在用户粘贴单元格和弄乱条件格式方面遇到麻烦,则可以将此sorouroutine添加到工作表中。

To add it: 要添加它:
- Press F11 -按F11
- Double click the sheet name -双击工作表名称
- Copy paste the code -复制粘贴代码
- Read the comments inside the code and adapt it to fit your needs -阅读代码中的注释,并使其适应您的需求
- Save the workbook as macro-enabled -将工作簿另存为启用宏

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

Please mark this answer if it helped you 如果有帮助,请标记此答案

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

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