1

我得到了一些 countifv 那计数可见和 sumifsv 仅对可见单元求和...我希望 sumif 仅使用 vba 可见

默认仅在可见行上使用 SUMIFS

我有一张大桌子,有很多行和很多列,其中包含材料(沙子、石头等)批处理信息。我希望能够允许用户使用过滤器来选择他们想要查看的数据,然后能够查看其他工作表上的一些摘要信息(一天中的小时总数、材料总数等)。

我使用 DSUM 和/或 SUBTOTAL 让它运行得非常好和快,但是这些函数不会排除列表中的不可见(过滤)行。

使用我在此站点上看到的一些以前的帖子,我能够想出一些可行的方法,但是速度非常慢。

我希望更有经验的人可以就更快的事情给我建议。

我需要做的是在一张包含多达 30 个材料列信息(目标和实际批次重量)的记录列表中。我需要将这些行中的每一行按材料分组到一个相对时间段(12 :00 AM 到 12:59 AM、1:00 AM 到 1:59 AM 等)用户当然可以使用他们想要查看的 Time Bucket 进行选择

我正在使用 sumifs 函数和两个条件(即 Time >=12:00 和 Time < 1:00 )来获取每小时存储桶。

    You can also see from this code that I have to count the number of lines for the data and each criteria value because I could not figure out how to set the range of "B" & "C" without counting. Since I am using a filter, I know that the ranges (from a row perspective) of A,B & C are the same, just the relative columns are different. I tried to use the offset function, (i.e. Range(B) = Range(A).Offset(-1,0).Select or B = A.offset(-1,0).Select but they failed for some reason, and no error messages either. I think I somehow turned the erroring off.

无论如何,长篇大论,真的可以使用一些帮助。以下是相关代码:

Function Vis(Rin As Range) As Range
'Returns the subset of Rin that is visible
Dim Cell As Range
'Application.Volatile
Set Vis = Nothing
For Each Cell In Rin
If Not (Cell.EntireRow.Hidden Or Cell.EntireColumn.Hidden) Then
If Vis Is Nothing Then
Set Vis = Cell
Else
Set Vis = Union(Vis, Cell)
End If
End If
Next Cell
End Function

Function SUMIFv(Rin As Range, CriteriaRange1 As Range, CriteriaValue1 As Variant, CriteriaRange2 As Range, CriteriaValue2 As Variant) As Long
'Same as Excel SUMIFS worksheet function, except does not count
'cells that are hidden
Dim A1() As Range
Dim B1() As Range
Dim C1() As Range
Dim Csum As Long
' First count up the number of ranges
Cnt = 0
For Each A In Vis(Rin).Areas
Cnt = Cnt + 1
Next A

ReDim A1(1 To Cnt)
ReDim B1(1 To Cnt)
ReDim C1(1 To Cnt)

CntA = 1
For Each A In Vis(Rin).Areas
Set A1(CntA) = A
CntA = CntA + 1
Next A
CntB = 1
For Each B In Vis(CriteriaRange1).Areas
Set B1(CntB) = B
CntB = CntB + 1
Next B
CntC = 1
For Each C In Vis(CriteriaRange2).Areas
Set C1(CntC) = C
CntC = CntC + 1
Next C

If CntA <> CntB Or CntB <> CntC Then
MsgBox ("Error in Sumifs Function: Counts from Ranges are not the same")
End If
Csum = 0
For Cnt = 1 To CntA - 1
Csum = Csum + WorksheetFunction.SumIfs(A1(Cnt), B1(Cnt), CriteriaValue1, C1(Cnt), CriteriaValue2)
Next
SUMIFv = Csum
End Function

((仅计数可见单元格

如果您有兴趣,这里有一个更通用的 COUNTIF 解决方案,您也可以将其应用于 SUM 和其他对单元格范围进行操作的函数。

此 COUNTIFv UDF 使用工作表函数 COUNTIF 仅对可见单元格进行计数,因此 Condition 参数的工作方式与 COUNTIF 相同。因此,您可以像使用 COUNTIF 一样使用它:

=COUNTIFv(A1:A100,1)

请注意,它使用辅助函数 (Vis) 返回给定范围内不相交的可见单元格范围。这可以与其他工作表函数一起使用,以使它们仅对可见单元格进行操作。例如,

=SUM(Vis(A1:A100))

产生 A1:A100 中可见单元格的总和。这种直接在参数列表中使用 Vis 的方法不适用于 COUNTIF 的原因是 COUNTIF 不会接受不相交的范围作为输入,而 SUM 会。

这是UDF代码:

Function Vis(Rin As Range) As Range
'Returns the subset of Rin that is visible
Dim Cell As Range
Application.Volatile
Set Vis = Nothing
For Each Cell In Rin
If Not (Cell.EntireRow.Hidden Or Cell.EntireColumn.Hidden) Then
If Vis Is Nothing Then
Set Vis = Cell
Else
Set Vis = Union(Vis, Cell)
End If
End If
Next Cell
End Function

Function COUNTIFv(Rin As Range, Condition As Variant) As Long
'Same as Excel COUNTIF worksheet function, except does not count
'cells that are hidden
Dim A As Range
Dim Csum As Long
Csum = 0
For Each A In Vis(Rin).Areas
Csum = Csum + WorksheetFunction.CountIf(A, Condition)
Next A
COUNTIFv = Csum
End Function ))

这都是带有可见单元格的 countif 和 sumifs 但我需要 sumif 而不是 sumifs 请编辑第二个代码并编译并发布正确的程序:)

4

1 回答 1

0

我不认为这是正确的...

......................................使用 DSUM 和/或 SUBTOTAL,但这些函数不t 排除列表中不可见(已过滤)的行。

对于“过滤”和“隐藏”行,SUBTOTAL 的行为是不同的。

这是来自excel帮助:

对于从 1 到 11 的 function_num 常量,SUBTOTAL 函数包括 Excel 桌面应用程序“主页”选项卡上“单元格”组中“格式”命令的“隐藏和取消隐藏”子菜单下的“隐藏行”命令隐藏的行的值。当您想对列表中的隐藏和非隐藏数字进行小计时,请使用这些常量。对于从 101 到 111 的 function_Num 常量,SUBTOTAL 函数忽略由 Hide Rows 命令隐藏的行的值。如果您只想对列表中的非隐藏数字进行小计,请使用这些常量。

无论您使用哪个 function_num 值,SUBTOTAL 函数都会忽略任何未包含在过滤器结果中的行。

于 2013-11-25T15:52:01.877 回答