John Caines
Well-known Member
- Joined
- Aug 28, 2006
- Messages
- 1,155
- Office Version
- 2019
- Platform
- Windows
Hello All.
I'm trying to find a vba solution to delete all empty (selected range) columns in a spreadsheet,,,but with a twist.
The spreadsheet columns have a header in Row A,,,so technically the column isn't completely empty.
So it's trying to write some vba code to delete all empty columns that have a header (in row A),,, and if nothing is in the column below A, delete the row.
I did find a vba code on the www. here;
Code was;
But the above code doesn't take into account the headings in row A.
Is there anyway to modify this code above so I can delete empty columns on a range I select (taking into account the selected range, you'd be ignoring the Row A
Hope the above makes sense and someone can help me out here.
Best regards
John C
I'm trying to find a vba solution to delete all empty (selected range) columns in a spreadsheet,,,but with a twist.
The spreadsheet columns have a header in Row A,,,so technically the column isn't completely empty.
So it's trying to write some vba code to delete all empty columns that have a header (in row A),,, and if nothing is in the column below A, delete the row.
I did find a vba code on the www. here;
How to remove blank columns in Excel
See how to delete blank columns in Excel with a macro, formula or a button-click.
www.ablebits.com
Code was;
Code:
Public Sub DeleteEmptyColumns()
Dim SourceRange As Range
Dim EntireColumn As Range
On Error Resume Next
Set SourceRange = Application.InputBox( _
"Select a range:", "Delete Empty Columns", _
Application.Selection.Address, Type:=8)
If Not (SourceRange Is Nothing) Then
Application.ScreenUpdating = False
For i = SourceRange.Columns.Count To 1 Step -1
Set EntireColumn = SourceRange.Cells(1, i).EntireColumn
If Application.WorksheetFunction.CountA(EntireColumn) = 0 Then
EntireColumn.Delete
End If
Next
Application.ScreenUpdating = True
End If
End Sub
But the above code doesn't take into account the headings in row A.
Is there anyway to modify this code above so I can delete empty columns on a range I select (taking into account the selected range, you'd be ignoring the Row A
Hope the above makes sense and someone can help me out here.
Best regards
John C