首页 > 解决方案 > 如何计算 VBA 中非空白的单元格?

问题描述

我遇到了一个错误,我无法让我的 CountIf 工作。在我的“2020 Master RMA”表中,我的 A 列中的日期格式为 m/d/yyyy。我想要发生的情况如下:检查 A 列。如果 A 列符合 1 月标准(即,如果 monthNum = 1),则直接转到 O 列,查看该单元格中是否还有日期。如果该单元格中有日期,则将该日期与该列 O 中日期的其余非空白单元格一起计算。

我希望 O 列的结果进入另一个名为“March Presentations”的表格中的单元格。结果将进入单元格 AE49。

我应该在单元格 AE49 中得到 3 的值,但我只得到 1 的值,在看了几个小时后我仍然无法弄清楚为什么。

Sheets("2020 Master RMA's").Select
lastRow = ActiveSheet.UsedRange.Rows.Count
For row = 2 To lastRow
    monthNum = Month(Worksheets("2020 Master RMA's").Range("A" & row).Value)
    If monthNum = 1 Then
        January = January + 1
        JanuaryQE = Application.WorksheetFunction.CountIfs(Worksheets("2020 Master RMA's").Range("O" & row), "<>""")
 Next

Sheets("March Presentation").Select
Worksheets("March Presentation").Range("AD49").Value = January
Worksheets("March Presentation").Range("AE49").Value = JanuaryQE

标签: excelvbacountif

解决方案


With Worksheets("2020 Master RMA's")
    lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
    For Row = 2 To lastRow
        monthNum = Month(.Range("A" & Row).Value)
        If monthNum = 1 Then
            January = January + 1
            januaryqe = januaryqe + IIf(.Range("O" & Row) <> "", 1, 0)
        End If
     Next
End With
With Worksheets("March Presentation")
    .Range("AD49").Value = January
    .Range("AE49").Value = januaryqe
End With

推荐阅读