繁体   English   中英

循环遍历我的行,如果2个单元格包含任何值,则删除行

[英]Loop trought my rows and delete row if 2 cells contains any value

图片

想要使其成为for循环,如果它们都包含一个值,则迭代并比较A1与B1,否则跳过并跳转到下一行,将A2与B2比较,将A3与B3比较,依此类推,直到大约100。

您可以在示例IMAGE中看到,像A4包含一个值,但B4却不这样,A5和B5相同,但是A6和B6都包含一个值,因此我想删除该行并继续进行:)

Sub Empty_cell()

    Dim score1 As Integer, score2 As Integer, result As String, col As Integer, RangeStr1 As String, RangeStr2 As String

    col = 4
    Let RangeStr1 = "A" & col & ""
    Let RangeStr2 = "B" & col & ""

    score1 = Range(RangeStr1).Value
    score2 = Range(RangeStr2).Value

    If score1 >= 0 & score2 >= 0 Then

        Range(RangeStr1 & ":" & RangeStr2).Delete
        MsgBox "Deleted"
    Else
        MsgBox "Failed"
        col = col + 1 '

        MsgBox col
    End If
End Sub

您可以使用一个班轮声明来做到这一点:

Range("A4", Cells(Rows.count, "A").End(xlUp)).SpecialCells(xlCellTypeConstants, xlNumbers).Offset(, 1).SpecialCells(xlCellTypeConstants, xlNumbers).EntireRow.Delete

哪里:

  • Range("A4", Cells(Rows.count, "A").End(xlUp))

    引用从第4行到最后一个不为空的所有A列单元格

  • .SpecialCells(xlCellTypeConstants, xlNumbers)

    用数字选择所有引用的范围单元格

  • .Offset(, 1).

    将结果范围向右移动一列(即,移至B列的相应单元格)

  • .SpecialCells(xlCellTypeConstants, xlNumbers)

    选择后一个带有数字的范围单元格

  • .EntireRow.Delete

    最后删除结果单元格的行

user3598756解决方案完全没有问题。 只是提供一些健壮且可能适应的东西

让我知道你的想法:

Option Explicit
Dim n As Long
Dim i As Long
Sub DeleteRows()

n = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

For i = 4 To n
    Select Case Cells(i, 1)
        Case Is <> vbNullString
            Select Case Cells(i, 2)
                Case Is <> vbNullString
                    Cells(i, 1).EntireRow.Delete
            End Select
    End Select
Next i

End Sub

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM