VBA:如何从过滤的数据中获取当前区域?
问题描述:
我想弄清楚如何在过滤变体对象后获取数据。当我使用这个: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
+0
在我之后的几秒钟......你潜伏着这个;) –
好吧,现在我有对象,其中包含此单元格的每个信息,我如何获得价值?我需要这些数据来计算每个月的方差。 –
@KamilZawistowski您可以将它读入数组中,但为此您需要共享工作表数据,以便我们可以更好地了解 –
drive.google.com/open?id=0B5PmrbRk4Uarai1fQk9SLWNrb1U那里有示例工作簿。假设我想要filer客户号码。 1126和1100在Zakupy的工作表中,然后我想计算篮子成本的方差。 –