从范围variables调用过滤的行

我使用VBA在Excel中过滤表格,并将其保存在范围variables中。 例如:我有1000个字段,过滤之后,它只有200个。我将这些数据保存在一个范围内,并随机select一个在这个范围内的数字。

在此之后,我想改变过滤的行的信息。 但是,我不知道这是怎么回事。 例如,如果我使用Cells(Line, 10)则vba会考虑所有行并打印出范围。

我怎样才能改变过滤单元格的数量或地址?

 Dim Rng As Range ActiveSheet.Range("A1:AE" & tlast_row).AutoFilter Field:=1, Criteria1:="teste" Set Rng= Range("A2", Range("A2").End(xlDown)).Cells.SpecialCells(xlCellTypeVisible) tSelCell = WorksheetFunction.RandBetween(1, RngC.Count) Rng.SpecialCells(xlCellTypeVisible).Cells(tSelCell, 3) = "TEST" '< Here is the problem 

假设我们有这样的数据:

在这里输入图像说明

我们过滤为“开心”:

在这里输入图像说明

可见的单元格形成不相交的范围。 为了从不相交的范围中随机选取,我们首先创build一个可见单元格地址数组,然后从该数组中随机选取:

 Option Base 1 Sub PickARandomVisibleRow() Dim rDisjoint As Range, ary() As Variant, NrD As Long Dim tSelCell As Long Set rDisjoint = Range("A2:A24").Cells.SpecialCells(xlCellTypeVisible) NrD = rDisjoint.Count ReDim ary(1) i = 1 For Each r In rDisjoint If i = 1 Then Else ReDim Preserve ary(i) End If ary(i) = r.Address i = i + 1 Next r msg = "" For i = LBound(ary) To UBound(ary) msg = msg & vbCrLf & i & vbTab & ary(i) Next i MsgBox msg tSelCell = Application.WorksheetFunction.RandBetween(1, UBound(ary)) msg = "Random Pick item: " & tSelCell & " which is cell: " & ary(tSelCell) MsgBox msg End Sub