【问题标题】:Selecting a specific visible range After Filtering dataset过滤数据集后选择特定的可见范围
【发布时间】:2019-05-02 20:28:14
【问题描述】:

我试图在过滤我的数据集后复制一个特定的列(不包括标题)。如果过滤后有多行,我选择的范围可以正常工作,我可以将该列复制到另一个工作表中。但是,如果过滤后只有一行,当我指定范围时,它会选择所有空单元格以及非空单元格,并且我的代码出现故障。我该如何解决这个问题?

我尝试使用不同的范围属性,但无法获得预期的结果

'''Finding the Pack Test Category from the filtered 1st column'''
Set RngA = ActiveSheet.AutoFilter.Range.SpecialCells(xlCellTypeVisible).Areas(2)(1, 1)

''Here the selection of range includes all the empty cells as well!
Set RngA = Range(RngA, RngA.End(xlDown)) 

'''Copy & Pasting in the Forecast Sheet for temporary use'''
RngA.SpecialCells(xlCellTypeVisible).Copy Destination:=wbA.ActiveSheet.Range("L1")

我希望只选择具有数据的可见单元格而不是空单元格的范围。

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    假设我们从:

    并过滤 45 岁以上的年龄:

    我们想将过滤后的列 A 复制到另一个工作表:

    Sub KopyOneKolumn()
        Dim r1 As Range
        Dim r2 As Range
        Dim r3 As Range
    
        Set r1 = ActiveSheet.AutoFilter.Range ' the total visible range
        Set r2 = Intersect(r1.Offset(1, 0), r1) ' clip off the header
        Set r3 = Intersect(r2, Columns(1)) ' pick only column A
    
        r3.Copy
        Sheets("Sheet2").Paste
    End Sub
    

    Sheet2 中的结果:

    注意:

    关键是r1 代表自动过滤表中可见单元格的“可复制”块。

    【讨论】:

    • 我测试了你的代码..这完全有效:) ..如果我想复制过滤数据集和一些特定的列怎么办让我们说只是 A 列和 C 列(跳过 B 列)以及标题?
    【解决方案2】:

    如果标题下的第一行是过滤(可见)单元格的一部分,则依赖Areas(2) 将不起作用。

    'at this point, AutoFilter has been applied
    'I have no idea what the range is
    'or what column you are interested in
    
    with ActiveSheet.AutoFilter.Range
        with .cells.resize(.rows.count-1, 1).offset(1, 0)
            set RngA = .SpecialCells(xlCellTypeVisible)
        end with
    end with
    
    RngA.copy Destination:=wbA.ActiveSheet.Range("L1")
    

    我不同意您使用 ActiveSheet 作为父工作表引用。应明确引用源工作表和目标工作表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2019-05-12
      • 2017-11-05
      • 2016-06-10
      • 1970-01-01
      • 2017-08-21
      相关资源
      最近更新 更多