Filter by selection of Year

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Filter by selection of Year

Post by adam »

Hi Anyone,

How could I make the following code to filter months from the pivot table,when I select the year from the drop down box in the worksheet?

Code: Select all

Private Sub Worksheet_Change(ByVal Target As Range)
  Dim d1 As Date
  Dim d2 As Date
  Dim m As Long
  Dim rng As Range
  Dim n As Long
  Dim c As Long

  ' Act only if AK1 or AL1 has changed
  If Not Intersect(Range("AK1,AL1"), Target) Is Nothing Then
    Application.ScreenUpdating = False
    m = Range("B10").CurrentRegion.Columns.Count
    If Range("AL1") = "" Or Range("AK1") = "" Then
      For c = 2 To m + 1
        Cells(10, c).EntireColumn.Hidden = False
      Next c
    Else
      ' First day of the month
     d1 = DateSerial(Worksheets("Stafflist").Range("YearList").Cells(Range("AL1")), Range("AK1"), 1)
      ' Last day of the month
    d2 = DateSerial(Worksheets("Stafflist").Range("YearList").Cells(Range("AL1")), Range("AK1") + 1, 0)
      ' Last data row
      ' Date column
      Set rng = Range(Cells(10, 2), Cells(10, m + 1))
      ' Count number of filtered records
      n = Application.CountIfs(rng, ">=" & CLng(d1), rng, "<=" & CLng(d2))
      ' Test if no records
      If n = 0 Then
        ' Inform user
        MsgBox "There are no records for this month/year combination.", vbInformation
      Else
        ' Hides rows for dates between first and last
        For c = 2 To m + 1
          Cells(10, c).EntireColumn.Hidden = Cells(10, c) < CLng(d1) Or Cells(10, c) > CLng(d2)
        Next c
      End If
    End If
    Application.ScreenUpdating = True
  End If
End Sub
I’ve Selected Months from the Group Selection in the Options tab in the PivotTable Tools group of the ribbon. And set the column headers with month names.

otherwise do I have to remove the line
d2 = DateSerial(Worksheets("Stafflist").Range("YearList").Cells(Range("AL1")), Range("AK1") + 1, 0)
to make the code do what I have asked above?

Any suggestion or help on this would be kindly appreciated.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

Sorry, I don't understand your question. What exactly do you mean by "... to filter months from the pivot table,when I select the year from the drop down box?"
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

To understand my question better I've attached the workbook.

I'm trying to filter the columns with when the year is selected form the drop down box.
You do not have the required permissions to view the files attached to this post.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

You have removed the months combo box, so you don't use the cell AK1 any more. You should change the code accordingly. For example, you don't have to check whether cell AK1 has changed.
You can't simply delete Range("AK1") from the lines that calculate d1 and d2. You'll have to think about what dates you want to use now.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

Sorry Hans I've removed the wrong code from the module. What I'm trying to get is when a year say for example 2009 is selected from the drop down combo box, all the datas in the months of 2009 to be visible and months from rest of the years to be hidden.

I've attached the workbook for your reference.
You do not have the required permissions to view the files attached to this post.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

My reply remains the same...
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

Hans, are you saying that I have to modify the lines
d1 = DateSerial(Worksheets("Stafflist").Range("YearList").Cells(Range("AL1")), Range("AK1"), 1)
' Last day of the month
d2 = DateSerial(Worksheets("Stafflist").Range("YearList").Cells(Range("AL1")), Range("AK1") + 1, 0)

and the associated; to remove the range AK1 from the code?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

You'll have to change those two lines, but you can't simply delete Range("AK1") from them.

The code filters dates between d1 and d2. They are currently set to the first and last day of the month represented by AK1 and AL1.
Now that you want to filter an entire year, what dates should you use for d1 and d2?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

I want to omit the AK1 range and use the year range instead. so that the table filters the months for the particular year selected from the combo box.

to filter an entire year couldn't it be made to select the first month of the year and the last month of the year as d1 and d2?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

adam wrote:to filter an entire year couldn't it be made to select the first month of the year and the last month of the year as d1 and d2?
Yes, but you have to be a bit more precise - which dates exactly should you use as d1 and d2?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

To be precise, if the current code uses the first day of the month as d1 and last day of the month as d2 cant d1 be made to select first month of the year and d2 as the last month of the year so that all the months of a particular year gets filtered when a year from the drop down combo box is selected?

I hope I have made my question clear.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

I'll leave you to work that out for yourself.

You'll have a problem, though, if you do. Since you grouped the data by month, row 10 doesn't contain dates any more, so it's no longer possible to filter correctly.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

Ok Hans, depending upon your suggestion I have added Years from the option button to the pivot table. Now the row 10 has dates. And row 11 contains month names. With this setting how should I do what I have asked in the previous post?
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

You haven't answered my question of a while back yet - which dates exactly should you use as d1 and d2?
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

Im sorry Hans. I might be unable to understand what you are referring by mentioning as d1 and d2. I would be happy if you could say so with an example or be more specific with the question.
Best Regards,
Adam

User avatar
HansV
Administrator
Posts: 78487
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Filter by selection of Year

Post by HansV »

I'm sorry Adam, this isn't working out. I suggest that you skip this one.
Best wishes,
Hans

User avatar
adam
SilverLounger
Posts: 2347
Joined: 23 Feb 2010, 12:07

Re: Filter by selection of Year

Post by adam »

No Hans. All I'm asking is to filter the pivot table with months when a year is selected. say for example if I select 2009 I want all the months for that year to be filtered. And if 2010 is selected I want all the (month) columns of data for that year to be filtered.

My question is simple, but your question with d1 & d2 is confusing me? for this reason I've asked for an example or a specification.
Best Regards,
Adam

User avatar
Leif
Administrator
Posts: 7209
Joined: 15 Jan 2010, 22:52
Location: Middle of England

Re: Filter by selection of Year

Post by Leif »

HansV wrote:I'm sorry Adam, this isn't working out. I suggest that you skip this one.
adam wrote:No Hans. All I'm asking....
No Adam, I'm sorry but you simply cannot carry on making demands like this.

It has been suggested on many occasions that you take the time to learn what you are trying to do, rather than blindly copying and pasting other people's code and then expecting them to fix it when it doesn't do what you want. Hans has politely declined to help further - you do not have the right to insist on others doing your work for you.
Leif