vba - 如何在VBA中删除前一行或后一行非空的空行

标签 vba excel

我的工作表中有空行,我想以这样的方式删除它们:如果上面的行或下面的行不为空,我不想删除它们。我不想删除它们,因此在之前填充的行下方至少有 1 个空行。

我知道如何删除工作表中的空行:

Worksheets("Sheet1").Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

但我不知道如何改变它来实现它。需要一些关于如何执行此操作的指导。

我的代码现在看起来像这样:

For Each ws In Workbooks(newwb).Sheets
    If (ws.Name <> "Sheet1") And (ws.Name <> "Sheet2") And (ws.Name <> "Sheet3") Then
        'ws.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        lRow = ws.Range("A" & Rows.Count).End(xlUp).Row
        For i = 1 To lRow
            If Application.WorksheetFunction.CountA(ws.Range("A" & i & ":O" & i + 2)) = 0 Then
                If IsEmpty(rngBlanks) Then
                    Set rngBlanks = ws.Rows(i + 1)
                Else
                    Set rngBlanks = Union(rngBlanks, ws.Rows(i + 1))
                End If
            End If
        Next i
        rngBlanks.EntireRow.Delete
        Set rngBlanks = Nothing
    Else
        ws.Delete
    End If
Next

执行时,它给我一个运行时错误“5”:行中的过程调用或参数无效Set rngBlanks = Union(rngBlanks, ws.Rows(i+1))

最佳答案

I know how to delete empty rows in a sheet:

没有。这是删除空行的错误方法,并且当您有多列不起作用。您可能想查看 This 。此函数使用循环和 Application.WorksheetFunction.CountA。或者,您也可以使用.Autofilter

But I am not sure how to change this to implement that. Need some guidance on how to do this.

使用与上面链接中我的回答中所示相同的逻辑。

    '~~> Loop through the rows to find which range is blank
    For i = 1 To lRow
        '~~> Checking 3 rows in one go
        If Application.WorksheetFunction.CountA(Range("A" & i & ":J" & i + 2)) = 0 Then
            If rngBlanks Is Nothing Then
                Set rngBlanks = .Rows(i + 1)
            Else
                Set rngBlanks = Union(rngBlanks, .Rows(i + 1))
            End If
        End If
    Next i

编辑

请尝试这个(未经测试)

For Each ws In Workbooks(newwb).Sheets
    Select Case ws.Name
    Case "Sheet1", "Sheet2", "Sheet3"
    Case Else
        With ws
            lRow = .Range("A" & .Rows.Count).End(xlUp).Row

            For i = 1 To lRow
                If i = .Rows.Count - 1 Then Exit For

                If Application.WorksheetFunction.CountA(.Range("A" & i & ":O" & i + 2)) = 0 Then
                    If rngBlanks Is Nothing Then
                        Set rngBlanks = .Rows(i + 1)
                    Else
                        Set rngBlanks = Union(rngBlanks, .Rows(i + 1))
                    End If
                End If
            Next i

            If Not rngBlanks Is Nothing Then
                rngBlanks.Delete
                Set rngBlanks = Nothing
            End If
        End With
    End Select
Next ws

关于vba - 如何在VBA中删除前一行或后一行非空的空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36587310/

相关文章:

vba - 如何以编程方式更改 VBA 项目的条件编译属性

excel - 在 vba (Excel/OneDrive) 中关闭自动保存

vba - 如何更改许多 Excel 工作簿中的单个宏?

javascript - 使用 VBA 自动执行数据抓取以及在多框架 Intranet 网页内按下按钮

excel - (Excel) 在 Excel 中报告街道号码

excel - 是否可以针对特定公式启用Excel手动计算?

Excel VBA : Setting a Cell Address to a variable "Object Required Error"

vba - Outlook VBA 更改邮件字体大小

excel - 根据分数在类(class)之间分配学生

excel - 返回 Excel 用户窗体中组中选定的单选按钮