VBA/Macro Deleting multiple columns on Multiple Sheets

sprook

New Member
Joined
Aug 26, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I have a report I run that is poorly formatted and I have to reformat. Essentially I need to delete 14 columns on a given sheet, autofit the remaining columns, and then repeat the same process for the remaining sheets in the workbook. Any assistance would be greatly appreciated. Thank you.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi There and welcome to the forum

If you are familiar with VBA you can past the code below to a module and then just run the macro from either Alt+F8 or assign it to a button... Please test on sample piece before trying it out on your original data.

VBA Code:
Sub redoit()
    Application.ScreenUpdating = FALSE
    Dim WS          As Worksheet
    Dim i           As Long
    For i = 1 To Sheets.Count
        Sheets(i).Range("B:B,D:D,F:F,G:G").Delete        'Here you specify which columns to delete
        For Each WS In ActiveWorkbook.Worksheets
            WS.Columns.AutoFit
        Next WS
    Next i
    Application.ScreenUpdating = TRUE
End Sub
 
Upvote 0
Hi There and welcome to the forum

If you are familiar with VBA you can past the code below to a module and then just run the macro from either Alt+F8 or assign it to a button... Please test on sample piece before trying it out on your original data.

VBA Code:
Sub redoit()
    Application.ScreenUpdating = FALSE
    Dim WS          As Worksheet
    Dim i           As Long
    For i = 1 To Sheets.Count
        Sheets(i).Range("B:B,D:D,F:F,G:G").Delete        'Here you specify which columns to delete
        For Each WS In ActiveWorkbook.Worksheets
            WS.Columns.AutoFit
        Next WS
    Next i
    Application.ScreenUpdating = TRUE
End Sub
Thank you for the welcome and thank you for the code. This was very helpful and exactly what I needed!
 
Upvote 0
Not really necessary to loop through all the sheets twice:
VBA Code:
Sub redoit()
    Application.ScreenUpdating = False
    Dim WS As Worksheet, i As Long
    For i = 1 To Sheets.Count
        With Sheets(i)
            .Range("B:B,D:D,F:F,G:G").Delete        'Here you specify which columns to delete
            .Columns.AutoFit
        End With
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@Himeshwari
Please start your own new thread and explain in detail what you want to do. Use the XL2BB add-in (icon in the menu) to attach a screenshot of your data.
 
Upvote 0

Forum statistics

Threads
1,223,929
Messages
6,175,448
Members
452,642
Latest member
acarrigan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top