简体   繁体   English

Excel公式:计算值为日期的单元格

[英]Excel Formula: Count cells where value is date

I'm looking for a formula to run a COUNTIF (or similar) on a range of cells, and where the contained value is a date, to increment the counter - essentially something like: 我正在寻找一个公式来在一系列单元格上运行COUNTIF(或类似的),并且包含的​​值是一个日期,以增加计数器 - 基本上类似于:

=COUNTIF(range, if_date())

What I haven't been able to find is a logical test for the if_date() part of the question. 我无法找到的是对问题的if_date()部分的逻辑测试。 Is there a way to test a cell to check whether its content is a date? 有没有办法测试一个单元格来检查它的内容是否是一个日期?

This is difficult with worksheet functions because dates in excel are simply formatted numbers - only CELL function lets you investigate the format of a cell (and you can't apply that to a range, so a helper column would be required).......or, if you only have dates and blanks.....or dates and text then it would be sufficient to use COUNT function, ie 这对于工作表函数来说很困难,因为excel中的日期只是格式化的数字 - 只有CELL函数可以让你研究一个单元格的格式(你不能将它应用到一个范围,所以需要一个辅助列).... ...或者,如果你只有日期和空白.....或日期和文本,那么使用COUNT函数就足够了,即

=COUNT(range)

That counts numbers so it won't be adequate if you want to distinguish dates from numbers. 这会对数字进行计数,因此如果您想将日期与数字区分开来,这将是不够的。 If you do then the number range could be utilised, eg if you have numbers in a range and dates but the numbers will all be lower than 10,000 and the dates will all be relatively recent then you could use this version to exclude the numbers 如果你这样做,那么可以使用数字范围,例如,如果你有一个范围和日期的数字,但数字都将低于10,000,日期将相对较新,那么你可以使用这个版本来排除数字

=COUNTIF(range,">10000")

Here's my solution. 这是我的解决方案。 If your cells will contain only dates or blanks, just compare it to another date. 如果您的单元格仅包含日期或空格,只需将其与另一个日期进行比较即可。 If the cell can be converted to date, it will be counted. 如果可以将单元格转换为日期,则会对其进行计数。

=COUNTIF(C:C,">1/1/1900")
## Counts all the dates in column C

Caution, cells with numbers will be counted. 注意,将计算具有数字的单元格。

Here's one approach. 这是一种方法。 Using a combination of the answers above do the following: 使用上述答案的组合执行以下操作:

  1. Convert cell to text using a predefined format 使用预定义格式将单元格转换为文本
  2. Try using DATEVALUE to convert it back to a date 尝试使用DATEVALUE将其转换回日期
  3. Exclude any cells where DATEVALUE returns an error 排除DATEVALUE返回错误的所有单元格

As a formula, just use the example below with <> replaced with your range reference. 作为公式,只需使用下面的示例,将<>替换为您的范围参考。

=SUM(IF(ISERROR(DATEVALUE(TEXT(<<RANGE HERE>>, "MM/dd/yyyy"))), 0, 1))

You must enter this as an array formula with CTRL + SHIFT + ENTER. 您必须使用CTRL + SHIFT + ENTER输入此数组公式。

This assumes that the column of potential date values is in column A. You could do something like this in an adjacent column: 这假设潜在日期值列在A列中。您可以在相邻列中执行以下操作:

Make a nested formula that converts the "date" to its numeric value if it's valid, or an error value to zero if it's not. 制作一个嵌套公式,如果它有效,则将“date”转换为数值;如果不是,则将错误值转换为零。
Then it converts the valid numeric values to 1's and leaves the zeroes as they are. 然后它将有效数值转换为1并按原样保留零。
Then sum the new column to get the total number of valid dates. 然后将新列相加以获得有效日期的总数。

=IF(IFERROR(DATEVALUE(A1),0)>0,1,0) = IF(IFERROR(DATEVALUE(A1),0)> 0,1,0)

To count numbers or dates that meet a single test (such as equal to, greater than, less than, greater than or equal to, or less than or equal to), use the COUNTIF function. 要计算满足单个测试的数字或日期(例如等于,大于,小于,大于或等于,或小于或等于),请使用COUNTIF函数。 In Excel 2007 and later, to count numbers or dates that fall within a range (such as greater than 9000 and at the same time less than 22500), you can use the COUNTIFS function. 在Excel 2007及更高版本中,要计算范围内的数字或日期(例如大于9000且同时小于22500),可以使用COUNTIFS函数。 If you are using Excel 2003 or earlier, you can use the SUMPRODUCT function to count the numbers that fall within a range (COUNTIFS was introduced in Excel 2007). 如果您使用的是Excel 2003或更早版本,则可以使用SUMPRODUCT函数来计算范围内的数字(COUNTIFS是在Excel 2007中引入的)。

please see more 请看更多

There is no interactive solution in Excel because some functions are not vector-friendly, like CELL , above quoted. Excel中没有交互式解决方案,因为某些函数不是向量友好的,如上面引用的CELL For example, it's possible counting all the numbers whose absolute value is less than 3, because ABS is accepted inside a formula array. 例如,可以计算绝对值小于3的所有数字,因为ABS在公式数组中被接受。

So I've used the following array formula ( Ctrl+Shift+Enter after edit with no curly brackets) 所以我使用了以下数组公式(编辑后没有花括号的Ctrl + Shift + Enter

             ={SUM(IF(ABS(F1:F15)<3,1,0))}

If Column F has 如果F列

                   F
          1 ...    2
          2 ....   4
          3 ....  -2
          4 ....   1
          5 .....  5

It counts 3! 它算3! (-2,2 and 1). (-2,2和1)。 In order to show how ABS is array-friendly function let's do a simple test: Select G1:G5 , digit =ABS(F1:F5) in the formula bar and press Ctrl+Shift+Enter . 为了展示ABS是如何对阵列友好的功能,让我们做一个简单的测试:在公式栏中选择G1:G5 ,digit = ABS(F1:F5) ,然后按Ctrl + Shift + Enter It's like someone write Abs(F1:F5)(1), Abs(F1:F5)(2) , etc. 这就像有人写Abs(F1:F5)(1),Abs(F1:F5)(2)等。

                   F    G
          1 ...    2  =ABS(F1:F5) => 2 
          2 ....   4  =ABS(F1:F5) => 4
          3 ....  -2  =ABS(F1:F5) => 2
          4 ....   1  =ABS(F1:F5) => 1
          5 .....  5  =ABS(F1:F5) => 5

Now I put some mixed data, including 2 date values. 现在我放了一些混合数据,包括2个日期值。

                   F
          1 ...    Fab-25-2012
          2 ....   4
          3 ....   May-5-2013
          4 ....   Ball
          5 .....  5

In this case, CELL fails and return 1 在这种情况下, CELL失败并返回1

             ={SUM(IF(CELL("format",F1:F15)="D4",1,0))}

It happens because CELL return the format of first cell of the range. 这是因为CELL返回范围的第一个单元格的格式。 ( D4 is a mdy format) D4是mdy格式)

So the only thing left is programming! 所以剩下的就是编程! A UDF(User defined Function) for formula array must return a variant array: 公式数组的 UDF(用户定义函数)必须返回变量数组:

Function TypeCell(R As Range) As Variant
Dim V() As Variant
Dim Cel As Range
Dim I As Integer
Application.Volatile '// For revaluation in interactive environment
ReDim V(R.Cells.Count - 1) As Variant
I = 0
For Each Cel In R
   V(I) = VarType(Cel) '// Output array has the same size of input range.
   I = I + 1
Next Cel
TypeCell = V
End Function

Now is easy (the constant VbDate is 7): 现在很简单(常量VbDate为7):

             =SUM(IF(TypeCell(F1:F5)=7,1,0))  

It shows 2. That technique can be used for any shape of cells. 它显示2.该技术可用于任何形状的细胞。 I've tested vertical, horizontal and rectangular shapes, since you fill using for each order inside the function. 我已经测试了垂直,水平和矩形形状,因为你在函数内填写了每个订单。

A bit long winded but it works for me: try this:: 有点长的啰嗦,但它对我有用:试试这个::

=SUM(IF(OR(ISBLANK(AU2), NOT(ISERR(YEAR(AU2)))),0,1)
 +IF(OR(ISBLANK(AV2), NOT(ISERR(YEAR(AV2)))),0,1))

first part of if will allow cell to be blank or if there is something in the cell it tries to convert to a year, if there is an error or there is something other than a date result = 1, do the same for each cell and sum the result if的第一部分将允许单元格为空白,或者如果单元格中有某些内容尝试转换为一年,如果存在错误或者除了日期结果= 1之外的其他内容,则对每个单元格执行相同操作总结结果

Here is how I was able to trick Excel to count expired certifications in a list. 以下是我如何欺骗Excel来计算列表中过期的认证。 I didn't have a set date, or date range, just current date. 我没有设定日期或日期范围,只有当前日期。 "TODAY()" doesn't work in these for Excel 2013. It sees it as text or condition, not the date value. “TODAY()”在Excel 2013中不起作用。它将其视为文本或条件,而不是日期值。 So these previous didn't work for me. 所以这些以前对我没用。 So the word problem/scenario: How many people are expired in this list? 所以单词问题/场景:这个列表中有多少人过期了?

Use: =IFERROR(D5-TODAY(),0) Where D5 is the date to be interrogated. 使用:= IFERROR(D5-TODAY(),0)其中D5是要被询问的日期。

Then use: =IF(J5>=1,1,0) Where J5 is the cell where the first equation is producing either a positive or negative number. 然后使用:= IF(J5> = 1,1,0)其中J5是第一个等式产生正数或负数的单元格。 This set, I have hidden on the side of the visible sheet, then I just sum the total for the number of unexpired members. 这个集合,我隐藏在可见表单的一侧,然后我只是总计未过期成员的数量。

Total number of cells in a range minus the blank cells of the same range. 范围内的单元总数减去相同范围的空白单元格。

=(115 - (COUNTBLANK(C2:C116))) =(115 - (COUNTBLANK(C2:C116)))

This counts everything in the range so, maybe not what you're looking for. 这会计算范围内的所有内容,也许不是您要查找的内容。

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

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