Horizontal Filters

tinferns

Board Regular
Joined
Aug 18, 2009
Messages
150
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hello Team,
Firstly thanks a million for all your help in the past.

I need your help on the below:

I prefer formula(s), but I guess this will work best with a VBA. In the below table, I want to apply a horizontal filter based on Week Number. So If I have selected Week 41, I want to see all 6 departments' dates between 05Oct2020 - 09Oct2020. Rest has to be hidden.

If required can place another table in the file to help VBA choose START DATE & END DATE depending on Year and Week Number. The format I am working is shown below:

VBA FOR HORIZONTAL FILTER.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBI
1Week NoDepartment 1Department 2Department 3Department 4Department 5Department 6Department 1Department 2Department 3Department 4Department 5Department 6
24105-Oct06-Oct07-Oct08-Oct09-Oct05-Oct06-Oct07-Oct08-Oct09-Oct05-Oct06-Oct07-Oct08-Oct09-Oct05-Oct06-Oct07-Oct08-Oct09-Oct05-Oct06-Oct07-Oct08-Oct09-Oct05-Oct06-Oct07-Oct08-Oct09-Oct12-Oct13-Oct14-Oct15-Oct16-Oct12-Oct13-Oct14-Oct15-Oct16-Oct12-Oct13-Oct14-Oct15-Oct16-Oct12-Oct13-Oct14-Oct15-Oct16-Oct12-Oct13-Oct14-Oct15-Oct16-Oct12-Oct13-Oct14-Oct15-Oct16-Oct
3Number of Emplooyes
4Employees Present
5Employees Absent
6
7Total Productivity
8Expected Productivity
9Actual Producitivty
10Difference
11
12Quality Scores
13Expected QA Scores
14Difference
15
16Client Scores
17Expected Client Scores
18Difference
19
Sheet1


Start Date and End Date based on Year and Week Number (I can keep this table hidden somewhere in the sheet)
VBA FOR HORIZONTAL FILTER.xlsx
ABCD
21YearWeek NoStart DateEnd Date
222020415-Oct-209-Oct-20
2320204212-Oct-2016-Oct-20
2420204319-Oct-2023-Oct-20
2520204426-Oct-2030-Oct-20
262020452-Nov-206-Nov-20
272020469-Nov-2013-Nov-20
2820204716-Nov-2020-Nov-20
2920204823-Nov-2027-Nov-20
3020204930-Nov-204-Dec-20
312020507-Dec-2011-Dec-20
3220205114-Dec-2018-Dec-20
3320205221-Dec-2025-Dec-20
3420205328-Dec-201-Jan-21
35202114-Jan-218-Jan-21
362021211-Jan-2115-Jan-21
372021318-Jan-2122-Jan-21
382021425-Jan-2129-Jan-21
39202151-Feb-215-Feb-21
40202168-Feb-2112-Feb-21
412021715-Feb-2119-Feb-21
422021822-Feb-2126-Feb-21
43202191-Mar-215-Mar-21
442021108-Mar-2112-Mar-21
4520211115-Mar-2119-Mar-21
4620211222-Mar-2126-Mar-21
4720211329-Mar-212-Apr-21
482021145-Apr-219-Apr-21
4920211512-Apr-2116-Apr-21
5020211619-Apr-2123-Apr-21
5120211726-Apr-2130-Apr-21
522021183-May-217-May-21
5320211910-May-2114-May-21
5420212017-May-2121-May-21
5520212124-May-2128-May-21
5620212231-May-214-Jun-21
572021237-Jun-2111-Jun-21
5820212414-Jun-2118-Jun-21
5920212521-Jun-2125-Jun-21
6020212628-Jun-212-Jul-21
612021275-Jul-219-Jul-21
6220212812-Jul-2116-Jul-21
6320212919-Jul-2123-Jul-21
6420213026-Jul-2130-Jul-21
652021312-Aug-216-Aug-21
662021329-Aug-2113-Aug-21
6720213316-Aug-2120-Aug-21
6820213423-Aug-2127-Aug-21
6920213530-Aug-213-Sep-21
702021366-Sep-2110-Sep-21
7120213713-Sep-2117-Sep-21
7220213820-Sep-2124-Sep-21
7320213927-Sep-211-Oct-21
742021404-Oct-218-Oct-21
7520214111-Oct-2115-Oct-21
7620214218-Oct-2122-Oct-21
7720214325-Oct-2129-Oct-21
782021441-Nov-215-Nov-21
792021458-Nov-2112-Nov-21
8020214615-Nov-2119-Nov-21
8120214722-Nov-2126-Nov-21
8220214829-Nov-213-Dec-21
832021496-Dec-2110-Dec-21
8420215013-Dec-2117-Dec-21
8520215120-Dec-2124-Dec-21
8620215227-Dec-2131-Dec-21
87202213-Jan-227-Jan-22
882022210-Jan-2214-Jan-22
Sheet1
Cell Formulas
RangeFormula
C22:C88C22=DATE(A22,1,-2)-WEEKDAY(DATE(A22,1,3))+B22*7
D22:D88D22=DATE(A22, 1, -2) - WEEKDAY(DATE(A22, 1, 3)) + B22 * 7 + 4
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I don't see where filtering fits into the question.

In B2, then drag right.
Excel Formula:
=VLOOKUP($A$2,Table1[[Week No]:[Start Date]],2,0)+MOD(COLUMNS($B$2:B$2)-1,5)
I've assumed that your table is named 'Table1', if it is different then you will need to change it.

Alternatively, you could just use the first part of the formula in B2.
Excel Formula:
=VLOOKUP($A$2,Table1[[Week No]:[Start Date]],2,0)
Then enter
Excel Formula:
=B2+1
into C2 and drag that right as far as F2. Finally, enter
Excel Formula:
=B2
into G2 and drag that right as far as needed.

The first method is simpler to set up, the second uses simpler formulas and is more efficient, although the difference would be negligible.
 
Upvote 0
Hi Jason.. Thanks for your super quick reply. The above formula gives me the dates based on week number.. thanks for that.

I want the formula or vba to hide & unhide columns.

For Eg. I have updated or I am looking for data only for Week Number 41 in the above example. This has to

UNHIDE: Column B - Column AE = Week 41
HIDE
: Column AF to Column BI = Week 42

I am sorry I did not explain the scenario clearly earlier. Hope the above explanation is clear.

Thanks..
 
Upvote 0
My fault for not scrolling right far enough, if I had then I would have seen it :oops: Because I wasn't seeing the relevance of the filter, I incorrectly thought that you were trying to fill the dates.

You can't filter in place with formulas, at best you can reproduce the relevant data in a different sheet. (Easier with office 365 but still possible with older versions).

The other option is to hide / unhide the columns on the existing sheet based on the week number using vba.

Which would you prefer?
 
Upvote 0
Thanks Jason..

I am using O 365 and 2019.

I will stick to VBA for now.

Please send me the script.

Thanks in advance..
 
Upvote 0
Try this in the worksheet module (select the sheet, then right click the tab and 'view code'. This will run automatically when you change the week number in A2.
The additional table for the week numbers is not required.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then
    Dim col As Long, c As Range
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .Calculation = xlManual
        
            col = Cells(2, Columns.Count).End(xlToLeft).Column
    
    For Each c In Range("B2").Resize(1, col - 1)
        c.EntireColumn.Hidden = (Evaluate("ISOWEEKNUM(" & CLng(c) & ")") <> Range("A2").Value)
    Next
    
        .EnableEvents = True
        .ScreenUpdating = True
        .Calculation = xlAutomatic
    End With
End If
End Sub
 
Upvote 0
Hi Jason,
Thanks for sending the code so quickly. I have pasted the code exactly how you have instructed, however when I change the week in A2 no action is visible.

I changed the week number to 42, thus Column B to AE should have hidden. Nothing happened. I guess I am not doing something right..

Screenshots attached.

Please help.

Thanks.
 

Attachments

  • Horizontal Filter.jpg
    Horizontal Filter.jpg
    160.8 KB · Views: 3
  • Horizontal Filter1.jpg
    Horizontal Filter1.jpg
    219 KB · Views: 2
Upvote 0
You have put the code into a general module, not the worksheet module.
Try this in the worksheet module (select the sheet, then right click the tab and 'view code'.
If you had done this then it would have taken you directly to the correct module, which is the one named Sheet1 (Sheet1) in your first screen capture.
 
Upvote 0
Hi Jason .. thanks for your reply.. I guess I am doing it right now. when I change to 42 all the columns are getting hidden and when I change back to 41, getting the below Debug error.

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then
Dim col As Long, c As Range

With Application
.EnableEvents = False
.ScreenUpdating = False
.Calculation = xlManual

col = Cells(2, Columns.Count).End(xlToLeft).Column

For Each c In Range("B2").Resize(1, col - 1)
c.EntireColumn.Hidden = (Evaluate("ISOWEEKNUM(" & CLng(c) & ")") <> Range("A2").Value)
Next

.EnableEvents = True
.ScreenUpdating = True
.Calculation = xlAutomatic
End With
End If
End Sub
 
Upvote 0
Try this way instead, it appears that the first method doesn't pick up the hidden columns to the right of the last visible column.
You will probably have to close excel and reopen it for this to work as the code would have ended with events being disabled.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A2")) Is Nothing Then
    Dim col As Long, c As Range
    
    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .Calculation = xlManual
        
            cols = UsedRange.Columns.Count
    
    For Each c In Range("B2").Resize(1, cols - 1)
        c.EntireColumn.Hidden = (Evaluate("ISOWEEKNUM(" & CLng(c) & ")") <> Range("A2").Value)
    Next
    
        .EnableEvents = True
        .ScreenUpdating = True
        .Calculation = xlAutomatic
    End With
End If
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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