VBA to delete entire column based on calculation/value of zero

IthoughtIknewExcel

New Member
Joined
Jan 5, 2012
Messages
24
Hello,

I'm looking for some macro help. The below small section of a worksheet has a few 100 columns that vary in amount each week. I have built a macro to insert the top row (row 1) and sum the columns that contain sizes. What I'm hoping to achieve is to delete every column where the total units per size = 0. The 0's are sporadic in row 1. Whatever direction the macro takes, it needs to loop/repeat the delete 0 column multiple times. I've tried a few things but nothing has worked thus far. Any help would be appreciated!

Thank you!


0346382382700346387383307346382482700130019
Intro DateProduct TypeCategoryModelColorCostUnitsXSSMLXL2XL3XL1-2-4-55-66-77-8-9-1010-11-12-1313-14-19
02/01/2018JACKETRUNNINGQ12331RED3512313278313278313278
12/15/2018JACKETFOOTBALLF12344BLUE35412752752752
02/01/2018SHORTFOOTBALL123345BLACK352389238923892
02/01/2018JACKETFOOTBALL121244RED354626262
12/15/2018SHORTRUNNINGCT0305BLACK3518424242
02/01/2018JACKETRUNNINGCT6880WHITE251234515151
12/15/2018JACKETBASKETBALLGG3224WHITE25523531531531
02/01/2018PANTBASKETBALLQ12331BLUE25123144144144
02/01/2018SHORTRUNNINGF12344BLUE2424610556105561055
02/01/2018PANTBASKETBALL123345GREEN24234411241124112
11/15/2017PANTRUNNING121244GREEN4512391043910439104
11/15/2017PANTBASKETBALLCT0305RED45234511152511152511152
11/15/2017SHORTBASKETBALLCT6880BLACK4512333133313331
05/01/2019SHORTSOCCERDY3119BLACK5054322322322
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Try this:
VBA Code:
Sub MyDeleteCols()

    Dim fc As Long, lc As Long, c As Long
    
    Application.ScreenUpdating = False
    
'   Find first and last column in row 1 with a value
    fc = Cells(1, 1).End(xlToRight).Column
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    
'   Loop through all columns backwards
    For c = lc To fc Step -1
'       Delete column if value in cell is zero
        If Cells(1, c) = 0 Then Columns(c).Delete Shift:=xlToLeft
    Next c
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
If you are happy changing the formula to something like
=IFERROR(1/(1/SUM(H3:H16)),FALSE)
copied across giving
+Fluff.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1FALSE3463823827FALSEFALSE3463823827FALSEFALSE34638238
2Intro DateProduct TypeCategoryModelColorCostUnitsXSSMLXL2XL3XL1-2-4-55-66-77-8-9-10
302/01/2018JACKETRUNNINGQ12331RED3512313278313278313278
412/15/2018JACKETFOOTBALLF12344BLUE35412752752752
502/01/2018SHORTFOOTBALL123345BLACK352389238923892
602/01/2018JACKETFOOTBALL121244RED354626262
712/15/2018SHORTRUNNINGCT0305BLACK351842424
802/01/2018JACKETRUNNINGCT6880WHITE25123451515
912/15/2018JACKETBASKETBALLGG3224WHITE2552353153153
1002/01/2018PANTBASKETBALLQ12331BLUE2512314414414
1102/01/2018SHORTRUNNINGF12344BLUE242461055610556105
1202/01/2018PANTBASKETBALL123345GREEN242344112411241
1311/15/2017PANTRUNNING121244GREEN4512391043910439104
1411/15/2017PANTBASKETBALLCT0305RED4523451115251115251115
1511/15/2017SHORTBASKETBALLCT6880BLACK4512333133313331
1605/01/2019SHORTSOCCERDY3119BLACK5054322322322
List
Cell Formulas
RangeFormula
H1:Z1H1=IFERROR(1/(1/SUM(H3:H16)),FALSE)


You could use
VBA Code:
Sub DelCols()
   Range("1:1").SpecialCells(xlFormulas, xlLogical).EntireColumn.Delete
End Sub
 
Upvote 0
If you are happy changing the formula to something like
=IFERROR(1/(1/SUM(H3:H16)),FALSE)
copied across giving
+Fluff.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1FALSE3463823827FALSEFALSE3463823827FALSEFALSE34638238
2Intro DateProduct TypeCategoryModelColorCostUnitsXSSMLXL2XL3XL1-2-4-55-66-77-8-9-10
302/01/2018JACKETRUNNINGQ12331RED3512313278313278313278
412/15/2018JACKETFOOTBALLF12344BLUE35412752752752
502/01/2018SHORTFOOTBALL123345BLACK352389238923892
602/01/2018JACKETFOOTBALL121244RED354626262
712/15/2018SHORTRUNNINGCT0305BLACK351842424
802/01/2018JACKETRUNNINGCT6880WHITE25123451515
912/15/2018JACKETBASKETBALLGG3224WHITE2552353153153
1002/01/2018PANTBASKETBALLQ12331BLUE2512314414414
1102/01/2018SHORTRUNNINGF12344BLUE242461055610556105
1202/01/2018PANTBASKETBALL123345GREEN242344112411241
1311/15/2017PANTRUNNING121244GREEN4512391043910439104
1411/15/2017PANTBASKETBALLCT0305RED4523451115251115251115
1511/15/2017SHORTBASKETBALLCT6880BLACK4512333133313331
1605/01/2019SHORTSOCCERDY3119BLACK5054322322322
List
Cell Formulas
RangeFormula
H1:Z1H1=IFERROR(1/(1/SUM(H3:H16)),FALSE)


You could use
VBA Code:
Sub DelCols()
   Range("1:1").SpecialCells(xlFormulas, xlLogical).EntireColumn.Delete
End Sub
Thank you both for the help! Fluff, your approach worked perfectly!!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,902
Messages
6,122,161
Members
449,069
Latest member
msilva74

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