简体   繁体   English

如果单元格值等于“”,如何编写 Excel 宏以删除整行

[英]How to Write Excel Macro to Delete Entire Row if cell Value Equals “”

I would like to have a macro that deletes an entire row of the cell value equals "" for multiple ranges.我想要一个宏来删除整个单元格值等于“”的多个范围。 Ranges are "B16:B115, B131:B230, B250:B349".范围是“B16:B115、B131:B230、B250:B349”。

Logic: If cell equals "" then delete the entire row.逻辑:如果单元格等于“”,则删除整行。

I want the row actually deleted and not just the contents of the cells.我希望实际删除该行,而不仅仅是单元格的内容。

Thank you.谢谢你。

This would be worth trying:这值得一试:

On Error Resume Next
Range("B16:B115,B131:B230,B250:B349").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error Goto 0

Depending on whether the "" cells have formulas or are just empty.取决于“”单元格是否有公式或只是空的。 Range.SpecialCells: What does xlCellTypeBlanks actually represent? Range.SpecialCells:xlCellTypeBlanks 究竟代表什么?

EDIT: if you have formulas then you have to go the long way round:编辑:如果你有公式,那么你必须 go 很长的路要走:

Sub DeleteEmpty()

    Dim c As Range, rngDel As Range

    For Each c In Range("B16:B115,B131:B230,B250:B349").Cells
        If Len(c.Value) = 0 Then
            If rngDel Is Nothing Then
                Set rngDel = c
            Else
                Set rngDel = Application.Union(rngDel, c)
            End If
        End If
    Next c

    If Not rngDel Is Nothing Then rngDel.EntireRow.Delete

End Sub

Following sub will allow you to select range to delete blank rows.以下 sub 将允许您在 select 范围内删除空白行。

Sub RemoveBlanks()
Dim rng As Range, rws As Long, i As Long
Dim LastRow As Range
Dim myRange As Range

  Set myRange = Application.InputBox(prompt:="Select Header Cell To Remove Blanks.", Type:=8)
  'LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Set LastRow = Cells(Rows.Count, myRange.Column).End(xlUp)
  Set rng = ActiveSheet.Range(myRange.Address & ":" & LastRow.Address)
  rws = rng.Rows.Count

  For i = rws To 1 Step (-1)
    If WorksheetFunction.CountA(rng.Rows(i)) = 0 Then rng.Rows(i).EntireRow.Delete
  Next

  Set myRange = Nothing
  Set LastRow = Nothing
  Set rng = Nothing

End Sub

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

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