对所有人来说,美好的一天,目前我仍然面临着我的老板任务的问题,创建一个MS excel宏。
现在面临的问题是
我曾尝试过很多来自互联网资源的代码,但结果仍然相同,它无法运行代码或计算的数量为0.
任何人都有任何建议或解决方案。谢谢。
以下是2个错误编码
此编码仅显示0数量的过期数据。
CountedAmount = Application.WorksheetFunction.CountIf(Range(" L4:L1048576")," Red")
此编码不会运行,警告显示运行时错误1004应用程序定义或对象定义的错误
CountedAmount = Application.WorksheetFunction.CountIf(Range(" L4:xlUp")," Red")
这些是我宏观上的完整编码。
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
答案 0 :(得分:0)
如果您想计算红色的细胞数量,请使用以下内容:
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以使您的Cell和列与列匹配。此循环当前从L4到L100搜索
你必须找出你正在使用的红色的哪种颜色指数。您可能必须使用.Color而不是.ColorIndex,它使用RGB代码。