我不知道我哪里错了。如果这两个都是真的,我希望这段代码没有任何作用。如果只有颜色为真,我希望它删除该行。请告诉我有什么问题。
For lngRow = lngRows To 2 Step -1
If ActiveWorkbook.Worksheets("Sheet1").Cells(lngRow, "E").DisplayFormat.Interior.ColorIndex = 38 _
And InStr(1, Range("N" & lngRow), LCase("po box") > 0) Then
Else:
ActiveWorkbook.Worksheets("Sheet1").Rows(lngRow).EntireRow.Delete
End If
Next
我觉得我需要另一个If声明或什么?我尝试了很多不同的版本,但我无法得到它。感谢您的帮助。
我添加了这张照片。如果这更好地解释了它。我只需要用PO Box着色的行。但是那些根本没有着色的行,其中大多数都没有被描绘出来。 (这都是假信息)
答案 0 :(得分:2)
要仅删除已着色但不包含“po box”的行,请使用
If ActiveWorkbook.Worksheets("Sheet1").Cells(lngRow, "E").DisplayFormat.Interior.ColorIndex = 38 _
And InStr(1, LCase(Range("N" & lngRow)), "po box") = 0 Then
ActiveWorkbook.Worksheets("Sheet1").Rows(lngRow).EntireRow.Delete
End If
注意:您在一个字符串中搜索“{box}”的LCase
版本(已经全部小写)并非全部为小写也会给您带来问题。您应该在单元格的LCase
版本中检查“po box”。
答案 1 :(得分:0)
在我看来,Instr
可能是括号的位置。也许它应该是InStr(1, Range("N" & lngRow), LCase("po box")) > 0
?
我也建议使用If Not
:
If Not (ActiveWorkbook.Worksheets("Sheet1").Cells(lngRow, "E").DisplayFormat.Interior.ColorIndex = 38 & _
And InStr(1, Range("N" & lngRow), LCase("po box")) > 0) Then 'check your parenthesis here
ActiveWorkbook.Worksheets("Sheet1").Rows(lngRow).EntireRow.Delete
End If
答案 2 :(得分:0)
我相信两列.AutoFilter会更合适。
Dim lngRows As Long
lngRows = 6
With Worksheets("sheet4")
If .AutoFilterMode Then .AutoFilterMode = False
With .Range("E1:N" & lngRows)
.AutoFilter Field:=1, Criteria1:=RGB(255, 153, 204), Operator:=xlFilterCellColor
.AutoFilter Field:=10, Criteria1:="<>*po box*"
With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
If CBool(.Application.Subtotal(103, .Cells)) Then
.SpecialCells(xlCellTypeVisible).EntireRow.Delete
End If
End With
End With
If .AutoFilterMode Then .AutoFilterMode = False
End With