我有一个循环遍历列的宏并删除数组中我不想要的任何内容。问题是我想保留某些包含"可删除字符串的单元格"但也包含其他信息。
E.G:如果在我删除的数组中我有" VARO"。我不想删除带有" VARO - summary"的单元格,我只想删除仅包含" VARO"的单元格。不知道在我的代码中要改变什么来实现这一点!任何关于语法和效率的帮助都会很棒,因为我还在学习:)。
Dim tDelete As Variant
Dim sKeep As String, x As Long
Dim rngSearch As Range, c As Range
Dim i As Long, j As Long
tDelete = Array("ABEL", "VARO")
Dim delCell As Boolean
For x = Range("A" & Rows.Count).End(xlUp).Row To 5 Step -1
Set c = Range("A" & x).Cells
delCell = False
For j = LBound(tDelete) To UBound(tDelete)
If InStr(c.value, tDelete(j)) Then
delCell = True
End If
Next j
If deleteCell Then c.EntireRow.Delete shift:=xlShiftUp
Next x
我的预感是问题在于InStr功能,但我不知道如何改变它!
编辑:考虑使用StrComp但不确定适当的语法。那么在IF中我可以拥有
If StrComp(c.value, tDelete(j))=0 Then
答案 0 :(得分:2)
你快到了!如果你使用
IF StrComp(c.value, dontDelete(j),vbtextcompare) = 0 Then
这将找到确切的细胞匹配!
答案 1 :(得分:1)
Sub check_this()
Dim tDelete As Variant
Dim sKeep As String, x As Long
Dim rngSearch As Range, c As Range
Dim i As Long, j As Long
tDelete = Array("ABEL", "VARO")
Dim delCell As Boolean
For x = Range("A" & Rows.Count).End(xlUp).Row To 5 Step -1
Set c = Range("A" & x).Cells
delCell = False
For j = LBound(tDelete) To UBound(tDelete)
If c.Value = tDelete(j) Then
c.EntireRow.Delete shift:=xlShiftUp
End If
Next j
Next x
End Sub
答案 2 :(得分:0)
您还可以制作一个AND语句,以便" Varo"是的," Varo" (Varo之后的空格)False然后删除。
相反,您可以检查字符串是否包含" Varo"并且字符串的长度也等于" Varo"的长度。如果这两种情况属实,则删除。
答案 3 :(得分:0)
编辑如何简单地写:
If c.value = tDelete(j) Then
' delete the cell
这测试它是完全匹配的,因此具有完全" VARO"将删除但包含" .... VARO ..."的单元格不