删除行时处理合并的单元格

时间:2014-11-11 17:38:53

标签: vba excel-vba excel

我正在编写一个模块,用于从单个工作簿中的多个工作表中删除不需要的文本。我拼凑在一起足以删除具有特定字体类型的行和空的行;但是,我遇到了障碍。

工作表有许多合并的单元格。我想根据关键短语删除特定行。例如,"评论"在A列的任何位置都可以删除该行。但是,如果评论在A2:A4之间合并,则B3:B4中的文字仍然存在,将垃圾留在我不想要的表格中。

有没有办法删除合并的单元格,以及该单元格右侧的所有行,如果列A中的值是我要查找的任意数量的关键字?

这是我到目前为止所拥有的......

Sub Delete_Rows_Courier()
    Dim ws As Excel.Worksheet
    Dim LastRow As Long
    Dim i As Integer

    For Each ws In Application.ThisWorkbook.Worksheets
        LastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
        i = 1
        Do While i <= LastRow
            If ws.Range("A" & i).Font.Name = "Courier New" Then
                ws.Rows(i).Delete
                i = i - 1
                LastRow = LastRow - 1
            End If
            i = i + 1
        Loop
    Next


End Sub


Sub Delete_Empty_Rows()
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim i As Long

    For Each ws In Application.ThisWorkbook.Worksheets
            'Deletes the entire row within the selection if the ENTIRE row contains no data.
            'We use Long in case they have over 32,767 rows selected.

            'We turn off calculation and screenupdating to speed up the macro.
    With Application
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
            'We work backwards because we are deleting rows.
            For i = ws.UsedRange.Rows.Count To 1 Step -1
            If WorksheetFunction.CountA(ws.Rows(i)) = 0 Then
            ws.Rows(i).EntireRow.Delete
            End If
            Next i

            .Calculation = xlCalculationAutomatic
            .ScreenUpdating = True
        End With
        Next ws
End Sub

Sub RunMacros()
Delete_Empty_Rows
Delete_Rows_Courier
End Sub

1 个答案:

答案 0 :(得分:1)

With .Range("A" & i).Mergearea
    x = .Rows.Count     'if you need to know how many rows were deleted
    .EntireRow.Delete   'delete merged rows 
End With