繁体   English   中英

编辑程序以将MS excel中的可见单元格加和

[英]edit the program to sumif with visible cells in MS excel

我有一些countifv多数民众赞成在可见和sumifsv只汇总可见单元格...我希望sumif仅使用vba可见

默认仅在可见行上使用SUMIFS

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

使用DSUM和/或SUBTOTAL可以使它运行得非常好并且很快,但是这些功能不会在列表中排除不可见(已过滤)行。

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

我希望更多有经验的人可以为我提供更快的建议。

我需要做的是在一张纸上记录一个列表,该记录包含多达30列物料的信息(目标和实际批次重量数量。)我需要按物料将这些行中的每行分组到一个相对的时间段中(12 :00 AM至12:59 AM,1:00 AM至1:59 AM,依此类推。)用户当然可以使用时间段选择他们要查看的时间

我正在将sumifs函数与两个条件(即时间> = 12:00和时间<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,请编辑第二个代码并编译并发布正确的程序:)

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

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

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

这是来自excel帮助:

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

SUBTOTAL函数将忽略筛选结果中不包含的任何行,无论您使用哪个function_num值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM