需要我的代码帮助。 在(列C)中,I具有值MG01,MG02a,MG02b,MG02c。并在(A列)中有不同的值。如果A列中的值为" 1"则代码需要删除行。和 在C列中,如果它在文本末尾找到字母,如b,c,d,e,....
使用" c "代码无法识别MG02c请帮忙。
Sub xDeleteRowz()
Last = Cells(Rows.Count, "A").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "A").Value) = "1" And (Cells(i, "C").Value) = "*c*" Then
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub
答案 0 :(得分:1)
更改
try...finally
到
If (Cells(i, "A").Value) = "1" And (Cells(i, "C").Value) = "*c*" Then
或
If (Cells(i, "A").Value) = "1" And (Cells(i, "C").Value) like "*c*" Then
我还建议您对代码进行一些更改并开始明确编码:
If (Cells(i, "A").Value) = "1" And InStr(1, Cells(i, "C").Value, "c", vbTextCompare) Then
根据最新评论,更合适的方法可能如下:
Option Explicit
Sub xDeleteRowz()
Dim i As Long, Last As Long
Last = Cells(Rows.Count, "A").End(xlUp).Row
With ThisWorkbook.Worksheets(1)
For i = Last To 1 Step -1
If .Cells(i, "A").Value = "1" And InStr(1, .Cells(i, "C").Value, "c", vbTextCompare) Then
.Rows(i).EntireRow.Delete
End If
Next i
End With
End Sub
答案 1 :(得分:1)
这听起来更适合正则表达式:
Sub xDeleteRowz()
Last = Cells(Rows.Count, "A").End(xlUp).Row
With CreateObject("VBScript.RegExp")
.Pattern = "MG\d{2}[a-z]"
.IgnoreCase = False
For i = Last To 1 Step -1
If (Cells(i, "A").Value) = "1" And .Test(Cells(i, "C").Value) Then
Cells(i, "A").EntireRow.Delete
End If
Next i
End With
End Sub
请注意,表达式要求值以MG ##开头。如果值的起点可能不同,请替换此行...
.Pattern = "MG\d{2}[a-z]"
...与...
.Pattern = ".+[a-z]"
...它将匹配末尾带小写字母的任何内容。您还可以通过更改括号内的范围来限制特定字母。即,如果它只是' a'通过'',它将是:
.Pattern = ".+[a-g]"