Excel VBA - Foreach循环内的Foreach循环(循环遍历表格,删除空行)

时间:2015-01-28 09:54:23

标签: excel vba excel-vba foreach

我对VB很陌生并且有一个可能很简单的问题 - 我需要浏览工作簿中的所有工作表并删除所有图表(散布在周围),选择包含数据的最后一行,向后移动并删除空行。我尝试了从StackOverflow甚至从MS网站组装的东西,但似乎没有任何效果。

Sub DeleteChartsRowsAllSheets()
Dim Ws As Worksheet, chtObj As ChartObject, i As Long

    For Each Ws In ThisWorkbook.Worksheets

            For Each chtObj In Ws.ChartObjects
            chtObj.Delete

            Next
        'We turn off calculation and screenupdating to speed up the macro.
        With Application

        'Turn off screen updating and calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False

            For i = Selection.Rows.Count To 1 Step -1

                If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
                    Selection.Rows(i).EntireRow.Delete
                End If

            Next i

        'Turn screen updating and calculation back on
        .Calculation = xlCalculationAutomatic
        .ScreenUpdating = True

        End With

    Next Ws

End Sub

任何提示? :)

1 个答案:

答案 0 :(得分:0)

在嵌套循环中将ActiveSheet.ChartObjects替换为ws.ChartObjects

您目前只删除ActiveSheet上的图表。