简体   繁体   English

当多列中的单元格为空或 0 时删除整行

[英]Delete entire rows when cells in multiple columns are blank or 0

I am trying to write a code which basically looks at rows 13-33 and deletes the entire row if the cells in Columns BM are all Blank AND column A is NOT blank.我正在尝试编写一个代码,它基本上查看第 13-33 行,如果 BM 列中的单元格都是空白且 A 列不是空白,则删除整行。

The first code which I have written below deletes the entire row only if the cell in Column B is blank but I need all the cells in BM to be blank in order to delete the entire row.我在下面编写的第一个代码仅当 B 列中的单元格为空时才删除整行,但我需要 BM 中的所有单元格都为空才能删除整行。

Sub scheduleA()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

Sheets("Schedule A Template").Select

Dim RowstoDelete As Long
x = 33
For RowstoDelete = Cells(x, 2).End(xlUp).Row To 13 Step -1

If (Cells(RowstoDelete, 2).Value = "0") And (Cells(RowstoDelete, 1).Value <> "") Then
        Rows(RowstoDelete).Delete Shift:=xlUp
    End If


Next RowstoDelete
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

I tried writing it differently as well in the following code but can't achieve the desire result.我尝试在以下代码中以不同的方式编写它,但无法达到预期的结果。

Sub DeleteRows()


Dim i As Integer

For i = 33 To 13 Step -1
  If WorksheetFunction.CountA(Range("B" & i, "M" & i)) = 0 And WorksheetFunction.CountA(Range("A" & i)) <> "" Then
     Rows(i).EntireRow.Delete
    End If

Next i

End Sub

Please help!请帮忙!

Your conditions for row deletion are: column A not blank, columns B to M blank.您的行删除条件是:A 列不为空,B 到 M 列为空。 Then something like this should do the trick:那么这样的事情应该可以解决问题:

Sub ScheduleA()
    On Error GoTo errHandler

    Const TOP_ROW As Long = 13
    Const BOTTOM_ROW As Long = 33

    Dim rowIndex As Long

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    With ThisWorkbook.Worksheets("Schedule A Template")
        For rowIndex = .Cells(BOTTOM_ROW, "A").End(xlUp).Row To TOP_ROW Step -1
            If Not IsEmpty(.Cells(rowIndex, "A").Value2) Then '...column A is not blank.
                If Application.WorksheetFunction.CountA(.Range(.Cells(rowIndex, "B"), .Cells(rowIndex, "M"))) = 0 Then '...all cells on row rowIndex from columns B to M are blank.
                    .Rows(rowIndex).Delete Shift:=xlUp
                End If
            End If
        Next
    End With

Cleanup:
    On Error Resume Next
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    Exit Sub

errHandler:
    MsgBox Err.Description, vbExclamation + vbOKOnly, "Error"
    Resume Cleanup
End Sub

Note that the .Select is gone;注意.Select不见了; you almost never have to select anything to get the job done.您几乎不需要选择任何东西来完成工作。 Not relying on the selection will make your code much more robust.不依赖选择将使您的代码更加健壮。 In the code above, the With block tells the code within it to refer to the target worksheet whenever an expression starts with a period, such as .Cells .在上面的代码中, With块告诉其中的代码在表达式以句点开头时引用目标工作表,例如.Cells

Also, when turning off ScreenUpdating and Calculation , systematically include error handling to turn them back on.此外,当关闭ScreenUpdatingCalculation ,系统地包括错误处理以重新打开它们。 This way, if something goes wrong, your code won't leave Excel in an undesirable state.这样,如果出现问题,您的代码不会让 Excel 处于不良状态。

Finally, instead of referring to worksheets by their tab's name (as seen from Excel), you can refer to them directly using their CodeName, as seen from the VBA editor, in the Properties window, under the worksheet's (Name) property (press Ctrl+R to show the Project Explorer, click on the worksheet under the Microsoft Excel Objects node, then press F4 to display the Properties window).最后,您可以直接使用它们的 CodeName(如从 VBA 编辑器中看到的)在“属性”窗口中工作表的(名称)属性下(按 Ctrl +R 显示项目资源管理器,单击 Microsoft Excel 对象节点下的工作表,然后按 F4 显示属性窗口)。 You can change this value;您可以更改此值; I'd typically change it to shtScheduleATemplate.我通常会将其更改为 shtScheduleATemplate。 Then, the With line could be re-written as:然后, With行可以重写为:

With shtScheduleATemplate

...which would still work even after you changed the worksheet's name from Excel. ...即使您从 Excel 更改了工作表的名称,它仍然可以工作。

EDIT : in your question's code, you are checking column B when determining at which bottom row index to start the loop.编辑:在您问题的代码中,您在确定从哪个底行索引开始循环时正在检查列 B。 However, by doing so, you may miss some rows that should be deleted.但是,这样做可能会遗漏一些应该删除的行。 I've changed my answer to check within column A instead:我已将答案更改为在 A 列内进行检查:

For rowIndex = .Cells(BOTTOM_ROW, "A").End(xlUp).Row To TOP_ROW Step -1

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

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