如何循环访问特定列中的单元格,并根据其内容删除整行?

这是另一个问题,我觉得有一个简单的答案,但我没有find它。 我遍历D列中的每个单元格,并查找特定date(来自用户input),并基于该单元格的date和某些相应单元格中的date,我正在更改该行的颜色。

而不是着色的行,我想能够删除它。

这是我的代码,目前着色这些行:

Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp)).Select Dim rCell, otherCell As Range Dim TheAnswer$ Dim ConvertedDate# TheAnswer = InputBox("In M/D/YY format, enter the first day of the month for which this report is being run." & _ vbCr & vbCr & "For example, you would enter ""12/1/2012"" for the December 2012 report.", "Enter Date M/D/YY") ConvertedDate = CDate(TheAnswer) For Each rCell In Selection If rCell.Value <> "" Then If rCell.Value And rCell.Offset(, 5) < ConvertedDate And rCell.Offset(, 5) <> "" Then rCell.EntireRow.Interior.Color = RGB(255, 102, 0) If rCell.Value < ConvertedDate And rCell.Offset(, 5) = "" Then If rCell.Offset(, 6) < ConvertedDate And rCell.Offset(, 6) <> "" Then rCell.EntireRow.Interior.Color = RGB(255, 102, 0) If rCell.Offset(, 7) < ConvertedDate And rCell.Offset(, 7) <> "" Then rCell.EntireRow.Interior.Color = RGB(255, 102, 0) End If End If Next rCell 

当我用rCell.EntireRow.Interior.Color = RGB(255, 102, 0) rCell.EntireRow.Delete rCell.EntireRow.Interior.Color = RGB(255, 102, 0) rCell.EntireRow.Delete我得到错误。 如果我通过代码,我可以看到它确实删除了第一行,但是在下一行出错。

我显然有多个“If Then”语句,但是如果满足第一个“If Then”条件并且该行被删除,则它应该移动到下一个单元格。 在我看来,它仍然试图检查那个被删除的行。 我绝对不是一个Excel VBA专家,但是我认为在rCell.EntireRow.Delete之后应该添加rCell.EntireRow.Delete内容,告诉它移动到下一个单元格。 我尝试添加Next rCell ,但是出错了。 Exit For只是完全停止macros。

看下面,我更新了一些重复的条件。 最好的两种方法是将数据添加到variables数组中,然后删除进程然后删除,或者在评论中添加标记,filter然后批量删除。 我更喜欢下面的内容,因为它可以很好地扩展,并且是知道其他数据操作的好习惯。 不作为没有模板的基础上。 见下文:

 Dim data() As Variant Dim i As Double Dim deleteRows As Range Dim sht As Worksheet Dim theAnswer As String Dim ConvertedDate As Date Set sht = Sheet1 theAnswer = InputBox("In M/D/YY format, enter the first day of the month for which this report is being run." & _ vbCr & vbCr & "For example, you would enter ""12/1/2012"" for the December 2012 report.", "Enter Date M/D/YY") ConvertedDate = CDate(theAnswer) data = sht.Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp)).Resize(, 8) For i = 1 To UBound(data, 1) If (data(i, 1) <> vbNullString) Then If data(i, 1) <> vbNullString And data(i, 6) < ConvertedDate And data(i, 6) <> vbNullString Then If (deleteRows Is Nothing) Then Set deleteRows = sht.Rows(i + 1) Else Set deleteRows = Union(deleteRows, sht.Rows(i + 1)) End If ElseIf data(i, 1) < ConvertedDate And data(i, 7) <> vbNullString And data(i, 8) <> vbNullString Then If data(i, 7) < ConvertedDate Or data(i, 8) < ConvertedDate Then If (deleteRows Is Nothing) Then Set deleteRows = sht.Rows(i + 1) Else Set deleteRows = Union(deleteRows, sht.Rows(i + 1)) End If End If End If End If Next i deleteRows.Delete Shift:=xlUp 

迈克,当你循环通过范围集和删除行,你需要从最后一行向后循环,因为当你删除一个行它不再存在,它通过Excel,因为该行不在循环范围。

尝试这个:

 Range(Cells(2, 4), Cells(Rows.Count, 4).End(xlUp)).Select Dim rCell as Range, otherCell As Range Dim TheAnswer$ Dim ConvertedDate# TheAnswer = InputBox("In M/D/YY format, enter the first day of the month for which this report is being run." & _ vbCr & vbCr & "For example, you would enter ""12/1/2012"" for the December 2012 report.", "Enter Date M/D/YY") ConvertedDate = CDate(TheAnswer) Dim xrows as Long, i as Long xrows = Selection.Rows.Count For i = xrows to 1 Step -1 Set rCell = Selection.Cells(i,1) If rCell.Value <> "" Then If rCell.Value And rCell.Offset(, 5) < ConvertedDate And rCell.Offset(, 5) <> "" Then rCell.EntireRow.Delete If rCell.Value < ConvertedDate And rCell.Offset(, 5) = "" Then If rCell.Offset(, 6) < ConvertedDate And rCell.Offset(, 6) <> "" Then rCell.EntireRow.Delete If rCell.Offset(, 7) < ConvertedDate And rCell.Offset(, 7) <> "" Then rCell.EntireRow.Delete End If End If Next i 

不知道如果你想全部是EntireRow.Delete ,但你可以很容易地切换回来。