2013-04-25 30 views
1

我得到了一些countifv這就是統計可見,只有sumifsv其總和可見單元格...我使用VBA編輯程序可見細胞SUMIF在MS Excel

默認可見線僅

使用SUMIFS想SUMIF纔可見

我有一張大桌子,很多行和很多有材料(沙子,石頭等)批量信息的列。我希望能夠允許用戶使用過濾器來選擇他們想要查看的數據,然後能夠查看其他工作表中的一些摘要信息(按小時計的總數,按材料的總數等)。

我已經使用DSUM和/或SUBTOTAL實現了非常好且快速的工作,但是這些函數並不排除列表中的不可見(Filtered)行。

使用一些以前的帖子,我在這個網站上看到,我能夠想出有用的東西,但它非常緩慢。

我希望更有經驗的人可以建議我更快的事情。

我需要做的是在包含多達30個材料欄信息(目標和實際批量權重)的工作表上記錄清單。我需要按材料將每個這些行分組到一個相對時間桶(上午12:00至12:59,上午1:00至凌晨1:59。等),用戶當然可以用時間段選擇他們想要查看

我使用與SUMIFS功能兩個critieria(即時間> = 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 

((countifv可見單元格僅

如果你有興趣,這裏是一個更一般的COUNTIF解決方案,和一個你也可以適用於SUM等功能,其操作。在

單元格區域這COUNTIFv UDF使用工作表函數COUNTIF只計算可見單元格,因此條件參數工作方式相同與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請編輯第二個代碼和編譯和發佈正確的程序:)

+0

我想sumifvis(a1:a5,「> 0」)...以上sumifv公式是sumifsvisible與多個標準..我想sumifvis 1標準...謝謝你.. – tejaslok 2013-04-26 15:38:42

回答

0

我不要以爲這是正確的...

......................................使用DSUM和/或SUBTOTAL ,但這些函數不會排除列表中的不可見(已過濾)行。

SUBTOTAL的行爲與「已過濾」行和「隱藏」行不同。

這是從Excel幫助:

對於從1到11的function_num常數,SUBTOTAL函數 包括的所述 隱藏&取消隱藏子菜單下由隱藏行隱藏行的值命令在Excel桌面應用程序的 主頁選項卡上的單元格組中的格式化命令。 您想要在列表中小計隱藏和非隱藏號碼時使用這些常量。對於從101到111的 函數常量,SUBTOTAL函數會忽略隱藏行命令隱藏的 行的值。當您只想在列表中小計非隱藏號碼時,請使用這些常量 。

SUBTOTAL函數會忽略沒有包含在篩選器的 結果中的任何行,而不管您使用哪個function_num值。