简体   繁体   English

根据日期和其他条件突出显示单元格

[英]Highlight cells based on date and other conditions

I am trying to code in VBA a highlighting function based on certain conditions. 我试图根据某些条件在VBA中编码突出显示功能。 I can easily do this via conditional formatting, but I have noticed that if a user cuts/pastes (other than special)/deletes rows/etc. 我可以通过条件格式轻松地做到这一点,但是我注意到,如果用户剪切/粘贴(非特殊)/删除行/等。 then the conditional formatting ranges are modified. 然后修改条件格式范围。 I want the conditional formatting ranges to stay fixed rather than mapped to the actual cells. 我希望条件格式设置范围保持固定,而不是映射到实际单元格。 If anyone knows how to do that, or protect the conditional formatting but still allow data manipulation, then this code would be unnecessary. 如果有人知道该怎么做,或者保护条件格式,但仍然允许数据操作,那么此代码将是不必要的。

I have found two different codes that I have been trying but since I'm new to VBA, I'm not very good at it and run into problems. 我发现了两个不同的代码,我一直在尝试,但是由于我是VBA的新手,所以我不太擅长此代码并遇到问题。 I don't know how to use the Isblank or Isempty feature for one. 我不知道如何使用Isblank或Isempty功能。

I need to highlight dates that are earlier than 30 days from now (including dates passed) in red. 我需要用红色突出显示自现在起30天内的日期(包括通过的日期)。 I need to highlight dates that are earlier than 60 days from now but more than 30 in yellow. 我需要以黄色突出显示自现在起60天内的日期,但以黄色突出显示30多个日期。 Cells without data and cells beyond 60 days out must remain unhighlighted. 没有数据的单元格和超过60天的单元格必须保持突出状态。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Private Sub Worksheet_Change(ByVal Target As Range)

Dim icolor As Integer

    If Not Intersect(Target, Range("C3:T65")) Is Nothing Then

        Select Case Target

            Case Is <= Date + 60

                icolor = 6

            Case Is <= Date + 30

                icolor = 3

            Case IsEmpty()

                icolor = 2

        End Select


        Target.Interior.ColorIndex = icolor

    End If

End Sub

Other option: 其他选择:

Sub Highlight()
    Dim cell As Range

    For Each cell In Range("C3:T65")
        If cell.Value <= Date + 60 And cell.Value > Date + 30 Then
            cell.Offset(0, 1).Interior.ColorIndex = 6

        ElseIf cell.Value <= Date + 30 Then
            cell.Offset(0, 1).Interior.ColorIndex = 3

        ElseIf cell.Value IsEmpty() Then
            cell.Offset(0, 1).Interior.ColorIndex = 2

        End If
    Next cell

End Sub

使用VBNullString检查单元格为空:

ElseIf cell.Value = vbNullString Then

Both your codes are almost there. 您的两个代码都差不多了。 The following combination of the two should do the job: 以下两者的结合可以完成任务:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icolor As Integer
    Dim cell As Range

    If Intersect(Target, Range("C3:T65")) Is Nothing Then Exit Sub

    For Each cell In Target
        icolor = 0
        Select Case cell
            Case Is <= Date + 30: icolor = 3
            Case Is <= Date + 60: icolor = 6
            Case "": icolor = 2
        End Select
        If icolor <> 0 Then cell.Interior.ColorIndex = icolor
    Next cell
End Sub

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

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