如果列D中的单元格为空,则Excel VBA将删除整行

时间:2015-07-03 11:46:20

标签: excel vba excel-vba

任何人都可以告诉我如何编写脚本来删除整行,如果列D中的单元格D =“”在D13:D40中的表3中。

此外,如果区域中的单元格已被删除且其他单元格现在位于D13:D40范围内,如何防止用户再次意外运行脚本?

6 个答案:

答案 0 :(得分:1)

解决方案:这对我有用:

Sub DeleteRowsWithEmptyColumnDCell()
    Dim rng As Range
    Dim i As Long
    Set rng = ThisWorkbook.ActiveSheet.Range("D13:D40")
    With rng
        ' Loop through all cells of the range
        ' Loop backwards, hence the "Step -1"
        For i = .Rows.Count To 1 Step -1
            If .Item(i) = "" Then
                ' Since cell is empty, delete the whole row
                .Item(i).EntireRow.Delete
            End If
        Next i
    End With
End Sub

说明:在D列的for中的所有单元格中运行Range循环,如果单元格值为空,则删除整行。 重要提示:在循环显示行并根据其内容删除部分行时,您需要向后循环,而不是向前循环。如果您继续前进并删除一行,则所有后续行都会获得不同的行号(-1)。如果你有两个空单元格彼此相邻,那么只会删除第一行中的第一行,因为第二行会向上移动一行,但循环将在下一行继续。

答案 1 :(得分:1)

无需循环:

Sub SO()

Static alreadyRan As Integer

restart:

If Not CBool(alreadyRan) Then
    With Sheets("Sheet3")
        With .Range("D13:D40")
            .AutoFilter 1, "="
            With .SpecialCells(xlCellTypeVisible)
                If .Areas.Count > 1 Then
                    .EntireRow.Delete
                    alreadyRan = alreadyRan + 1
                End If
            End With
        End With
        .AutoFilterMode = False
    End With
Else
    If MsgBox("procedure has already been run, do you wish to continue anyway?", vbYesNo) = vbYes Then
        alreadyRan = 0
        GoTo restart:
    End If
End If

End Sub

使用AutoFilter查找空白单元格,然后使用SpecialCells删除结果。使用Static变量来跟踪程序的运行时间。

答案 2 :(得分:0)

这是我对它的看法。请参阅代码中的注释,了解在此过程中会发生什么。

Sub deleterow()
  ' First declare the variables you are going to use in the sub
  Dim i As Long, safety_net As Long
  ' Loop through the row-numbers you want to change.
  For i = 13 To 40 Step 1
    ' While the value in the cell we are currently examining = "", we delete the row we are on
    ' To avoid an infinite loop, we add a "safety-net", to ensure that we never loop more than 100 times
    While Worksheets("Sheet3").Range("D" & CStr(i)).Value = "" And safety_net < 100
      ' Delete the row of the current cell we are examining
      Worksheets("Sheet3").Range("D" & CStr(i)).EntireRow.Delete
      ' Increase the loop-counter
      safety_net = safety_net + 1
    Wend
    ' Reset the loop-counter
    safety_net = 0
  ' Move back to the top of the loop, incrementing i by the value specified in step. Default value is 1.
  Next i
End Sub

为了防止用户意外地运行代码,我可能只是在模块顶部添加Option Private Modulepassword-protect the VBA-project,但是它再次不是 首先容易出错。

答案 3 :(得分:0)

我最近不得不写类似的东西。我不确定下面的代码是非常专业的,因为它涉及在单元格J1中存储一个值(显然这可以改变),但它将完成你需要的工作。我希望这会有所帮助:

Sub ColD()

Dim irow As long
Dim strCol As String

Sheets("sheet2").Activate
If Cells(1, 10) = "" Then
    lrun = " Yesterday."
Else: lrun = Cells(1, 10)
End If

MsgBox "This script was last run: " & lrun & "  Are you sure you wish to     continue?", vbYesNo
If vbYes Then
    For irow = 40 To 13 step -1
        strCol = Cells(irow, 4).Value
        If strCol = "" Then
            Cells(irow, 4).EntireRow.Delete
        End If
    Next
    lrun = Now()
    Cells(1, 10) = lrun
Else: Exit Sub
End If
End Sub

答案 4 :(得分:0)

此代码通过工作表上的按钮执行,该工具一旦运行,就会从工作表中删除该按钮,使其无法再次运行。

Sub DeleteBlanks()
    Dim rw As Integer, buttonID As String

    buttonID = Application.Caller

    For rw = 40 To 13 Step -1

        If Range("D" & rw) = "" Then
            Range("D" & rw).EntireRow.Delete
        End If

    Next rw

    ActiveSheet.Buttons(buttonID).Delete
End Sub

您需要在电子表格中添加一个按钮并将宏指定给它。

答案 5 :(得分:0)

无需循环或过滤器即可找到指定范围内的空白单元格。 Range.SpecialCells属性可用于查找Range中的任何空白单元格以及Range.EntireRow属性以删除这些单元格。为了保留运行状态,代码将注释添加到范围中的第一个单元格。即使工作簿已关闭(假设已保存),这也将保留运行状态。

html4css1.css