我有一个表,其中两列包含表中每行或记录的开始和结束编号。在一个单独的区域,我有从1到39的数字。我想对所有这些数字执行计数,以便我知道有多少记录包括包含该数字的范围。
所以,让我们说左栏是开始编号,右边是结束编号:
7 36
8 14
8 35
9 29
10 39
11 36
13 34
13 15
14 39
我想获得每行可以包含14的计数。这将包括第一行,因为它从7开始到36结束,因此在它们之间找到14。同样,下一行以14结尾,因此也包含14.依此类推。
注意:14只是一个例子,我需要检查更多数字以查看它们是否在上述范围内。上述范围也是示例,有许多记录包括所有不同的范围字段。
虽然我知道怎么做计数如果开始和结束编号是计数的标准如果(如果公式只是多个标准计数),我不知道如何反向情况。或者我还没有想到一个简单的方法。有什么想法吗?
答案 0 :(得分:1)
我认为这段代码可以让你非常接近你想去的地方
它会询问您两列的数据范围,然后是一个单元格输出结果
扫描的每个rec的结果将在单独的行中列出
一如既往地在工作副本上试试这个。祝你好运!
Sub CountInRange()
Dim RangeToScan As Range
Dim RangeToWrite As Range
Dim NumToFind As Integer
Dim Found As Integer
Dim Row As Long
Dim CurrentCell As Range
'Have the user show me the range of numbers
Set RangeToScan = Application.InputBox("Select the range to scan", _
Title:="Count in Range", Type:=8)
'Make sure the range is only two cols wide
Do While RangeToScan.Columns.Count <> 2
MsgBox ("The range can only be 2 columns wide!")
Set RangeToScan = Application.InputBox("Select the range to scan", _
Title:="Count in Range", Type:=8)
Loop
'Have the user show me the starting write location
Set RangeToWrite = Application.InputBox("Select the starting write location", _
Title:="Count in Range", Type:=8)
Do While RangeToWrite.Columns.Count > 1 And RangeToWrite.Rows.Count > 1
MsgBox ("The starting write location must be one cell")
Set RangeToWrite = Application.InputBox("Select the range to scan", _
Title:="Count in Range", Type:=8)
Loop
'For each number to find
For NumToFind = 1 To 39
Found = 0
Set CurrentCell = RangeToScan(1, 1)
'for each row in the Range to Scan
For Row = 1 To RangeToScan.Rows.Count
If NumToFind >= CurrentCell.Value And NumToFind <= CurrentCell.Offset(0, 1).Value Then
Found = Found + 1
End If
Set CurrentCell = CurrentCell.Offset(1, 0)
Next
Set CurrentCell = RangeToWrite(NumToFind, 1)
CurrentCell.Value = "Record " & NumToFind & " was found " & Found & " times"
Next
End Sub
答案 1 :(得分:0)
请尝试:
=COUNTIFS(A:A,"<="&D$1,B:B,">="&D$1)
其中D1包含14
。
您可能希望在第1行的D1右侧放置14
的替代品,并将上述公式复制到右侧。