Hide columns with zero or blank after filter

Accountant88

New Member
Joined
Oct 16, 2021
Messages
6
Office Version
  1. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Hi

I have a spreadsheet with so many columns and rows. I am looking for a macro to filter the product in Column A, cell 2; then hide any columns in the range from BU to CW with zero value or no value. Can anyone help please?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
I am thinking if I filter the product I want first, then press the macro button to hide any columns that with zero value or no value after the filter. The following codes work but I don't know how to make it perform looping until the last column is checked. Please help.

Sub HideColumnsWhenAllRowsAreZero()

Dim Lastrow As Long

Lastrow = Range("BU" & Rows.Count).End(xlUp).Row 'finds the last row for column BU

If WorksheetFunction.Sum(Range("BU1:BU" & Lastrow)) = 0 Then 'sum the values of the column if it equals 0 then move on to hide column

Range("BU1:BU" & Lastrow).EntireColumn.Hidden = True 'Hides column

End If

End sub
 
Upvote 0
Hi

maybe try this below

VBA Code:
Sub HideColumnsWhenAllRowsAreZero()
For A = 73 To 101
    If WorksheetFunction.Sum(Range(Cells(1, A), Cells(Cells(Rows.Count, A).End(xlUp).Row, A))) = 0 Then Columns(A).EntireColumn.Hidden = True
Next A
End Sub


Dave
 
Last edited:
Upvote 0
Hi

maybe try this below

VBA Code:
Sub HideColumnsWhenAllRowsAreZero()
For A = 73 To 101
    If WorksheetFunction.Sum(Range(Cells(1, A), Cells(Cells(Rows.Count, A).End(xlUp).Row, A))) = 0 Then Columns(A).EntireColumn.Hidden = True
Next A
End Sub


Dave
Brilliant, thanks so much Dave.
 
Upvote 0
Hi

maybe try this below

VBA Code:
Sub HideColumnsWhenAllRowsAreZero()
For A = 73 To 101
    If WorksheetFunction.Sum(Range(Cells(1, A), Cells(Cells(Rows.Count, A).End(xlUp).Row, A))) = 0 Then Columns(A).EntireColumn.Hidden = True
Next A
End Sub


Dave
Hi again Dave,

It seems to work if I select the first product from the filter, after I unhide all the columns hidden from the first filter, it doesn't work. I want to upload a mini-sheet but don't know how!! Can only upload the two images. Hope you don't mind helping me again.

I have amended the code as A=2 - 46
 

Attachments

  • works.JPG
    works.JPG
    45.3 KB · Views: 12
  • Doesn't work.JPG
    Doesn't work.JPG
    44.6 KB · Views: 13
Upvote 0
Hi

It would be my guess actually that my code did not work from the start, unless by pure luck.

I believe now i understand what you are trying to achieve.

The code i posted will not work after a filter as it is still checking the whole column, not the filtered column.

I will look at it a bit later, but this reply by me now will bump the thread for you in case any one else wants a go.

Dave
 
Upvote 0
Hi

It would be my guess actually that my code did not work from the start, unless by pure luck.

I believe now i understand what you are trying to achieve.

The code i posted will not work after a filter as it is still checking the whole column, not the filtered column.

I will look at it a bit later, but this reply by me now will bump the thread for you in case any one else wants a go.

Dave
Thanks in advance Dave.
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,621
Members
449,109
Latest member
Sebas8956

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