简体   繁体   English

自动过滤后取可见范围

[英]Taking Visible range after AutoFilter

Im trying to select filtered rows into a range variable xRange . 我试图将筛选的行选择到范围变量xRange I need to have some operations on that range. 我需要对该范围进行一些操作。 Before filtering there was 10000+ rows. 过滤之前有10000多行。 After filtering it is 5000 rows. 过滤后为5000行。 So after filtering I tried this to get the filtered range which is not working as I expected. 因此,在过滤之后,我尝试使用此方法来获得过滤范围,但该范围无法按预期工作。

With Sheets("FullInvoice").Range("A1:N" & lRow)
    .AutoFilter
    .AutoFilter Field:=6, Criteria1:=">=" & fD, Criteria2:="<=" & lD

End With
Set xRange = Sheets("FullInvoice").Cells.SpecialCells(xlCellTypeVisible)

I added a break point at Set xRange line. 我在Set xRange行添加了一个断点。 and tried xRange.rows.count. 并尝试了xRange.rows.count。 But its just returning 1 even there is 5000 rows. 但是即使有5000行,它也只返回1。 So whats my purpose is to take the cell value of first cell after filtering. 所以我的目的是获取过滤后第一个单元格的单元格值。 How can I accomplish that. 我该怎么做。

When I try 当我尝试

dim s as string
s=xRange.Range("A1").value

It returning first cell value of the whole rows. 它返回整个行的第一个单元格值。 Not from the filtered rows. 不是来自过滤的行。

So how to solve this situation. 那么如何解决这种情况。

It's even easier than that. 比这更容易。 Just bring xrange inside your With . 只需将xrange带入With Also I'd use Range.Cells(x,y) rather than Range.Range to get a cell inside a range: 另外我会使用Range.Cells(x,y)而不是Range.Range来获取范围内的单元格:

With Sheets("FullInvoice").Range("A1:N" & lRow)
    .AutoFilter
    .AutoFilter Field:=6, Criteria1:=">=" & fD, Criteria2:="<=" & lD
    Set xrange = .SpecialCells(xlCellTypeVisible)
End With

MsgBox xrange.Cells(1, 1)

Try this code 试试这个代码

    With Sheets("FullInvoice").Range("A1:N" & lrow)
    .AutoFilter
    .AutoFilter Field:=6, Criteria1:=">=" & fd, Criteria2:="<=" & ld
End With

With Sheets("FullInvoice").Range("A1").CurrentRegion
    Set xRange = .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
End With
MsgBox xRange.Address

Dim s As String
s = xRange.Range("A1").Value
MsgBox s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM