简体   繁体   English

使用Excel VBA中的自动筛选器从Excel工作表剪切粘贴数据

[英]Cut Paste data from excel sheet using Autofilter in Excel VBA

Current i have an excel with roughly 200000+ records and i need to filter data based on a column. 当前,我有一个具有大约200000多个记录的excel,我需要根据列过滤数据。 The column has around 5 values and i need to filter out 2 values in one sheet and the rest 3 to remain in the same sheet. 该列有大约5个值,我需要在一张纸上过滤掉2个值,其余3个要保留在同一张纸上。

Now instead of using cell by cell comparison to check whether the value of the cell falls in any of the above 2 values and then cut paste the row into another sheet. 现在,而不是使用逐个单元比较来检查单元格的值是否属于上述2个值中的任何一个,然后将行剪切粘贴到另一张工作表中。 This wouldn't work with 200k+ records and simply hangs,. 这无法处理200k +记录,只是挂起。

Instead am planning to take the auto filter method. 相反,我打算采用自动过滤方法。 I tried using the 'Record macro' feature, but the problem is that it gives me some error like 我尝试使用“记录宏”功能,但问题是它给了我一些错误,例如

"Excel cannot create or use the data range reference because its too complex.Try one of the following Use data that can be selected in rectangle Use data from the same sheet" “ Excel无法创建或使用数据范围引用,因为它太复杂了。请尝试以下使用矩形中可以选择的数据之一使用同一张纸中的数据”

Moreover how to copy paste only the filtered values to another sheet? 此外,如何将仅过滤后的值复制粘贴到另一张纸上? If I try to copy paste directly or special paste as 'values' then also even the hidden rows get copy pasted. 如果我尝试直接复制粘贴或将特殊粘贴复制为“值”,那么即使是隐藏的行也会复制粘贴。

Below is the macro code i have been tampering around with 下面是我一直在篡改的宏代码

    Sub Macro34()
    '
    ' Macro34 Macro
    '

    '
        Rows("1:1").Select
        Selection.AutoFilter
        ActiveSheet.Range("$A$1:$T$81335").AutoFilter Field:=6, Criteria1:="=242", _
            Operator:=xlOr, Criteria2:="=244"
        Cells.Select
        Selection.Copy
        ActiveWindow.SmallScroll Down:=21
        Sheets("Sheet2").Select
        ActiveWindow.SmallScroll Down:=-18
        Range("A1").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Selection.ClearContents
        Range("A1").Select
        Sheets("Sheet1").Select
        Selection.Copy
        Sheets("Sheet2").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Sheets("Sheet2").Select
        ActiveWindow.SmallScroll Down:=93
        Sheets("Sheet1").Select
        ActiveWindow.SmallScroll Down:=-9
        ActiveWindow.ScrollRow = 1
        Rows("1:1").Select
        Application.CutCopyMode = False
        Selection.AutoFilter
    End Sub

There might be some junk lines of code above as its generated using the 'record macro' feature. 使用“记录宏”功能生成的上面可能有一些垃圾代码行。

Could someone please help me. 有人可以帮我吗。 The problem is the amount of data present in excel. 问题是excel中存在的数据量。 Cant excel not handle this much data in VBA? Cant Excel无法在VBA中处理这么多数据? Am using Excel 2007 我正在使用Excel 2007

Here's your code cleaned up: 这是您清理的代码:

Sub Macro34()

    ' Turn off autofiltering
    If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False

    ' Turn it back on
    Rows(1).AutoFilter

    ' Set the autofiltering conditions
    Rows(1).AutoFilter Field:=6, _
        Criteria1:="=242", _
        Operator:=xlOr, _
        Criteria2:="=244"

    ' Copy only the relevant range
    Range("A1", _
          Cells(65536, Cells(1, 256).End(xlToLeft).Column).End(xlUp)).SpecialCells(xlCellTypeVisible).Copy

    ' Paste the data into Sheet2 (assuming that it exists)
    Sheets("Sheet2").Range("A1").PasteSpecial xlPasteValuesAndNumberFormats
    Application.CutCopyMode = False

End Sub

The key is that SpecialCells part. 关键是SpecialCells部分。

Now, as much as I love a good autofilter copy/paste, when you're dealing with that much data, you might want to look into using ADO, which would allow you to query your Excel worksheet using SQL. 现在,尽管我喜欢一个很好的自动过滤器复制/粘贴,但是当您处理那么多数据时,您可能希望使用ADO,这将允许您使用SQL查询Excel工作表。

A good overview of ADO in VBA is provided here: http://www.xtremevbtalk.com/showthread.php?t=217783 . 此处提供了VBA中ADO的完整概述: http ://www.xtremevbtalk.com/showthread.php?t= 217783

In the 1st empty column to the right of your data insert a formula that tests for your criteria: eg 在数据右侧的第一个空白列中,插入一个用于测试您的条件的公式:例如

=if(or(a2=242,a2-244),"Move","Keep")

then in your macro, sort the whole 200,000 line data set by that column before you attempt the filter and cut visible code described in answer1. 然后在您的宏中,排序该列设置的整个200,000行数据,然后再尝试过滤并剪切在answer1中描述的可见代码。

This will make the block of data to be cut-n-pasted one contiguous range. 这将使数据块被剪切-粘贴为一个连续范围。 This should get around the 'data range too complex' error. 这应该绕过“数据范围太复杂”错误。

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

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