简体   繁体   English

如何遍历特定列中的单元格并根据其内容删除整行?

[英]How do I loop through cells in a specific column and delete the entire row based on its contents?

This is another question where I feel like there's a simple answer, but I'm not finding it. 这是我觉得有一个简单答案的另一个问题,但我找不到。 I'm looping through each cell in column D and looking for specific dates (from the user input) and based on that cell's date and the dates in some corresponding cells, I'm changing the color of the row. 我遍历D列中的每个单元格,并查找特定日期(从用户输入中),并基于该单元格的日期和某些相应单元格中的日期,更改行的颜色。

Instead of coloring the row, I'd like to be able to delete it. 除了希望为行着色之外,我希望能够删除它。

Here's my code that currently colors those rows: 这是我当前为这些行着色的代码:

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

When I substitute rCell.EntireRow.Interior.Color = RGB(255, 102, 0) with rCell.EntireRow.Delete I get errors. 当我将rCell.EntireRow.Interior.Color = RGB(255, 102, 0) rCell.EntireRow.Delete rCell.EntireRow.Interior.Color = RGB(255, 102, 0)替换为rCell.EntireRow.Delete出现错误。 If I step through the code I can see that it actually does delete the first row, but then errors out at the next line. 如果我单步执行代码,可以看到它确实删除了第一行,但是在下一行出错了。

I obviously have multiple "If Then" statements, but if the first "If Then" condition is satisfied and the row is deleted, it should move on to the next cell. 我显然有多个“ If Then”语句,但是如果满足第一个“ If Then”条件并删除了该行,则应继续到下一个单元格。 It seems to me that it's still trying to check that row that's been deleted. 在我看来,它仍在尝试检查已删除的行。 I'm definitely not an Excel VBA expert, but I'm thinking there should be something to add after the rCell.EntireRow.Delete piece that tells it to move to the next cell. 我绝对不是Excel VBA专家,但我认为rCell.EntireRow.Delete部分之后应添加一些内容,以rCell.EntireRow.Delete其移至下一个单元格。 I tried adding in Next rCell , but that errors out. 我尝试添加Next rCell ,但是出错了。 Exit For just stops the macro altogether. Exit For完全停止宏。

Mike, when you loop through range sets and delete rows, you need to loop backward from the last row because when you delete a row it doesn't exist anymore and it throughs Excel because that row is no longer in the loop range. 迈克,当您遍历范围集并删除行时,您需要从最后一行开始向后遍历,因为当您删除某行时,该行不再存在,并且它通过Excel,因为该行不再在循环范围内。

Try this: 尝试这个:

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

Not sure if you wanted all to be EntireRow.Delete , but you can easily switch it back. 不知道是否要全部都是EntireRow.Delete ,但是可以轻松地将其切换回去。

See below, I updated and changed some duplicate conditions. 参见下文,我更新并更改了一些重复的条件。 The best two ways are either add the data into a variant array, process and then delete or as brettdj mentions in the comment add a flag, filter and then mass delete. 最好的两种方法是将数据添加到变量数组中,进行处理,然后删除,或者如brettdj在评论中所述,添加标志,过滤器然后批量删除。 I prefer the below as it scales well and is good practise to know for other data manipulation. 我喜欢以下内容,因为它可以很好地扩展,并且是其他数据操作的好习惯。 Not tested as no template to base it on. 未测试为没有模板可作为基础。 See below: 见下文:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据其中一个单元格的内容为整行着色 - Colour an entire row based on the contents of one of its cells 宏/VBA:根据列中的值清除行中的单元格,并循环遍历整列 - Macro/VBA: Clear cells in a row based on values in a column, and loop through entire column 如果特定列中的单元格有数据,如何告诉 Excel 宏删除整行? - How do I tell an excel macro to delete the entire row if cell in a specific column have data? 尝试遍历 B 列,如果一行中的两个单元格包含相同的文本,则删除第一个整行并保留第二个 - Trying to loop through column B and if two cells in a row contain the same text then delete the first ones entire row and keep the second 如果在vba中包含特定字符串,我如何循环遍历单元格并创建新行? - How do I loop through cells and create a new row if it contains a specific string in vba? 如果两个单元格包含特定文本,如何删除整行 - How to delete an entire row if two cells contain specific text VBA 尝试遍历列并删除整行(如果它们包含值) - VBA Trying to loop through a column and delete the entire row if they contain a value 如何遍历行中的单元格以添加到字符串? - How do I loop through the cells in a Row to add to a string? VBA如何根据同一行中其他两个单元格的内容编辑单元格内容 - VBA how do I edit cell contents based on contents of two other cells in same row Excel / VB-如何遍历每个行/列,并根据值进行格式化? - Excel / VB - How do I loop through each row/column and do formatting based on the value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM