如何统计观察次数(在Excel中)?
我有一个Excel文件中的两个工作表:
公司
ABCDE 1 COMPANY SECTOR VAR_1 VAR_2 VAR_3 2 Sony Tech 2.40 no 3 Ikea Home 7 1.44 yes 4 ING Bank 5 0 yes 5 BofA Bank 0 no 6 Google Tech 0 yes 7 Staples Home 5.24 no 8 Trump Ego 5 9.99 yes 9 ABN Bank 2.64 no
这张表为成千上万的公司持有数百个VAR
。 有许多缺失值(空单元格缺失值, 0
实际上是一个观测值)。 我需要知道我对每个VAR
有多less观察。 我需要(1)观察数量的总数和(2)银行业公司的观察数量。 在下面的表格中,(1)在B
栏中,(2)在C
栏中:
variables
ABC 1 VARIABLE TOTAL BANK 2 VAR_1 4 2 3 VAR_2 7 2 4 5 Some random comment... 6 7 VAR_3 8 3
我们以VAR_1
为例。 查看companies
表,有4个观察这个variables( 0
和5
)。 单看银行( SECTOR == 'Bank')
,有2个观测值(ING为5
,BofA为0
)。
在variables
表中,可能有不同variables之间的注释(尽pipe从不在A
列;这个列包含variables名或为空)。 另外,variables的顺序可能不一样。 所以在companies
可以这样说
... GH GI GJ 1 VAR_40 VAR_41 VAR_42
而在variables
说
A ... 60 VAR_40 61 VAR_42 62 VAR_41
我的问题是:什么公式可以计算variables
列B
和C
中观测值的数量? 任何帮助是极大的赞赏。
这不需要公式。 你可以使用枢轴
看这个截图
跟进
使用公式
对于Var2
, Var3
,相应地调整公式中的列。
例如, Var2
B14
将变为=COUNTA(D2:D9)
, C14
将变为=COUNTIFS($B$2:$B$9,$C$12,D2:D9,"<>")
跟随(从评论/聊天)
由于你的表不是连续的,所以我会推荐使用VBA(UDF)方法,以便实际上可以复制公式;)
将这两个代码粘贴到Module中
Function getVarCount(rngVar As Range, strSector As String, rngRw As Range) As Variant getVarCount = "Incomplete Data in Formula" If rngVar Is Nothing Or _ rngRw Is Nothing Or _ Len(Trim(strSector)) = 0 Then Exit Function Dim aCell As Range, SectorRange As Range Dim colSector As Long, colVar As Long Set aCell = rngRw.Find(What:=rngVar.Value, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then colVar = aCell.Column Else getVarCount = "VAR Heading Not Found" Exit Function End If Set aCell = rngRw.Find(What:="Sector", LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then colSector = aCell.Column Else getVarCount = "SECTOR Heading Not Found" Exit Function End If Set SectorRange = ThisWorkbook.Sheets(rngRw.Parent.Name).Columns(colSector) Set aCell = SectorRange.Find(What:=strSector, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If aCell Is Nothing Then getVarCount = strSector & " Not Found" Exit Function End If getVarCount = Application.WorksheetFunction.CountA(ThisWorkbook.Sheets(rngRw.Parent.Name).Columns(colVar)) - 1 End Function Function getVarSectorCount(rngVar As Range, strSector As String, rngRw As Range) As Variant getVarSectorCount = "Incomplete Data in Formula" If rngVar Is Nothing Or _ rngRw Is Nothing Or _ Len(Trim(strSector)) = 0 Then Exit Function Dim aCell As Range, SectorRange As Range Dim colSector As Long, colVar As Long Set aCell = rngRw.Find(What:=rngVar.Value, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then colVar = aCell.Column Else getVarSectorCount = "VAR Heading Not Found" Exit Function End If Set aCell = rngRw.Find(What:="Sector", LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If Not aCell Is Nothing Then colSector = aCell.Column Else getVarSectorCount = "SECTOR Heading Not Found" Exit Function End If Set SectorRange = ThisWorkbook.Sheets(rngRw.Parent.Name).Columns(colSector) Set aCell = SectorRange.Find(What:=strSector, LookIn:=xlValues, _ LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=False, SearchFormat:=False) If aCell Is Nothing Then getVarSectorCount = strSector & " Not Found" Exit Function End If '=COUNTIFS($B$2:$B$9,$C$12,D2:D9,"<>") getVarSectorCount = Application.WorksheetFunction.CountIfs(ThisWorkbook.Sheets(rngRw.Parent.Name).Columns(colSector), _ strSector, _ ThisWorkbook.Sheets(rngRw.Parent.Name).Columns(colVar), _ "<>") End Function
您可以从我们在聊天中讨论的Excel单元中调用它
截图