Excel-将年龄分组到预定义的类别中
我在一列中使用了这些分组和一些人口数据(称之为Data1): 10至14年 15至17年 18岁和19岁 二十年 21年 22至24岁 25至29岁 30至34岁 35至39岁 ... 85岁及以上 全部的 我还有另一段数据(称之为Data2),我用它来代替按年龄从1岁到100岁的人口分组 我需要将数据2分组到与数据1相同的分组中 我开始用Excel公式(使用IF语句)来做这件事,但是有24个不同的年龄组,所以我很快就放弃了Excel-将年龄分组到预定义的类别中,excel,vba,grouping,excel-formula,Excel,Vba,Grouping,Excel Formula,我在一列中使用了这些分组和一些人口数据(称之为Data1): 10至14年 15至17年 18岁和19岁 二十年 21年 22至24岁 25至29岁 30至34岁 35至39岁 ... 85岁及以上 全部的 我还有另一段数据(称之为Data2),我用它来代替按年龄从1岁到100岁的人口分组 我需要将数据2分组到与数据1相同的分组中 我开始用Excel公式(使用IF语句)来做这件事,但是有24个不同的年龄组,所以我很快就放弃了 使用公式/vba/其他方法有更简单的方法吗?如果您构建一个两列查找
使用公式/vba/其他方法有更简单的方法吗?如果您构建一个两列查找表,您可以使用一个简短的公式,例如在Y2中,按升序放下每个范围的下限,例如在Y2中10,在Y3中15,18在Y4等中。然后在Z列的相应行中,您可以放置组文本,例如在Z2“10到14年”在Z3中,“15到17年”等 现在,对于A1中的特定年龄,您可以使用此公式得到正确的组
=LOOKUP(A1,$Y$2:$Z$25)
如果我了解您的数据,看起来数据2是一个原始的年龄列表,而数据1是一个装箱数据
您可以通过数组公式将原始年龄数据移动到bind中
=FREQUENCY(<bins>,<Data2>)
另外,如果我的第一个猜测不是你想要的,你可以试试这个UDF
Public Function Between(inputCell As Range, twoColBetweenRage As Range, outputLabel As Range)
If twoColBetweenRage.Rows.Count <> outputLabel.Rows.Count Then
Err.Raise 12345, "", "Input ranges are not equally sized"
Exit Function
End If
Dim r As Integer, inputValue As Double
inputValue = inputCell.Value
For r = twoColBetweenRage.Row To twoColBetweenRage.Rows.Count
If twoColBetweenRage(r, 1) <= inputValue And twoColBetweenRage(r, 2) >= inputValue Then
Between = outputLabel(r, 1)
Exit Function
End If
Next r
If IsEmpty(Between) Then
Err.Raise 12345, "", "No value was found"
Exit Function
End If
End Function
公共函数介于(inputCell作为范围,twoColBetweenRage作为范围,outputLabel作为范围)
如果twoColBetweenRage.Rows.Count输出标签.Rows.Count,则
错误:Raise 12345,“,”输入范围大小不相等”
退出功能
如果结束
Dim r为整数,inputValue为双精度
inputValue=inputCell.Value
对于r=twoColBetweenRage.Row到twoColBetweenRage.Rows.Count
如果twoColBetweenRage(r,1)=输入值,则
介于=输出标签(r,1)
退出功能
如果结束
下一个r
如果我是空的,那么
错误:Raise 12345,“,”未找到值
退出功能
如果结束
端函数
如果您运行的是excel 2007或更高版本,则可以使用
COUNTIF
10到14年
=COUNTIF(Data2,“>=10”,Data2,“=15”,Data2,“=18”,Data2,“谢谢Mike。只是跟进一下,在我的回答中,你最终使用了哪种解决方案?”?
10
15
18
20
21
22
25
30
35
40
45
50
55
60
65
70
75
80
85
Public Function Between(inputCell As Range, twoColBetweenRage As Range, outputLabel As Range)
If twoColBetweenRage.Rows.Count <> outputLabel.Rows.Count Then
Err.Raise 12345, "", "Input ranges are not equally sized"
Exit Function
End If
Dim r As Integer, inputValue As Double
inputValue = inputCell.Value
For r = twoColBetweenRage.Row To twoColBetweenRage.Rows.Count
If twoColBetweenRage(r, 1) <= inputValue And twoColBetweenRage(r, 2) >= inputValue Then
Between = outputLabel(r, 1)
Exit Function
End If
Next r
If IsEmpty(Between) Then
Err.Raise 12345, "", "No value was found"
Exit Function
End If
End Function