Autofiltering columns

cpis0002

Board Regular
Joined
Jul 28, 2007
Messages
96
I have a spreadsheet template with a range of years from 1997-2020. I want an autofiltering tool so when the year is greater than the current year (current year is named: Year_End) it will be hidden...

That is: like when you have a long list of items with quantities, you can autofilter the rows to only show items with quanitities greater than 0...

I want the same thing for columns, any idea?
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

cpis0002

Board Regular
Joined
Jul 28, 2007
Messages
96
The sheet is quite big, about 600 rows and 50 columns :S

Theres nothing much to understand about it... to make it simler:

I have Row 4 with amounts 0 or 1 throught columns C to AA.
These are made up of the formula IF(year>Year_End,0,1)

So then I need something to remove all the columns with the amount 0 in row 4... That is: the year is after the current year (Year_End).
 
Upvote 0
Joined
Jul 30, 2006
Messages
3,656
cpis0002,

I have a solution using the Worksheet_Change Event, and the range name 'Year_End'.
Hide Column if row 4 value equals to zero - with Worksheet_Change Event.xls
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1<---Year_End
2
31997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
40000000000000000000000000
5
Sheet1
 
Upvote 0
Joined
Jul 30, 2006
Messages
3,656
Next part:

If you make a change to cell A1, range name 'Year_End', say to 2011, and press the ENTER key:
Hide Column if row 4 value equals to zero - with Worksheet_Change Event.xls
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
12011<---Year_End
2
3199719981999200020012002200320042005200620072008200920102011
4111111111111111          
5
Sheet1
 
Upvote 0
Joined
Jul 30, 2006
Messages
3,656
One more screenshot:

If you enter '3000' into the range name 'Year_End' and press the 'ENTER' key:
Hide Column if row 4 value equals to zero - with Worksheet_Change Event.xls
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
13000<---Year_End
2
31997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
41111111111111111111111111
5
Sheet1
 
Upvote 0
Joined
Jul 30, 2006
Messages
3,656
And the code:

Press and hold down the 'ALT' key, and press the 'F11' key.

Insert a Module in your VBAProject, Microsoft Excel Objects

Copy the below code, and paste it into VBAProject, Microsoft Excel Objects, Sheet1(Sheet1): or the sheetname where the data is

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("Year_End")) Is Nothing Then
        Exit Sub
    End If

    If Target.Value < 1997 Then
        'Do nothing
        Exit Sub
    End If

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Dim lngColumnNumber As Long

    For lngColumnNumber = 27 To 3 Step -1
        If Cells(4, lngColumnNumber).Value < 1 Then
            Cells(4, lngColumnNumber).EntireColumn.Hidden = True
        Else
            Cells(4, lngColumnNumber).EntireColumn.Hidden = False
        End If
    Next lngColumnNumber

    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub

Make some changes to the range name 'Year_End'.

Have a great day,
Stan
 
Upvote 0

Forum statistics

Threads
1,191,025
Messages
5,984,195
Members
439,877
Latest member
kellylet

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
Top