我正在编写一个宏,除了做其他事情之外,还应该过滤一长列由正数和负数组成的数据。这些数字可以是任何值,不一定是 -4095 到 4095 之间的所有值,但整个列的长度最多可达 800,000 行以上。我想过滤掉列中任何 -4095、1、2、3、4 或 4095 的数字。 使用过滤器选项并取消选中我想要省略的数字,手动执行此操作很容易,但将其记录到宏会在大约 230 个数字后导致错误“太多行延续”。显然,这是因为宏列出了它在列中留下的数字,而不是它删除的数字。我知道 25 条物理行的限制,并且我应该向每行添加更多数字,但是如何才能做到这一点,而不必输入超过 8100 个不同的数字 - 大多数四位数字长,每个数字都在引号中,每个数字之间有逗号?这将要我永远! 我知道还有其他代码方法可以完成此过程,但对于大型文件,它们可能需要超过 45 分钟才能完成。过滤过程要快得多,通常会在 30 秒左右完成,即使是大文件也是如此。我还尝试了其他过滤标准,但似乎没有其他效果。
Columns("C:C").Select
Selection.AutoFilter
ActiveSheet.Range("$C$4:$C$82784").AutoFilter Field := 1, Criteria1 := Array("0" _
,"-1","10","-10","100","-100","1000","-1000","1001","-1001","1002","-1002", _
"1003","-1003","1004","-1004","1005","-1005","1006","-1006","1007","-1007", _
"1008","-1008","1009","-1009","101","-101","1010","-1010","1011","-1011","1012" _
,"-1012","1013","-1013","1014","-1014","1015","-1015","1016","-1016","1017", _
"-1017","1018","-1018","1019","-1019","102","-102","1020","-1020","1021", _
"-1021","1022","-1022","1023","-1023","1024","-1024","1025","-1025","1026", _
"-1026","1027","-1027","1028","-1028","1029","-1029","103","-103","1030", _
"-1030","1031","-1031","1032","-1032","1033","-1033","1034","-1034","1035", _
"-1035","1036","-1036","1037","-1037","1038","-1038","1039","-1039","104", _
"-104","1040","-1040","1041","-1041","1042","-1042","1043","-1043","1044", _
"-1044","1045","-1045","1046","-1046","1047","-1047","1048","-1048","1049", _
"-1049","105","-105","1050","-1050","1051","-1051","1052","-1052","1053", _
"-1053","1054","-1054","1055","-1055","1056","-1056","1057","-1057","1058", _
"-1058","1059","-1059","106","-106","1060","-1060","1061","-1061","1062", _
"-1062","1063","-1063","1064","-1064","1065","-1065","1066","-1066","1067", _
"-1067","1068","-1068","1069","-1069","107","-107","1070","-1070","1071", _
"-1071","1072","-1072","1073","-1073","1074","-1074","1075","-1075","1076", _
"-1076","1077","-1077","1078","-1078","1079","-1079","108","-108","1080", _
"-1080","1081","-1081","1082","-1082","1083","-1083","1084","-1084","1085", _
"-1085","1086","-1086","1087","-1087","1088","-1088","1089","-1089","109", _
"-109","1090","-1090","1091","-1091","1092","-1092","1093","-1093","1094", _
"-1094","1095","-1095","1096","-1096","1097","-1097","1098","-1098","1099", _
"-1099","11","-11","110","-110","1100","-1100","1101","-1101","1102","-1102", _
唉,我发现我想做的事情是不可能的。
我打开一个空白的 Excel 页面,创建了一行从 -4090 到 4095 的数字。然后我将其保存为 .csv 文件,用写字板打开它,并使用查找/替换功能替换所有逗号 (,)引号中包含逗号和空格 (", ")。这给了我根据需要格式化的所有 8,191 个数字。然后,我复制了整个内容并将其粘贴到宏的自动过滤器数组中。灾难随之而来。显然,除了函数的 25 行限制之外,还有 1024 个字符的单独行长度限制。复制的数字达到了 1024 的限制,然后绕到下一行。这种情况一直持续到所有数字都被粘贴进去为止。不幸的是,远远超过了行长度限制 - 加起来达到 66 行,远远超过了 25 行限制。现在整个功能是红色的并且无效。
除非有人有任何其他想法,否则看起来循环或辅助列可能是唯一可行的选择。
最简单的方法是辅助列。想象一下数据如下图 A 列所示:
DataSheet
。
然后将您想要从该数据中排除的所有数字放入另一个工作表中,并将其命名为
ExcludeList
。
图 2:将所有要排除的数据放入排除列表中(这样您就可以轻松编辑它,而无需更改 VBA 代码)。
然后使用以下公式在数据工作表中添加辅助列
=IF(IFERROR(MATCH(A:A,ExcludeList!A:A,0),0)>0,TRUE,FALSE)
以便
A:A
指向您要过滤的数据,ExcludeList!A:A
指向您要排除的数字列表。现在,您要排除的数据值旁边有一个 TRUE
,您要显示的数据旁边有一个 FALSE
。
现在您可以轻松过滤该帮助列
ThisWorkbook.Worksheets("DataSheet").Columns("A:B").AutoFilter Field:=2, Criteria1:="FALSE"
并且所有应该排除的数据都是不可见的:
再次感谢大家的建议,但经过深思熟虑后,我找到了一个快速且更适合我的应用程序的解决方案。 我已经习惯了使用主页选项卡中的过滤器选项,并且完全忽略了数据选项卡上高级过滤器选项的存在!在对该功能的 VBA 代码进行了一些研究后,我得出了以下解决方案:
'Copies Column headings from "A9:O9" to "G1:U1"
Range("A9:O9").Select
Selection.Copy
Range("G1:U1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
'Sets filter parameters for column "B" in Column "H"
Range("H2").Value = "<-4095"
Range("H3").Value = ">4095"
Range("H4").Value = ">3"
Range("H5").Value = "<1"
'Advanced Filter of data based on above parameter values for "B" column
Range("A9:O" & LastRow).AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("G1:U5"), Unique:=False
' Clears the parameter contents from cells "G1" thru "U5"
Range("G1:U5").Select
Selection.ClearContents
我的数据从 B9 开始,因此我能够使用空白单元格 G1 到 U5 作为临时标准范围。 它就像一个魅力!
Selection.AutoFilter 字段:=2,Criteria1:=“<>印度”,运算符:=xlAnd,Criteria2:=“<>美国” 范围(“B2”).选择 ActiveCell.FormulaR1C1 =“ROW”
只需使用上面的行,我们将除印度和美国之外的所有国家/地区 namw 替换为 ROW <> - 不等于
因此在不会改变的标准中使用该词。