删除所有工作表中的空列

Delete empty columns in all worksheets

我创建了一个宏来从工作表中删除空列。

如何将其应用于所有工作表?

下面是删除列的代码:

Sub DeleteColumns()
Dim Col As Integer

 With ActiveSheet.UsedRange
  For Col = .Column + .Columns.Count - 1 To 1 Step -1
   If IsEmpty(Cells(1048562, Col)) And IsEmpty(Cells(1, Col)) Then
    If Cells(1048562, Col).End(xlUp).Row = 1 Then Columns(Col).Delete
   End If
  Next Col
 End With

End Sub

这是我用来从所有工作表中删除空行的方法。

Sub RemoveEmptyRows()

    Dim row As Long
    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        For row = 10 To 1 Step -1
            If IsError(ws.Cells(row, 4)) Then
                ws.Rows(row).Delete
            ElseIf ws.Cells(row, 4).Value = "" Then
                ws.Rows(row).Delete
            End If
        Next row
    Next ws

End Sub

这些代码的合并看起来像这样:

Sub RemoveEmptyColumns()

    Dim row As Long
    Dim ws As Worksheet
    Dim Col As Integer

    For Each ws In ThisWorkbook.Worksheets
        For Col = ws.UsedRange.Column + ws.UsedRange.Columns.Count - 1 To 1 Step -1
            If IsEmpty(ws.Cells(1048562, Col)) And IsEmpty(ws.Cells(1, Col)) Then
            If ws.Cells(1048562, Col).End(xlUp).row = 1 Then ws.Columns(Col).Delete
            End If
        Next Col
    Next ws

End Sub