你不需要公式。您可以使用枢轴
看这个截图
跟进
使用公式
对于Var2
, Var3
,相应地调整公式中的列。
例如B14
for Var2
will become=COUNTA(D2:D9)
和C14
will 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 单元格中调用它
截屏