VBA-日期范围内的计数数据

VBA-日期范围内的计数数据,vba,excel,Vba,Excel,我正在编写一个脚本,在这个脚本中,如果数据副本是>3,我可以从一列中计算数据,我将对其进行标记 我的问题是,我需要放置一个条件语句,以便在1个月的日期范围内计算数据 示例输入文件::(mm/dd/yyyy) 示例输出文件: Column A | Column B | Column C| Column D 023 | 1/2/2016 | | 023 | 1/3/2016 | | 023 | 1/4/2016 | 1

我正在编写一个脚本,在这个脚本中,如果数据副本是
>3
,我可以从一列中计算数据,我将对其进行标记

我的问题是,我需要放置一个条件语句,以便在1个月的日期范围内计算数据

示例输入文件::(mm/dd/yyyy)

示例输出文件:

Column A | Column B | Column C| Column D
023      | 1/2/2016 |         |   
023      | 1/3/2016 |         |    
023      | 1/4/2016 |   1     |    3
024      | 2/1/2016 |         |
024      | 3/1/2016 |         |
024      | 4/1/2016 |         |
如果重复数据不在月份范围内,则不会对其进行标记

如果数据大于3,并且
列B
中的所有数据的日期在一个月内,则代码要做的是计算
列A
中的数据,并从
列D
E
中标记它,不是所有行,而是
列B
中的最近日期

我的代码所做的是从
列A
中计算数据,如果数据是
>3
则从
列C
D
中从列B的最近日期开始标记

我的代码:

注意:

  • 在我的代码中,我没有使用
    A
    B
    CD而是
    t
    CG
    CH
    CI
我不知道如何将其范围扩大到一个月,我尝试了收集,但仍然是VBA的新手,我不熟悉它,我不知道它是否正确

此输入

已生成此输出

从这个代码

Option Explicit

Sub main()
Dim iLoop As Long, jLoop As Long
Dim lastRow As Long, countRow As Long
Dim myDate1 As Variant, myDate2 As Variant

    lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row

    For iLoop = 1 To lastRow
        countRow = Application.CountIf(Sheet1.Range(Sheet1.Cells(iLoop, 1), Sheet1.Cells(lastRow, 1)), Sheet1.Cells(iLoop, 1))
        If countRow > 2 Then
            For jLoop = lastRow To (iLoop + 1) Step -1
                If Sheet1.Cells(jLoop, 1).Value = Sheet1.Cells(iLoop, 1).Value Then
                    myDate1 = Application.EDate(Sheet1.Cells(iLoop, 2), 3)
                    myDate2 = Sheet1.Cells(jLoop, 2)
                    If myDate2 > myDate1 Then Sheet1.Cells(jLoop, 3).Resize(1, 2) = Array("1", "3")
                    Exit For
                End If
            Next jLoop
        End If
    Next iLoop
End Sub
具体地说,使用工作表函数EDate将给定项目编号的第一个日期加上三个月

此外,随着循环计数器iLoop的进行,工作表函数CountIf使用的列表的大小也会缩短


另外,在代码片段中,您使用了
i1
作为循环计数器。这很容易与
il
混淆。8)

已编辑:仅根据上次OP的规格和公式更正留下代码

你可以试试这个

Sub sbFindDuplicatesInColumn_C3ter()

With ThisWorkbook.Worksheets("duplicates")   '<~~ you should know what workbook and worksheet you are on!!
    With .Range("T1").Resize(.Range("T" & .Rows.Count).End(xlUp).Row) ' the "base" column is column "T"
        With .Offset(, 67) ' column "CI" is 67 columns away from column "T"
            .FormulaR1C1 = "=IF(COUNTIFS(C20, RC20, C72,""<="" & EOMONTH(RC72,0), C72,"">="" & EOMONTH(RC72,-1)+1 )>2,    IF(COUNTIFS(C20, RC20,C72,"">"" &RC72,C72,""<="" & EOMONTH(RC72,0))=0,                   3   , """")      , """")" ' substituted relative references with absolute ones : column "T" has index 20, column "BT" has index 72
            .Value = .Value '<== if you want to get rid of formulas
        End With
        With .Offset(, 66) ' column "CH" is 66 columns away from column "T"
            .FormulaR1C1 = "=IF(RC[1]>0, 1, """") " ' I left relative references since columnn "CH" is always one left of column "CG" as was for columns "A" and "B"
            .Value = .Value '<== if you want to get rid of formulas
         End With
   End With
End With

End Sub
Sub-sbFindDuplicatesInColumn_C3ter()

使用此工作簿。工作表(“副本”)“我的Excel将日期解释为m/d/yyyy。您的解释似乎是d/m/yyyy。它显示的日期是“每月”,应该是1个月或30天(如果您不需要月,但需要30天的周期,则不使用EDate,只需添加30*月数。如果您需要2个月,则添加60。
myDate1=Sheet1.Cells(iLop,2)+60
mydate2的目的是什么?它起作用了,但是如果我的日期是1月1日1月2日1月3日2月4日,那么将被标记的是feb4而不是1月3日,我想知道为什么?谢谢你把你的公式去掉!出于好奇,如果我要更改列
A
B
C
D
T
CG
CG
CH
,请参见编辑的代码以满足“好奇心”。如果我完成了你的问题,请将我的答案标记为已接受。谢谢你,你可以轻松快速地找到它。阅读我留下的评论,让你处理参考列中的任何可能更改,它们之间有“T”列的索引20,“CG”列的索引85。因此,如果你需要将“CG”更改为“BT”你必须用你想要指向的任何新列的索引列替换每一次出现的85。我刚刚注意到,在列
CI
中,它返回它计数的值,例如,如果有5个重复的it标记为5,我如何将其转换为像“3”这样的特定值
Option Explicit

Sub main()
Dim iLoop As Long, jLoop As Long
Dim lastRow As Long, countRow As Long
Dim myDate1 As Variant, myDate2 As Variant

    lastRow = Sheet1.Range("A" & Rows.Count).End(xlUp).Row

    For iLoop = 1 To lastRow
        countRow = Application.CountIf(Sheet1.Range(Sheet1.Cells(iLoop, 1), Sheet1.Cells(lastRow, 1)), Sheet1.Cells(iLoop, 1))
        If countRow > 2 Then
            For jLoop = lastRow To (iLoop + 1) Step -1
                If Sheet1.Cells(jLoop, 1).Value = Sheet1.Cells(iLoop, 1).Value Then
                    myDate1 = Application.EDate(Sheet1.Cells(iLoop, 2), 3)
                    myDate2 = Sheet1.Cells(jLoop, 2)
                    If myDate2 > myDate1 Then Sheet1.Cells(jLoop, 3).Resize(1, 2) = Array("1", "3")
                    Exit For
                End If
            Next jLoop
        End If
    Next iLoop
End Sub
Sub sbFindDuplicatesInColumn_C3ter()

With ThisWorkbook.Worksheets("duplicates")   '<~~ you should know what workbook and worksheet you are on!!
    With .Range("T1").Resize(.Range("T" & .Rows.Count).End(xlUp).Row) ' the "base" column is column "T"
        With .Offset(, 67) ' column "CI" is 67 columns away from column "T"
            .FormulaR1C1 = "=IF(COUNTIFS(C20, RC20, C72,""<="" & EOMONTH(RC72,0), C72,"">="" & EOMONTH(RC72,-1)+1 )>2,    IF(COUNTIFS(C20, RC20,C72,"">"" &RC72,C72,""<="" & EOMONTH(RC72,0))=0,                   3   , """")      , """")" ' substituted relative references with absolute ones : column "T" has index 20, column "BT" has index 72
            .Value = .Value '<== if you want to get rid of formulas
        End With
        With .Offset(, 66) ' column "CH" is 66 columns away from column "T"
            .FormulaR1C1 = "=IF(RC[1]>0, 1, """") " ' I left relative references since columnn "CH" is always one left of column "CG" as was for columns "A" and "B"
            .Value = .Value '<== if you want to get rid of formulas
         End With
   End With
End With

End Sub