简体   繁体   English

计算日期范围内的所有行

[英]Count all rows that are within a date range

I'm attempting to count the number of rows in my sheet that meet 3 sets of criteria: the client name in column C matches my active row;我正在尝试计算工作表中满足 3 组条件的行数: C 列中的客户名称与我的活动行匹配; the due date in column G; G栏中的到期日; and column M is blank (indicating no previous submission was sent).并且 M 列是空白的(表示之前没有发送过提交)。

I can get this to work just fine with the following code:我可以使用以下代码使其正常工作:

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Broker Workflow")

Dim i As Long
Dim iVal As Long

Dim lastRow As Long: lastRow = ws.Range("C" & Rows.Count).End(xlUp).Row
Dim strClient As String: strClient = Cells(ActiveCell.Row, "C").Value
Dim strRenDate As String: strRenDate = Cells(ActiveCell.Row, "G").Value
Dim strNotSubmitted As String: strNotSubmitted = ""

Dim strCriteria As String: strCriteria = strClient & strRenDate & strNotSubmitted

iVal = 0
    
    For i = 8 To lastRow
        If ws.Range("C" & i).Value & ws.Range("G" & i).Value & ws.Range("M" & i).Value = strCriteria Then
            iVal = iVal + 1
        End If
   Next i
   
Dim strCount As String: strCount = iVal

My problem is that now I want to extend this to count all rows with a due date that is within a range of my active row date +/- 7 days (14 day range).我的问题是,现在我想扩展它以计算到期日期在我的活动行日期 +/- 7 天(14 天范围)范围内的所有行。 So if my due date is 07/06/2020 it will count the number of rows that match my client name in C, have blank cell in M and a date of anything between 01/06/2020-14/06/2020 in G.因此,如果我的截止日期是 2020 年 7 月 6 日,它将计算在 C 中与我的客户名称匹配的行数,在 M 中具有空白单元格,在 G 中具有介于 01/06/2020-14/06/2020 之间的任何日期.

You are making it more complicated than needed... can get rid of the four variables above, and simply test like this:你让它比需要的更复杂......可以摆脱上面的四个变量,并像这样简单地测试:

For i = 8 To lastRow
    If ws.Range("C" & i).Value = Cells(ActiveCell.Row, "C").Value & _
        ws.Range("M" & i).Value = "" & _
        ws.Range("G" & i).Value >= DateAdd(Cells(-7, "d", ActiveCell.Row, "G").Value) & _
        ws.Range("G" & i).Value <= DateAdd(Cells(7, "d", ActiveCell.Row, "G").Value) Then
         iVal = iVal + 1
    End If
Next i

[EDIT] Sorry. [编辑]对不起。 I have no idea what I wrote earlier.我不知道我之前写了什么。

I mixed up the parameters in DateAdd and used & instead of and我混合了 DateAdd 中的参数并使用&而不是and

This works, tested:这有效,经过测试:

For i = 8 To lastRow
        If Cells(i, "C").Value = Cells(ActiveCell.Row, "C").Value And _
            Cells(i, "M").Value = "" And _
            Cells(i, "G").Value >= DateAdd("d", -7, Cells(ActiveCell.Row, "G").Value) And _
            Cells(i, "G").Value <= DateAdd("d", 7, Cells(ActiveCell.Row, "G").Value) Then
             iVal = iVal + 1
        End If
Next i

It will also work with your ws.Range syntax, it was just simpler for me to test it like this with Cells Please note that the current line also gets counted if it has an empty M column...它也适用于您的ws.Range语法,对我来说使用Cells测试它更简单 请注意,如果当前行有一个空的 M 列,它也会被计算在内...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM