使用 Excel VBA 中的自动筛选从 Excel 工作表中剪切粘贴数据
目前我有一个包含大约 200000 多条记录的 Excel,我需要根据列过滤数据。该列有大约 5 个值,我需要过滤掉一张纸中的 2 个值,其余 3 个值保留在同一张纸中。
现在,不要使用逐个单元格比较来检查单元格的值是否属于上述两个值中的任何一个,然后将该行剪切粘贴到另一张纸中。这不适用于 200k 以上的记录,并且会挂起。
相反,我计划采用自动过滤方法。我尝试使用“记录宏”功能,但问题是它给了我一些错误,例如
“Excel 无法创建或使用数据范围引用,因为它太复杂了。请尝试以下操作之一 使用可以在矩形中选择的数据 使用同一张纸中的数据”
此外,如何仅复制粘贴过滤后的值到另一张纸?如果我尝试直接复制粘贴或特殊粘贴为“值”,那么即使是隐藏的行也会被复制下面
是我一直在篡改的宏代码
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
,上面可能有一些垃圾代码行,
有人可以帮助我吗?问题是Excel中存在的数据量。无法处理这么多数据我使用的是 Excel 2007
Current i have an excel with roughly 200000+ records and i need to filter data based on a column. 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.
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. This wouldn't work with 200k+ records and simply hangs,.
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"
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. Cant excel not handle this much data in VBA? Am using Excel 2007
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是清理后的代码:
关键是 SpecialCells 部分。
现在,尽管我非常喜欢良好的自动筛选复制/粘贴功能,但当您处理大量数据时,您可能需要考虑使用 ADO,它允许您使用 SQL 查询 Excel 工作表。
这里提供了 VBA 中 ADO 的详细概述:http://www.xtremevbtalk.com/ showthread.php?t=217783。
Here's your code cleaned up:
The key is that SpecialCells part.
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.
A good overview of ADO in VBA is provided here: http://www.xtremevbtalk.com/showthread.php?t=217783.
在数据右侧的第一个空列中插入一个测试您的条件的公式:例如
,然后在宏中,在尝试过滤并剪切答案1中描述的可见代码之前,对该列对整个 200,000 行数据集进行排序。
这将使数据块被剪切粘贴到一个连续的范围。这应该可以解决“数据范围太复杂”错误。
In the 1st empty column to the right of your data insert a formula that tests for your criteria: e.g.
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.
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.