简体   繁体   English

Excel:将函数应用于公式中的每个单元格

[英]Excel: Apply function to each cell inside formula

I am trying to devise a non-vba Excel function which checks each cell in multiple ranges for different parameters and then returns the number of applicable occurrences. 我正在尝试设计一个非vba Excel函数,该函数将检查多个范围中的每个单元格的不同参数,然后返回适用的次数。 My specific problem occurs when applying the HOUR function to a range of date-cells and counting how many of these cells are equal to a certain hour. 当将HOUR函数应用于一系列日期单元并计算这些单元中有多少等于一个小时时,会发生我的特定问题。

To clarify, in VBA I would write something like (please assume that column A contains dates): 为了澄清起见,在VBA中,我将编写如下内容(请假设A列包含日期):

Sub tester33()
Dim r As Range
Dim c
Dim a As Integer
Set r = Range("A1:A10")
a = 0
For Each c In r
    If (Hour(c) = 5) Then 'time is 5 am, other if conditions are omitted
        a = a + 1
    End If
Next c
MsgBox (a)
End Sub

What I would like to have working is 我想工作的是

=COUNTIF(HOUR(A:A);"=5")

My search so far returned nada. 到目前为止,我的搜索返回了nada。 Please note that I found answers to this question here and here , that unfortunately won't work for nonmathematical Excel functions. 请注意,我在这里这里找到了此问题的答案,不幸的是,这些答案不适用于非数学的Excel函数。 I hope this isn't a completely trivial problem, as I am new to in-sheet formulas. 我希望这不是一个完全无关紧要的问题,因为我是表内公式的新手。

Use 采用

=SUM((HOUR(A1:A50)=5)*1)

or 要么

=SUM(--(HOUR(A1:A50)=5))

Both are array formulas so commit it by pressing Ctrl + Shift + Enter . 两者都是数组公式,因此可以通过按Ctrl + Shift + Enter提交。 Change A1:A50 to your data range. A1:A50更改为您的数据范围。

An alternative (non-array) formula is to use 另一种(非数组)公式是使用

=SUMPRODUCT(--(HOUR(A:A)=5))

which doesn't require committing the formula using Ctrl + Shift + Enter . 不需要使用Ctrl + Shift + Enter提交公式。 It's ultimately a matter of preference. 最终这是一个优先事项。

Also, if your data doesn't stretch the entire length of column A, consider using only the range occupied by data (eg A1:A50 instead of A:A ), as Excel then doesn't have to iterate through blank cells, which can clunk things up. 另外,如果您的数据没有扩展A列的整个长度,请考虑仅使用数据所占的范围(例如A1:A50而不是A:A ),因为Excel则不必遍历空白单元格,会把东西弄碎。

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

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