1

我在 Excel 文件中有两个工作表:

公司

  A       B      C     D     E
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. 我需要 (1) 观察次数的总计数和 (2) 银行业公司的观察次数。在下表中,(1) 在列中B,(2) 在列中C

变量

   A       B     C
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 个观测值(75和)。只看银行(有 2 个观察值(ING 和BofA)。05SECTOR == 'Bank')50

在工作variables表中,不同变量之间可能有注释(尽管从未在列中A;此列包含变量名称或为空)。此外,变量的顺序可能不一样。所以companies可以说

  ... GH     GI     GJ
1     VAR_40 VAR_41 VAR_42

variables里面说

    A
...
60  VAR_40
61  VAR_42
62  VAR_41

我的问题是:什么公式可以计算variables列中的观察数BC?任何帮助是极大的赞赏。

4

1 回答 1

1

你不需要公式。您可以使用枢轴

看这个截图

在此处输入图像描述

跟进

使用公式

在此处输入图像描述

对于Var2, Var3,相应地调整公式中的列。

例如B14for Var2will become=COUNTA(D2:D9)C14will become=COUNTIFS($B$2:$B$9,$C$12,D2:D9,"<>")

跟进(来自评论/聊天)

由于您的表格不是连续的,因此我建议您使用 VBA (UDF) 方法,以便您可以实际复制公式;)

将这两个代码粘贴到模块中

    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 单元格中调用它

截屏

在此处输入图像描述

于 2013-04-21T10:35:41.093 回答