简体   繁体   English

SpecialCells(xlCellTypeVisible)选择整个工作表

[英]SpecialCells(xlCellTypeVisible) choose the whole worksheet

I am Sorry to make the question unclear. 很抱歉,这个问题不清楚。 Here is an example from Error when I use SpecialCells of AutoFilter to get visible cells in VBA changed the code: 这是来自Error的示例, 当我使用AutoFilter的SpecialCells来获取VBA中的可见单元格时更改了代码:

Sub Sample():
    ActiveSheet.AutoFilterMode = False

    Dim rRange As Range
    Dim Rnge As Range
    Dim last_Row As Integer

    Set rRange = Sheets("Sheet1").Range("A1:F6")

    '~~> Filter,
    With rRange
      .AutoFilter Field:=1, Criteria1:="=1"
    End With

    last_Row = Workbooks("Book1").Sheets("Sheet1").Range("A1048576").End(xlUp).Row

      '~~> Offset(to exclude headers)
    Set Rnge = Range("A2:A" & last_Row).SpecialCells(xlCellTypeVisible)

    Debug.Print Rnge.Address
    Debug.Print last_Row

End Sub

Samples data: 样本数据:

Number1 Number2 Number3 Number4 Number5 Number6
1       1       1       1       1       1
2       2       2       2       2       2
3       3       3       3       3       3
4       4       4       4       4       4
5       5       5       5       5       5

If I set the criteria = 2. Then the debug.print will give me the row3. 如果我将条件设置为2,则debug.print将给我row3。 But if I set the criteria = 1. The it returns $1:$2,$7:$1048576 2. What happend? 但是,如果我将条件设置为1。它将返回$ 1:$ 2,$ 7:$ 1048576 2.发生了什么?

This is a known problem with attempting .SpecialCells(xlCellTypeVisible) on a single first row (your range is Range("A2:A2") when last_row is 2). 这是在单个第一行上尝试.SpecialCells(xlCellTypeVisible)时的已知问题.SpecialCells(xlCellTypeVisible) last_row为2时,您的范围是Range("A2:A2") )。 If you set .SpecialCells(xlCellTypeVisible from a single row of filtered data it includes the data range outside of the filtered range, If you set last_row before filtering, you would get the correct results assuming you had more than a single row of data to start with. 如果从单行过滤的数据中设置.SpecialCells(xlCellTypeVisible,则它包括过滤范围之外的数据范围;如果在过滤前设置了last_row ,则假设您要启动的数据多于一行,则将获得正确的结果用。

    last_Row = Sheets("Sheet1").Range("A" & rows.count).End(xlUp).Row
    Set rRange = Sheets("Sheet1").Range("A1:F" & last_Row)

    '~~> Filter,
    With rRange
      .AutoFilter Field:=1, Criteria1:="=1"
    End With

I prefer to work strictly within the range to be filtered and use the worksheet function .Subtotal to determine if there are any cells to process. 我更喜欢严格地在要过滤的范围内工作,并使用工作表函数.Subtotal来确定是否有任何要处理的单元格。

Sub Sample2()
    ActiveSheet.AutoFilterMode = False

    With Sheets("Sheet1").Cells(1, 1).CurrentRegion
        .AutoFilter
        .AutoFilter Field:=1, Criteria1:="=1"

        With .Offset(1, 0).Resize(.Rows.Count - 1, .Columns.Count)
            If Application.Subtotal(103, .Columns(1)) Then
                Debug.Print .Columns(1).SpecialCells(xlCellTypeVisible).Address(0, 0)
                Debug.Print .SpecialCells(xlCellTypeVisible).Rows.Count
            Else
                Debug.Print "no cells were found"
            End If
        End With

    End With
End Sub

The .CurrentRegion returns the block or island of date encompassing A1 until it meets a fully blank row or column. .CurrentRegion返回包含A1的日期块或日期岛,直到遇到完全空白的行或列。 This is the same as selecting A1 and tapping Ctrl + A . 这与选择A1并点击Ctrl + A相同 By working strictly within the confines of CurrentRegion (remember to resize when offsetting), you can avoid a range reference that might spill into the rest of the worksheet where the visible rows will be the remainder of the worksheet's cells. 通过严格地在CurrentRegion的范围内工作(请记住在偏移时调整大小),可以避免范围引用可能溢出到工作表的其余部分,在这些地方可见行将是工作表单元格的其余部分。

There was a KB whitepaper on this but regretfully I could not locate it. 上面有一个知识库白皮书,但很遗憾,我找不到它。

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

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