Hide Columns (F:MC) with Zero value

rtehlan

New Member
Joined
Mar 23, 2017
Messages
19
Hi,

Can anyone help me with this ? I am currently using a macro that would hide all the column which sums to 0 including (A:E) which are text but i only want it to work from Column (F:MC). Please note that i have row headers on row 9 throughout these columns. Any help would be greatly appreciated :)

Sub HideZeroColumn()


Dim anyWS As Worksheet
Dim anyTotalRow As Range
Dim anyCell As Range
Set anyWS = ActiveSheet
Set anyTotalRow = anyWS.Range(Cells(ActiveCell.Row, 2).Address, _
Cells(ActiveCell.Row, _
anyWS.Cells(ActiveCell.Row, Columns.Count). _
End(xlToLeft).Column))


Application.ScreenUpdating = False


anyTotalRow.Columns.EntireColumn.Hidden = False
For Each anyCell In anyTotalRow
If IsEmpty(anyCell) Or anyCell = 0 Then
anyCell.EntireColumn.Hidden = True
End If
Next
Set anyTotalRow = Nothing
Set anyWS = Nothing
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I am not entirely clear which columns you want to apply this to. I wrote it for columns F:MC, but that can be changed easily.

Maybe something like this:
Code:
Sub HideZeroColumn()

    Dim myCol As Long
    Dim colAdd As String
    
    Application.ScreenUpdating = False

'   Loop through all columns from F to MC
    For myCol = 6 To 341
        colAdd = Replace(Columns(myCol).Address, "$", "")
'       Hide column if sum is zero
        If WorksheetFunction.Sum(Range(colAdd)) = 0 Then
            Columns(myCol).Hidden = True
        End If
    Next myCol
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,194
Members
452,616
Latest member
intern444

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