Vba 删除不带';t包含任一单元格I1 J1 K1中的值
我在AC23-AC130中有2,5,8和7,8,8格式的字符串数组 我正在寻找代码来设置必须根据I1:K1检查的数据,如果该值没有1匹配,则必须删除所有与I1不匹配的字符串数组J1 K1Vba 删除不带';t包含任一单元格I1 J1 K1中的值,vba,excel,Vba,Excel,我在AC23-AC130中有2,5,8和7,8,8格式的字符串数组 我正在寻找代码来设置必须根据I1:K1检查的数据,如果该值没有1匹配,则必须删除所有与I1不匹配的字符串数组J1 K1 Sub Tester() Dim sKeep As String, x As Long Dim rngSearch As Range, c As Range 'J1:K1 has values to keep and checked against arrays Ac23-ac130 sKe
Sub Tester()
Dim sKeep As String, x As Long
Dim rngSearch As Range, c As Range
'J1:K1 has values to keep and checked against arrays Ac23-ac130
sKeep = Chr(0) & Join(Application.Transpose(Range("j1:k1").Value), _
Chr(0)) & Chr(0)
Set rngSearch = Range("AC23:AC130")
For x = rngSearch.Cells.Count To 1 Step -1
Set c = rngSearch.Cells(x)
If InStr(sKeep, Chr(0) & c.Value & Chr(0)) = 0 Then
c.Delete shift:=xlShiftUp
End If
Next x
End Sub
我在按钮上尝试了代码,但没有运行
因此,必须对照字符串范围检查I1 J1 K1中的正常数字。
例如,如果I1 J1 K1是8 7 7,则必须删除不包含8 7 7的所有字符串。示例:2,4,1不包含8,7,因此必须将其删除。4,5,8包含一个8,因此不应将其删除
Sub Tester()
Dim sKeep As String, x As Long
Dim rngSearch As Range, c As Range
'J1:K1 has values to keep and checked against arrays Ac23-ac130
sKeep = Chr(0) & Join(Application.Transpose(Range("j1:k1").Value), _
Chr(0)) & Chr(0)
Set rngSearch = Range("AC23:AC130")
For x = rngSearch.Cells.Count To 1 Step -1
Set c = rngSearch.Cells(x)
If InStr(sKeep, Chr(0) & c.Value & Chr(0)) = 0 Then
c.Delete shift:=xlShiftUp
End If
Next x
End Sub
您可以使用
AutoFilter()
过滤不符合“I1”和“J1”值的单元格,然后循环通过这些过滤后的单元格并收集(使用Union()
)不符合“K1”值的单元格)
子测试仪()
尺寸偏差作为范围,c作为范围
带量程(“AC22:AC130”)
.AutoFilter字段:=1,准则1:=“*”&范围(“I1”)。Value&“*”,运算符:=xlAnd,准则2:=“*”&范围(“J1”)。Value&“*”下面的代码在列AC(“AC23:AC130”)中的所有单元格中循环,并且对于每个单元格,它都使用拆分将值放入数组中
然后,它检查范围(“I1:K1”)中的每个单元格,并使用应用程序.Match
查看数组是否存在于其中一个单元格中。如果有,则不删除,如果不匹配,则删除当前单元格
代码
Sub Tester()
Dim sKeep As String, x As Long
Dim rngSearch As Range, c As Range
Dim CelArr As Variant, DelFlag As Boolean
Set rngSearch = Range("AC23:AC130")
For x = rngSearch.Rows.Count To 1 Step -1
CelArr = Split(rngSearch.Item(x), ",") '<-- convert values in column AC to array
DelFlag = True '<-- init delete flag
If Len(Join(CelArr)) > 0 Then '<-- check if array (current cell) is empty
For Each c In Range("I1:K1").Cells
If Not IsError(Application.Match(CStr(c.Value), CelArr, 0)) Then '<-- if there is at least 1 match, don't delete the cell
DelFlag = False '<-- don't delete current cell with a match
Exit For
End If
Next c
End If
If DelFlag Then rngSearch.Item(x).Delete shift:=xlShiftUp
Next x
End Sub
子测试仪()
将sKeep变暗为字符串,x变长
变暗RNG搜索范围,c搜索范围
Dim CelArr作为变量,DelFlag作为布尔值
设置rngSearch=范围(“AC23:AC130”)
对于x=rngSearch.Rows.Count到1步骤-1
CelArr=Split(rngSearch.Item(x),“,”)我想确保我理解,您想检查列AC(“AC23:AC130”)中的每个单元格,它的内容不等于至少一个单元格“I1:K1”,然后删除该单元格。如果其中一个数值与其中一个单元格匹配,则不删除它,对吗?AC列中的每个单元格都有几个数字?使用,
作为分隔符?您在我下面的回答中测试过te代码吗?任何反馈?工作完美无瑕谢谢你非常感谢先生…伟大的编码@Ricklou不客气,请标记为答案(单击我答案旁边的V)@Ricklou你为什么去掉我答案的V?