Error When Trying to Filter Pivot Table With Between Dates Using vb.net

121 Views Asked by At

I'm automating a number of reports using a vb.net winforms application. I have one report where I need to update the filter on a Date Field to between 2 dates (Monday & Sunday or the week in question).

I'm getting the dates by having the user select an End Date from a DateTimePicker dateStatusEnd

When the code gets to adding the filter I get the below error message:

enter image description here

The dates are valid because I mistakenly thought I had added them in the wrong order but this gave an error saying the end date had to be less then the start date so it's definitely reading them as errors.

Any ideas how I can resolve this? Code snippet below:

xlSht = aWorkBook.Worksheets("RBA Expiry Dates")

            Dim xlPivot As PivotTable = xlSht.PivotTables("PivotTable1")
            Dim xlPivotField As PivotField = xlPivot.PivotFields("RBA Expiry Date")

            xlPivotField.ClearAllFilters()

            If dateStatusEnd.Value.Day = 31 AndAlso dateStatusEnd.Value.Month = 12 And dateStatusEnd.Value.Date.ToString("dddd") <> "Sunday" Then
                Dim aDate As Date = dateStatusEnd.Value.Date

                Do Until aDate.ToString("dddd") = "Monday"
                    aDate = aDate.AddDays(-1)
                Loop

                xlPivotField.PivotFilters.Add2(Type:=XlPivotFilterType.xlDateBetween, Value1:=aDate.ToShortDateString, Value2:=dateStatusEnd.Value.Date.ToShortDateString)
            Else
                xlPivotField.PivotFilters.Add2(Type:=XlPivotFilterType.xlDateBetween, Value1:=dateStatusEnd.Value.Date.AddDays(-6).Date.ToShortDateString, Value2:=dateStatusEnd.Value.Date.ToShortDateString)
            End If
1

There are 1 best solutions below

0
On

In case anyone else comes across this in the future, it turns out that somewhere between opening the file & applying the filter the Pivot has stop recognising the date column as a date column.

Now I just need to figure out why that's happening