Vba code required to delete columns

Menace1977

New Member
Joined
Jul 23, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I'm looking for a vba code that will hopefully delete multiple columns preferably (or ranges) based on a cell value (0 in this case).
What you see in the image is a front and back page.In my worksheet there are 40 pages but only need to print the ones that don't have 0.
In the scenario in the image it would delete columns A -AB as the Route =0 .
 

Attachments

  • Screenshot_20230723_084638_com.microsoft.office.excel_edit_3413181309374~2.jpg
    Screenshot_20230723_084638_com.microsoft.office.excel_edit_3413181309374~2.jpg
    158.5 KB · Views: 10

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Assuming your route value is in range H4, If not please change this line into desire range
Rich (BB code):
If .[h4] = 0 Then

VBA Code:
Sub test()

For i = 1 To Worksheets.Count
    With Sheets(i)
        If .[h4] = 0 Then .[a:ab].Delete 'del range
    End With
Next i

End Sub
 
Upvote 0
Welcome to the MrExcel board!

In my worksheet
That sounds singular to me and in the image I think that I can see another section starting in column AD so I'm thinking the 40 'pages' might be across the one worksheet.
If that is the case, give this a try with a copy of your workbook.

BTW, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

VBA Code:
Sub Del_Cols()
  Dim c As Long, lastcol As Long
 
  lastcol = Cells(4, Columns.Count).End(xlToLeft).Column
 
  Application.ScreenUpdating = False
  For c = lastcol - 14 To 8 Step -28
    If Cells(4, c).Value = 0 Then Columns(c - 6).Resize(, 28).Delete
  Next c
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Assuming your route value is in range H4, If not please change this line into desire range
Rich (BB code):
If .[h4] = 0 Then

VBA Code:
Sub test()

For i = 1 To Worksheets.Count
    With Sheets(i)
        If .[h4] = 0 Then .[a:ab].Delete 'del range
    End With
Next i

End Sub
Thanks for the code i will give this a try when i get a chance.
 
Upvote 0
Welcome to the MrExcel board!


That sounds singular to me and in the image I think that I can see another section starting in column AD so I'm thinking the 40 'pages' might be across the one worksheet.
If that is the case, give this a try with a copy of your workbook.

BTW, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.

VBA Code:
Sub Del_Cols()
  Dim c As Long, lastcol As Long
 
  lastcol = Cells(4, Columns.Count).End(xlToLeft).Column
 
  Application.ScreenUpdating = False
  For c = lastcol - 14 To 8 Step -28
    If Cells(4, c).Value = 0 Then Columns(c - 6).Resize(, 28).Delete
  Next c
  Application.ScreenUpdating = True
End Sub
Thanks, that is correct. The 40 pages are across the worksheet. I will give this a try when i chance and let you know the outcome.
 
Upvote 0

Forum statistics

Threads
1,215,273
Messages
6,123,987
Members
449,137
Latest member
abdahsankhan

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