删除所有工作表宏中的空列

问题描述 投票:0回答:1

我创建了一个宏,用于从工作表中删除空列。我试图将其应用于基于另一个宏的所有工作表,但是我做不到。您如何将其应用于所有工作表?谢谢!

这是删除列的代码:

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

我如何合并它们?再次感谢!

vba excel-vba macros multiple-columns worksheet
1个回答
0
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.