我希望在我的数据中找到一行,该行对应于名为Val4的参考单元格并删除该范围。我有这个用于删除整行或只是该行的内容,但我想删除范围(“L:M”)而不是整行。这是我的代码的一部分:
Sheets("Sheet1").Select
Last = Cells(Rows.Count, "L").End(xlUp).Row
For I = Last To 1 Step -1
If (Cells(I, "L").Value) = Val4 Then
'Cells(I, "L").ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
'Cells(I, "A").EntireRow.Delete USE THIS TO DELETE ENTIRE ROW
GoTo NextSheet1
End If
Next I
感谢您的帮助。
罗斯
答案 0 :(得分:0)
你可以使用
Intersect(Sheets("Sheet1").Range("L:M"), Sheets("Sheet1").Rows(i)).ClearContents
但要加快代码速度,您可以使用AutoFilter()
而不是循环遍历单元格:
With Sheets("Sheet1") '<--1 reference your sheet
With .Range("M1", .Cells(.Rows.Count, "L").End(xlUp)) '<--| reference its column L:M cells from row 1 down to column "L" last not empty row
.AutoFilter field:=1, Criteria1:=Val4 '<--| filter referenced range on its 1st column (i.e. column "L") with Val4
If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then '<--| if any cell filtered other than header one (in row1)
.Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).ClearContents '<--|clear filtered referenced cells
End If
End With
.AutoFilterMode = False
End With