我想弄清楚如何在過濾變體對象後獲取數據。當我使用這個:VBA:如何從過濾的數據中獲取當前區域?
table = ActiveSheet.Range("A1").CurrentRegion
我gettig所有數據,但我只想要過濾。
我想弄清楚如何在過濾變體對象後獲取數據。當我使用這個:VBA:如何從過濾的數據中獲取當前區域?
table = ActiveSheet.Range("A1").CurrentRegion
我gettig所有數據,但我只想要過濾。
可以使用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
你CA n使用SpecialCells
得到:
Sheet1.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Address
在我之後的幾秒鐘......你潛伏着這個;) –
好吧,現在我有對象,其中包含此單元格的每個信息,我如何獲得價值?我需要這些數據來計算每個月的方差。 –
@KamilZawistowski您可以將它讀入數組中,但爲此您需要共享工作表數據,以便我們可以更好地瞭解 –
drive.google.com/open?id=0B5PmrbRk4Uarai1fQk9SLWNrb1U那裏有示例工作簿。假設我想要filer客戶號碼。 1126和1100在Zakupy的工作表中,然後我想計算籃子成本的方差。 –