A1没有被检测到,并且区分大小写

我得到了一个朋友的帮助下的代码,但它没有检测到第一行。 此代码将删除不以某些字母开头的行。

我必须多次运行它才能删除不必要的单元格,但A1(其中有一个要删除的项目)未被删除。

Sub colortargets() Range("A1").End(xlDown).Offset(1, 0).Activate Do Until ActiveCell.Row = 1 If Left(ActiveCell.Value, 2) <> "AB" And Left(ActiveCell.Value, 2) <> "AC" And Left(ActiveCell.Value, 2) <> "AD" Then Rows(ActiveCell.Row).Delete End If ActiveCell.Offset(-1, 0).Activate Loop Range("A1").Select End Sub 

另外,有没有办法让这个代码不区分大小写? 这是我第一次绕过VBA,所以我不知道该怎么做。

试试这是因为Do Until ActiveCell.Row = 1条件,当它到达第一行退出循环,而不处理

您可能希望避免Activate / ActiveXXX模式,并使用不同的循环方法:

 Option Explicit Sub colortargets() Dim val As String Dim iRow As Long For iRow = Cells(Rows.count, 1).End(xlUp).row To 1 Step -1 val = Left(Cells(iRow, 1).Value, 2) If val <> "AB" And val <> "AC" And val <> "AD" Then Rows(iRow).Delete Next End Sub 

如果你必须使其不区分大小写:

 Option Explicit Sub colortargets() Dim val As String Dim iRow As Long For iRow = Cells(Rows.count, 1).End(xlUp).row To 1 Step -1 val = UCase(Left(Cells(iRow, 1).Value, 2)) If val <> "AB" And val <> "AC" And val <> "AD" Then Rows(iRow).Delete Next End Sub