MS excel宏自动计数功能

MS excel macro auto count function

大家好,目前我仍然面临老板任务的问题,即创建一个 MS excel 宏。

现在面临的问题是

我试了很多网上的代码,结果还是一样,无法运行代码或者计数为0。

任何人有任何建议或解决方案。谢谢。

以下是2个错误编码

这些是我的宏的完整代码。

Sub Worksheet_Activate()

Dim CountedAmount As Integer

With Worksheets("Sheet1")

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

'This codding will only display 0 amount of outdated data.
CountedAmount = Application.WorksheetFunction.CountIf(Range("L4:L1048576"),     "Red")

'This codding will not running, warning show up Run-time error 1004  Application-defined or object-defined eror
'CountedAmount = Application.WorksheetFunction.CountIf(Range("L4:xlUp"), "Red")

For i = 4 To lastrow

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

    If Range("L" & i).Value <= Now Then
        MsgBox CountedAmount & " expiring"          
        Range("L" & i).Font.ColorIndex = 3

    End If
End If
Next i
End With
End Sub

如果你想计算红色细胞的数量,那么使用这样的东西:

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

startCell = 4
endCell = 100

column = 12 'Column L

CountCells = 0

Dim i As Integer

For i = startCell To endCell Step 1

    If Cells(i, column).Interior.ColorIndex = 3 Then

        CountCells = CountCells + 1
    End If
Next i

调整 startCell 和 endCell 以使您的单元格和列与您的列匹配。此循环目前从 L4 搜索到 L100

而且你必须找出你使用的红色有哪个colorindex。您可能必须使用 .Color 而不是 .ColorIndex,后者使用 RGB 代码。