MSexcel宏自动计数功能,结果显示在window弹出

MS excel macro auto count function, result display in window pop up

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

问题依旧:

在上一个问题上,我已经问了一些解决方案,并将这些建议编码与我原来的编码结合起来,但结果也是一样的,即使有过时的员工合同,消息仍然弹出为 0。

下面是你的建议和我的原创编码的结合...请看一下。

以下是您的建议和我的原始编码的组合...请查看并随时发表评论,让我知道哪里出了问题,谢谢。我尽快需要它..

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



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

        End If
    End If
Next i

    column = 12 'Column L

    startCell = 4
    endCell = xlUp

    CountCells = 0



    For x = startCell To endCell Step 1

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

        CountCells = CountCells + 1 


    End If
Next x

    MsgBox CountCells & " expiring"

End With
End Sub

使用With...End With时,所有属于With子句的对象都应该加上.(句号)

例如

With Worksheets("Sheet1")
    lastrow = Range("L1048576").End(xlUp).Row

应该是

With Worksheets("Sheet1")
    lastrow = .Range("L1048576").End(xlUp).Row

进行修复,看看是否有帮助。如果仍然无效,请使用您当前的代码更新您的问题。

为什么不使用相同的 lastrow 而不是创建 endCell 这将确保代码在相同的值范围内 运行。

您也可以将 endCell 更改为

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

我认为 xlUp 本身行不通。

编辑:

Sub Worksheet_Activate()

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



With Worksheets("Sheet1")

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



For i = 4 To lastrow

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

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

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

        End If
    End If
Next i

column = 12 'Column L

startCell = 4

CountCells = 0


For x = startCell To lastrow Step 1

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

        CountCells = CountCells + 1

    End If

Next x

MsgBox CountCells & " expiring"

End With
End Sub

问题已解决,以下是正确/可用的编码。 谢谢大家,只有我能一直测试和修改codding。

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