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

图片

想把它作为一个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 

你可以用一个class轮语句来做:

 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)

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

  • .Offset(, 1).

    产生的范围向右移动一列(即到列B的相应单元格)

  • .SpecialCells(xlCellTypeConstants, xlNumbers)

    用数字select后面引用的范围单元格

  • .EntireRow.Delete

    最后删除产生的单元格的行

根本就没有任何user3598756解决scheme的错误。 只是提供一些有用的,可能适应性强的东西

让我知道你的想法:

 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