[英]Excel VBA Countifs with wildcard criteria - for dates
I would like to know if I can use a wildcard for dates? 我想知道是否可以对日期使用通配符吗?
I have dates with time stored on A1:A5 and I want to count how many items are 我将日期和时间存储在A1:A5上,我想计算多少个项目
there with criteria "8/12/2015" regardless of the time, the output compl should be 2 as A1 and A4 contains the date "8/12/2015" 无论时间如何,条件为“ 8/12/2015”,输出compl应该为2,因为A1和A4包含日期“ 8/12/2015”
A1 = 8/12/2015 12:00 AM A1 = 2015年8月12日上午12:00
A2 = 8/25/2015 3:00 PM A2 = 2015年8月25日下午3:00
A3 = 8/23/2015 4:00 AM A3 = 8/23/2015 4:00 AM
A4 = 8/12/2015 1:30 AM A4 = 8/12/2015 1:30 AM
A5 = 8/20/2015 12:00 AM A5 = 8/20/2015 12:00 AM
Sub counter()
With Sheet1
Dim compl As String
Dim xdate As String, xdate2 As String
xdate = "08/12/2015"
xdate2 = xdate & "*"
compl = WorksheetFunction.CountIfs(Range("a1:a5"), xdate2)
Debug.Print compl
End With
End Sub
Make sure your date is a date, not a string. 确保您的日期是日期,而不是字符串。
Make sure to use ISO standard formats, not some random date order. 确保使用ISO标准格式,而不是某些随机的日期顺序。
Dim xdate As date
xdate = "2015-08-12"
compl = WorksheetFunction.CountIfs(Range("a1:a5"), ">=" & xdate, _
Range("a1:a5"), "<" & (xdate + 1))
Also, you don't need VBA for this, simply use the Excel's COUNTIFS function like this: 另外,您不需要VBA,只需使用Excel的COUNTIFS函数,如下所示:
=COUNTIFS(a1:a5,">=2015-08-28",a1:a5,"<2015-08-29")
Edit For me, adding the Format function was necessary to keep the date in a human format for Excel to understand. 编辑对于我来说,添加格式功能对于将日期保持为人为格式以便Excel理解是必要的。 Change the Worksheetfunction line like this, if you want to make sure it works:
如果要确保它可以正常工作,请像这样更改Worksheetfunction行:
WorksheetFunction.CountIfs(Range("a1:a5"), ">=" & Format(xdate, "yyyy-mm-dd"), _
Range("a1:a5"), "<" & Format(xdate + 1, "yyyy-mm-dd"))
In Excel the date are stored as numbers since 1/1/1970. 在Excel中,日期自1970年1月1日起以数字形式存储。 this means that the date you wrote is 42239. When the the 2 decimal digits are the hours.
这表示您写的日期是42239。当两位小数点是小时时。 sou you can use the function
round
(with 0 digits) when this number will include all the cells in the specific date. 因此,当此数字将包含特定日期的所有单元格时,您可以使用
round
功能(带有0位数字)。
如果不必在VBA和countif中使用(我看不到目标是什么):= sumproduct((floor(value(A1:A5),1)= date(2015,8,20))* 1)否则会有周期等选项,但是如果没有目的,很难指出正确的方向
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.