Tuesday, June 21, 2016

VBA code to Delete Blank Columns from Active Worksheet

You can use below VBA code to Delete Blank Columns from Active Worksheet:

Sub Delete_Blank_Columns()

'Step1:  Declare your variables.
    Dim MyRange As Range
    Dim iCounter As Long

'Step 2:  Define the target Range.
    Set MyRange = ActiveSheet.UsedRange
   
'Step 3:  Start reverse looping through the range.
    For iCounter = MyRange.Columns.Count To 1 Step -1
   
'Step 4: If entire column is empty then delete it.
       If Application.CountA(Columns(iCounter).EntireColumn) = 0 Then
       Columns(iCounter).Delete
       End If

'Step 5: Increment the counter down
    Next iCounter
   
End Sub

Delete Blank Rows from active worksheet

No comments:

Post a Comment