2017-08-08 131 views

回答

4

可以使用SpecialCells(xlCellTypeVisible)得到過濾行:

Dim Tbl As Range 

Set Tbl = ActiveSheet.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible) 
' for DEBUG onlu 
Debug.Print Tbl.Address 

編輯1:全碼

Option Explicit 

Sub VarfiltRange() 

Dim BasketCostFiltRng As Range 
Dim LastRow As Long 
Dim VarRes As Double 

With Worksheets("Sheet1") '< -- modift "Sheet1" to your sheet's name 
    LastRow = .Cells(.Rows.Count, "D").End(xlUp).Row 

    ' get only the filteres rows in column D 
    Set BasketCostFiltRng = .Range("D2:D" & LastRow).SpecialCells(xlCellTypeVisible) 

    ' get the variance of only visible cells in Column "D" (after you filter to show only 1100 and 1112 in column "A") 
    VarRes = WorksheetFunction.Var(BasketCostFiltRng) 
    MsgBox VarRes 
End With 

End Sub 
+0

好吧,現在我有對象,其中包含此單元格的每個信息,我如何獲得價值?我需要這些數據來計算每個月的方差。 –

+1

@KamilZawistowski您可以將它讀入數組中,但爲此您需要共享工作表數據,以便我們可以更好地瞭解 –

+0

drive.google.com/open?id=0B5PmrbRk4Uarai1fQk9SLWNrb1U那裏有示例工作簿。假設我想要filer客戶號碼。 1126和1100在Zakupy的工作表中,然後我想計算籃子成本的方差。 –

2

你CA n使用SpecialCells得到:

Sheet1.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Address 
+0

在我之後的幾秒鐘......你潛伏着這個;) –