MS Excel基于字体颜色选择行

MS Excel基于字体颜色选择行,excel,vba,button,macros,Excel,Vba,Button,Macros,我有大量关于员工合同的数据,创建了一个宏来自动将过期员工行的字体颜色更改为“红色”,并弹出MsgBox来提醒用户过期数据的数量 下面是代码 Sub Worksheet_Activate() Dim startCell As Integer, endCell As Integer Dim column As Integer Dim CountCells As Integer Dim x As Integer With Worksheets("Sheet1") last

我有大量关于员工合同的数据,创建了一个宏来自动将过期员工行的字体颜色更改为“红色”,并弹出MsgBox来提醒用户过期数据的数量

下面是代码

Sub Worksheet_Activate()

  Dim startCell As Integer, endCell As Integer
  Dim column As Integer
  Dim CountCells As Integer
  Dim x As Integer

  With Worksheets("Sheet1")

  lastrow = Range("L1048576").End(xlUp).Row

  CountCells = 0

  For i = 4 To lastrow

      If Range("L" & i).Value <> "" And Now <> "" Then

          If Range("L" & i).Value <= Now Then

              Range("L" & i).Font.ColorIndex = 3

                  If Range("L" & i).Font.ColorIndex = 3 Then

                     CountCells = CountCells + 1

                  End If
          End If
      End If
  Next i

     MsgBox CountCells & " expiring"

  End With
End Sub
子工作表_Activate()
Dim startCell为整数,endCell为整数
将列设置为整数
将单元格计数为整数
作为整数的Dim x
带工作表(“表1”)
lastrow=范围(“L1048576”)。结束(xlUp)。行
CountCells=0
对于i=4到最后一行
如果范围(“L”&i).值为“”,现在为“”,则

如果范围为(“L”&i).Value则无需在单元格中循环。设置条件格式规则,当L列中的日期小于现在时,将字体颜色变为红色。有了它,a可以过滤红色字体单元格

Sub red_font_cells()
    With Worksheets("Sheet1")
        'turn off AutoFilter if it is on
        If .AutoFilterMode Then .AutoFilterMode = False
        'set a CF rule for <Now
        With .Range(.Cells(2, "L"), .Cells(Rows.Count, "L").End(xlUp))
            .FormatConditions.Delete
            With .FormatConditions.Add(Type:=xlExpression, Formula1:="=$L2<NOW()")
                .Font.Color = vbRed
            End With
        End With
        'add an AutoFilter for red font cells
        With .Range(.Cells(1, "L"), .Cells(Rows.Count, "L").End(xlUp))
            .AutoFilter Field:=1, Criteria1:=vbRed, _
                        Operator:=xlFilterFontColor
        End With
        'deal with the red font cells
        With .Range(.Cells(2, "L"), .Cells(Rows.Count, "L").End(xlUp))
            If CBool(Application.Subtotal(103, .Cells)) Then
                With .SpecialCells(xlCellTypeVisible)
                    'select them (there are better ways to get things done)
                    '.Select
                    'copy them to sheet2 (do not need Select for this)
                    .Copy Destination:=Sheet2.Range("A1")
                    'delete them
                    .EntireRow.Delete
                End With
            End If
        End With
        'turn off AutoFilter
        If .AutoFilterMode Then .AutoFilterMode = False
    End With
End Sub
子红色字体单元格()
带工作表(“表1”)
'如果“自动筛选”处于启用状态,请关闭它
如果.AutoFilterMode,则.AutoFilterMode=False

“为yes bro设置CF规则,它正在工作,但不适合我,因为一旦运行,过时的员工直接复制粘贴到第2页并删除。我可以使用我的代码并创建3个按钮吗?(1) 自动选择(2)复制并粘贴(3)删除每个选定行。这让用户对数据有了更多的控制。实际上,我把按钮留给了你。我想像你这样的人会欢迎这个挑战的。我在这里回答的目的是向您展示一种更好的方法来识别并隔离比今天更短的日期。嗯……如果我只是将您的代码从注释“处理红色字体单元格”复制到ActiveX类型按钮上的结束粘贴,它将不会运行。“编译错误:无效或不合格引用”突出显示在“.Cells”上。好的,我将整个代码复制到按钮上,它运行,但它只复制并粘贴在“L”列中的数据,其他(从A到L)不包括在内。这有可能改变吗?看看下面两行。如果要复制整行,请使用
.EntireRow.Copy