2016-03-31 53 views
-1

好日子,所有人,目前我仍然面临着我的老板任务的问题,创建一个MS Excel宏。MS excel宏自动计数功能

问题现在面临的是

  • 自动计数的消息框,当用户打开的工作表过时的数据并显示。

我曾试图从互联网资源这么多的代码,但结果还是一样,它不能运行代码或计数数为0

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

下面是2错误作弄

  • 这作弄将只显示0过时的数据量。

    CountedAmount = Application.WorksheetFunction.CountIf(范围( “L4:L1048576”), “红”)

  • 这在作弄我不会跑,预警秀运行时错误1004应用程序定义或对象定义的错误

    CountedAmount = Application.WorksheetFunction.CountIf(范围( “L4:xlUp”), “红”)

这些都是我的微距完整的作弄。

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

如果单元格的值为“红色”Application.WorksheetFunction.CountIf(范围(“L4:L1048576”),“红色”),但如果要计算红色的单元格数量,你必须找到其他的东西 – gizlmo

回答

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到手机和列匹配您的列。此循环当前搜索从L4到L100

而且您必须找出您正在使用的红色是哪种颜色索引。您可能必须使用.Color而不是.ColorIndex,它使用RGB代码。

+0

thx作答复,但在你的编码部分提供的编码中加入,它不工作,他计数仍然是0,由于评论框有限的字符空间,我可以用信息吗?顺便说一句,我知道如何开始向用户发送消息,我在这里是新的.z – Han

+0

请告诉我过时的单元格是如何标记的,如果您只是复制这些代码,colorindex可能是错误的。如果它们是彩色的,告诉我Color或Colorindex(单击Cell并使用Debug.Print ActiveCell.Interior.Color/ActiveCell.Interior。ColorIndex查看调试窗口中的值) – gizlmo

+0

>>> Font.ColorIndex = 3是的,它与我以前的编码相同。根据我发布的编码,过期的合约到期日与今日的日期相比,所以如果到期日=>今日考虑过时,则正面颜色将变为红色。 – Han