Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何使用COUNTIFS VBA函数计算两个日期之间的值?_Excel_Vba - Fatal编程技术网

Excel 如何使用COUNTIFS VBA函数计算两个日期之间的值?

Excel 如何使用COUNTIFS VBA函数计算两个日期之间的值?,excel,vba,Excel,Vba,我想使用VBA计算两个给定日期之间出现值的次数。输出应显示1 我正在使用以下代码,但似乎无法获得正确的值 Sub clientIntAnalysis() Dim r As Integer Dim startdate As Date, endDate As Date startdate = "07/01/2019" endDate = "07/30/2019" Dim LastCol As Long, LastRow As Long, rng As Range, rng2 As Range

我想使用VBA计算两个给定日期之间出现值的次数。输出应显示1

我正在使用以下代码,但似乎无法获得正确的值

Sub clientIntAnalysis()
Dim r As Integer
Dim startdate As Date, endDate As Date
startdate = "07/01/2019"
endDate = "07/30/2019"
    Dim LastCol As Long, LastRow As Long, rng As Range, rng2 As Range

    LastRow = Sheet3.Range("M" & Sheet3.Rows.Count).End(xlUp).Row
    LastCol = Sheet3.Cells(8, Columns.Count).End(xlToLeft).Column

    With Sheet3
        Set rng = .Range(.Cells(8, 14), .Cells(LastRow, LastCol))
        Set rng2 = .Range(.Cells(8, 13), .Cells(LastRow, LastCol))
    End With

    r = Application.WorksheetFunction.CountIfs(rng, ">=" & startdate, rng, "<=" & endDate, rng, "=" & "Client Interested") 'q3

    MsgBox r


End Sub
使用未经测试的代码

现在,您的范围与您在评论中所说的相同:

Sub clientIntAnalysis()

Dim r As Integer
Dim startdate As Date, endDate As Date
startdate = "07/01/2019"
endDate = "07/30/2019"


    Dim LastCol As Long, LastRow As Long, rng As Range, rng2 As Range

    LastRow = Sheet3.Range("M" & Sheet3.Rows.Count).End(xlUp).Row
    'LastCol = Sheet3.Cells(8, Columns.Count).End(xlToLeft).Column

    With Sheet3
        Set rng = .Range(.Cells(8, 13), .Cells(LastRow, 14))
        Set rng2 = .Range(.Cells(8, 14), .Cells(LastRow, 15))
    End With

    r = Application.WorksheetFunction.CountIfs(rng, ">=" & startdate, rng, "<=" & endDate, rng2, "=" & "Client Interested") 'q32

    MsgBox r


End Sub

注释:=COUNTIFSclientmenu$M$8:$N$8,>=&2019年1月7日,客户端菜单$M$8:$N$8,这是我的代码的工作版本,看起来我必须将最后一列的值增加1

Sub clientIntAnalysis()

Dim LastCol As Long, LastRow As Long, rng As Range, rang2 As Range, lastcol2 As Long, r as long

startdate = "07/01/2019"
enddate = "07/30/2019"
LastRow = Sheet3.Range("M" & Sheet3.Rows.Count).End(xlUp).Row
LastCol = Sheet3.Cells(8, Columns.Count).End(xlToLeft).Column
lastcol2 = Sheet3.Cells(8, Columns.Count).End(xlToLeft).Column + 1

Set rang2 = Sheet3.Range(Sheet3.Cells(8, 14), Sheet3.Cells(lastrow, lastcol2))
    With Sheet3
        Set rng = .Range(.Cells(8, 13), .Cells(LastRow, LastCol))
End With

r= Application.WorksheetFunction.CountIfs(rng, ">=" & startdate, rng, "<=" & enddate, rang2, "Client Interested") 'q3

    MsgBox r


End Sub

您的CountIfs函数是否在电子表格上工作?你能试试吗?@Mikku是的,这是公式版本,=COUNTIFSclientmenu$M$8:$N$8,>=&2019年1月7日,客户端菜单$M$8:$N$8,您在代码中为感兴趣的客户使用了错误的范围,应该是RNG2A,因此,即使在您的电子表格公式中,您只比较了2个单元格8到8,请查看范围,你会know@Mikku我已声明LastRow和LastCol在添加新数据时进行相应调整,我接受这一回答,因为它确实产生了我想要的输出结果