Countifs无法计算日期

时间:2018-03-01 14:26:25

标签: excel vba

我有一系列的countifss应该计算列中特定单词的实例,其中另一列在两个日期之间(一周的开始和结束)。感谢用户BruceWayne的输入,所有内容都编译并运行,但是countifss用0填充我的表。以下是相关部分:

Dim thisBook As Workbook
Dim sSheet As Worksheet, rSheet As Worksheet
Dim outputRng As Range
Dim rowCount As Integer, i As Integer
Dim thisWeek As Date
Dim rSheetVal As Long

Set thisBook = ActiveWorkbook
Set rSheet = thisBook.Sheets("*** Report")
Set sSheet = thisBook.Sheets("Unique ***s")

thisWeek = Date - Weekday(Date, 2) + 1
rSheet.Range("S14").Value2 = (rSheet.Range("S21").Value2 - 49)
rSheet.Range("S15").Value2 = (rSheet.Range("S21").Value2 - 42)
rSheet.Range("S16").Value2 = (rSheet.Range("S21").Value2 - 35)
rSheet.Range("S17").Value2 = (rSheet.Range("S21").Value2 - 28)
rSheet.Range("S18").Value2 = (rSheet.Range("S21").Value2 - 21)
rSheet.Range("S19").Value2 = (rSheet.Range("S21").Value2 - 14)
rSheet.Range("S20").Value2 = (rSheet.Range("S21").Value2 - 7)
rSheet.Range("S21").Value2 = (thisWeek + 6)

For i = 14 To 21
    rSheetVal = rSheet.Cells(i, "S").Value2
    Set outputRng = rSheet.Cells(i, "T")
    With sSheet
        outputRng = Application.WorksheetFunction.CountIfs( _
            .Range("K2", "K" & .Cells(Rows.Count, "K").End(xlUp).Row), "APPROVED", _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), ">=" & (rSheetVal - 6), _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), "<=" & (rSheetVal))
    End With
    Set outputRng = rSheet.Cells(i, "U")
    With sSheet
        outputRng = Application.WorksheetFunction.CountIfs( _
            .Range("K2", "K" & .Cells(Rows.Count, "K").End(xlUp).Row), "REJECTED", _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), ">=" & (rSheetVal - 6), _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), "<=" & (rSheetVal))
    End With
    Set outputRng = rSheet.Cells(i, "V")
    With sSheet
        outputRng = Application.WorksheetFunction.CountIfs( _
            .Range("K2", "K" & .Cells(Rows.Count, "K").End(xlUp).Row), "DRAFT", _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), ">=" & (rSheetVal - 6), _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), "<=" & (rSheetVal))
    End With
    Set outputRng = rSheet.Cells(i, "W")
    With sSheet
        outputRng = Application.WorksheetFunction.CountIfs( _
            .Range("K2", "K" & .Cells(Rows.Count, "K").End(xlUp).Row), "ONHOLD", _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), ">=" & (rSheetVal - 6), _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), "<=" & (rSheetVal)) + _
        Application.WorksheetFunction.CountIfs( _
            .Range("K2", "K" & .Cells(Rows.Count, "K").End(xlUp).Row), "REVIEWED", _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), ">=" & (rSheetVal - 6), _
            .Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), "<=" & (rSheetVal))
    End With
Next i

这是sSheet的样子:

    K               M
REVIEWED  ...  28 Feb 2018
APPROVED  ...  28 Feb 2018
REVIEWED  ...  27 Feb 2018
APPROVED  ...  26 Feb 2018
DRAFT     ...  26 Feb 2018

rSheet是带有表格和图表的工作表,sSheet是一个工作表列表。该表在这个和最后7个星期结束时正确构建,所以没关系,但是在宏运行之后我的表只是满0。

我错过了正确计算这些日期的内容吗?

0 个答案:

没有答案