I created a macro to remove empty columns from a worksheet.
How do you apply it to all worksheets?
Here's the code for removing columns:
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
And this is the one I've used to remove empty rows from all worksheets.
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
A merge of those codes would look like this:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.