繁体   English   中英

VBA 循环工作表以删除空白列

[英]VBA Looping through worksheets to delete blank columns

我创建了这个宏来删除活动工作簿中的每个空列。 但是,它不适用于每张纸,仅适用于活动纸。

请让我知道您的想法可以做些什么来使宏正常工作并循环遍历所有工作表,而不仅仅是活动的工作表。

Sub DeleteBlankColumns()

Dim LastColumn As Long
Dim r As Long
Dim Counter As Long
Dim Current As Worksheet

    Application.ScreenUpdating = False
    For Each Current In Worksheets
    LastColumn = Current.UsedRange.Columns.Count + Current.UsedRange.Columns(1).Column - 1
        For r = LastColumn To 1 Step -1
            If Application.WorksheetFunction.CountA(Columns(r)) = 0 Then
                Current.Columns(r).Delete
                Counter = Counter + 1
            End If
        Next r
    Next
    Application.ScreenUpdating = True
    MsgBox "Deleted " & Counter & " empty columns."

End Sub

尝试这个:

Option Explicit

Sub DeleteBlankColumns()

Dim LastColumn As Long
Dim r As Long
Dim Counter As Long
Dim Current As Worksheet

    Application.ScreenUpdating = False
    For Each Current In Worksheets
        With Current
            LastColumn = .UsedRange.Columns.Count + .UsedRange.Columns(1).Column - 1
            For r = LastColumn To 1 Step -1
                If Application.WorksheetFunction.CountA(.Columns(r)) = 0 Then
                    .Columns(r).Delete
                    Counter = Counter + 1
                End If
            Next r
        End With
    Next
    Application.ScreenUpdating = True
    MsgBox "Deleted " & Counter & " empty columns."

End Sub

暂无
暂无

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

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