编辑该程序与MS excel中的可见单元求和

我得到了一些countifv计数可见和sumifsv总和可见细胞只…我想sumif只可见使用vba

默认仅在可见行上使用SUMIFS

我有一个大桌子,很多行和很多有材料(沙子,石头等)批量信息的列。 我希望能够允许用户使用filter来select他们想要查看的数据,然后能够查看其他表单上的一些摘要信息(按小时计的总数,材料总数等)。

我使用DSUM和/或SUBTOTAL可以非常快速地工作,但是这些函数不会排除列表中的不可见(Filtered)行。

使用之前在这个网站上看到的一些post,我能够想出一些可行的方法,但速度非常慢。

我希望有更多的经验丰富的人可以提供更快的build议。

我需要做的是在一张表上包含多达30个材料栏目的信息(目标和实际批量重量)的logging列表。我需要按材料将这些行按照相对时间段(12 :00 AM到12:59 AM,1:00 AM到1:59 AM等)用户当然可以select他们想要查看的时间段

我使用两个批评(即时间> = 12:00和时间<1:00)的sumifs函数来获得小时桶。

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, (ie 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解决scheme,你也可以应用SUM和其他函数来操作单元格范围。

这个COUNTIFv UDF使用工作表函数COUNTIF来只计算可见的单元格,所以Condition参数和COUNTIF一样。 所以你可以像使用COUNTIF一样使用它:

 =COUNTIFv(A1:A100,1) 

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

 =SUM(Vis(A1:A100)) 

生成A1:A100中可见单元格的总和。 这种在参数列表中直接使用Vis的方法在COUNTIF中不起作用的原因是COUNTIF不会接受不相交的范围作为input,而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,但是这些function不会“排除列表中的不可见(Filtered)行。

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

这是来自excel的帮助:

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

SUBTOTAL函数会忽略任何不包含在filter结果中的行,不pipe您使用哪个function_num值。