我在这里是因为(大约两天)我在代码上,需要帮助。
我的脚本的目标是过滤“ Check_Column”列中没有数字的行。
问题在于过滤器无法“正常”工作(它怀疑自己每半工作一次)。
我的代码是:
Sub hide_numbers()
Dim WorkBk As Workbook, WorkSh As Worksheet, FilterRow As Variant
Set WorkSh = Sheets("DataBase")
WorkSh.Activate
FilterRow = Rows("1:1").Find(what:="Check_Column", lookat:=xlWhole).Column
WorkSh.UsedRange.AutoFilter Field:=FilterRow, Criteria1:=Array("*1*","*2*","*3*","*4*","*5*","*6*", "*7*","*8*","*9*")
End Sub
如果我使用(*1*","*2*")
,则可以使用,但是如果我使用("*1*","*2*","*3*","*4*","*5*","*6*", "*7*","*8*","*9*")
,则不能使用。为什么?
这是excel屏幕截图(作为示例)。
我想要这个输出(是一个例子)。
请有人告诉我我错了吗?
谢谢。
弗朗切斯科
答案 0 :(得分:1)
以下宏使用Dictionary对象从Check_Column收集包含数字的唯一值,然后过滤这些值。
顺便说一句,由于要显示Check_Column中的值包含数字的行,因此将子项的名称从hide_numbers更改为show_numbers可能更合适。
Option Explicit
Sub hide_numbers()
Dim criteriaDictionary As Object
Set criteriaDictionary = CreateObject("Scripting.Dictionary")
Dim criteriaArray As Variant
criteriaArray = Array("*1*", "*2*", "*3*", "*4*", "*5*", "*6*", "*7*", "*8*", "*9*")
Dim databaseWorksheet As Worksheet
Set databaseWorksheet = Worksheets("DataBase")
With databaseWorksheet
Dim fieldIndex As Long
fieldIndex = .Rows("1:1").Find(what:="Check_Column", lookat:=xlWhole).Column
Dim currentCell As Range
Dim currentItem As Variant
For Each currentCell In .Range(.Cells(2, fieldIndex), .Cells(.Rows.Count, fieldIndex).End(xlUp)).Cells
For Each currentItem In criteriaArray
If currentCell.Value Like currentItem Then
criteriaDictionary(currentCell.Value) = ""
Exit For
End If
Next currentItem
Next currentCell
If criteriaDictionary.Count > 0 Then
With .UsedRange
.AutoFilter field:=fieldIndex, Criteria1:=criteriaDictionary.keys(), Operator:=xlFilterValues
End With
Else
MsgBox "No records found!", vbExclamation
End If
End With
Set criteriaDictionary = Nothing
Set databaseWorksheet = Nothing
End Sub
答案 1 :(得分:0)
MS Excel在数组值中不支持通配符。您可以使用数字创建所有值的数组,并使用以下代码:
WorkSh.UsedRange.AutoFilter Field:=FilterRow, Criteria1:=myArray, Operator:=xlFilterValues
或者,您可以在数据集中添加包含相关公式的True / False值的新列,并添加一些公式,并简化代码,如下所示:
WorkSh.UsedRange.AutoFilter Field:=FilterRow, Criteria1:=True