Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
编辑程序,使其在MS excel中具有可见单元格_Excel_Sumifs_Vba - Fatal编程技术网

编辑程序,使其在MS excel中具有可见单元格

编辑程序,使其在MS excel中具有可见单元格,excel,sumifs,vba,Excel,Sumifs,Vba,我得到了一些计算可见细胞数的countifv和只计算可见细胞数的sumifsv。。。我希望sumif仅使用vba可见 默认情况下,仅在可见行上使用SUMIFS 我有一个很大的表,很多行和很多列,其中包含材料(沙子、石头等)的成批信息。我希望能够允许用户使用过滤器选择他们想要查看的数据,然后能够查看其他表单上的一些汇总信息(按小时汇总、按材料汇总等) 我使用DSUM和/或SUBTOTAL让它运行得非常好和快速,但这些函数并不排除列表中不可见(过滤)的行 使用我以前在这个网站上看到的一些帖子,我能够

我得到了一些计算可见细胞数的countifv和只计算可见细胞数的sumifsv。。。我希望sumif仅使用vba可见

默认情况下,仅在可见行上使用SUMIFS

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

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

使用我以前在这个网站上看到的一些帖子,我能够想出一些有效的方法,但是速度非常慢

我希望更有经验的人能给我一些更快的建议

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

我使用带有两个临界值(即时间>=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, (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
请注意,它使用了一个辅助函数(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,但我需要sumifs。如果不是sumifs,请编辑第二个代码,编译并发布正确的程序:)

我认为这不正确

使用DSUM和/或小计,但 这些函数不排除列表中不可见(过滤)的行

“筛选”行和“隐藏”行的小计行为不同

这来自excel帮助:

对于从1到11的函数_num常量,小计函数 包括由“隐藏行”命令隐藏的行的值 在屏幕上的“单元格”组中隐藏“格式”命令的子菜单(&Unhide) Excel桌面应用程序中的“主页”选项卡。在需要时使用这些常量 您希望对列表中的隐藏和非隐藏数字进行小计。对于 函数_Num常量从101到111,小计函数忽略 通过“隐藏行”命令隐藏的行的值。使用这些常量 当您只想小计列表中的非隐藏数字时

SUBTOTAL函数忽略未包含在列表中的任何行 过滤器的结果,无论使用哪个函数的数值

我想要sumifvis(a1:a5,“>0”)。。。上面的sumifv公式可以用多个标准来计算。。我要sumifvis有1个标准。。。谢谢你。。
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 ))