VBA - Pivot table - day and month are sometime reversed

clefev01

New Member
Joined
Mar 24, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello

It is my first post as usually I can find answer in existing post. But since two days I get crazy with my problem.

With VBA, I want to select some specific dates in a pivot table field. Hereunder my example

1648098925478.png


All the dates are stored in a table with a date format dd-mmm-yy (eg: 14-Mar-22)

To explain my problem I wrote this piece of code :
VBA Code:
Set pt = Sheets("tcd non reçus").PivotTables("PT_non_recu")
    ' change field as needed
    Set pf = pt.PivotFields("expected date")

    For Each Pi In pf.PivotItems
        If Pi.Value <> "NULL" And Pi.Value <> "#N/A" And Not IsError(Pi.Value) Then
            MsgBox Pi.Value & " - Day=" & Day(Pi)
        End If
    Next Pi

The problem ? For the first dates until march I get the right day - Day(Pi) - (23, 25, 29, ...) but starting the value '01/04/2022' the day is wrong as it says 4 instead of 1, 5, 8..

1648099257137.png
=> OK
1648099294563.png
=> KO

If someone has an idea, he/she is welcome !

Thank you very much for your help
 
integer is until +32,000, my error, a long-variable is better, so use cLng instead of cInt.
But as the other solution works, it's not necessary anymore.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
integer is until +32,000, my error, a long-variable is better, so use cLng instead of cInt.
But as the other solution works, it's not necessary anymore.

Thanks for the explanation. Have all a Good day.
 
Upvote 0
@BSALV, I also played around with CLng but couldn't get it to work, I get an error 13 Type Mismatch.
I can stop the error happening by doing a CDate first but then I again get the error of 1 Apr to 12 Apr reversing month and day.
It seems that some VBA functions use US format while some of the conversion functions use your region settings.

I opted to use Application.Text to avoid the early VBA conversion to US format. Using the equivalent VBA function "Format" seems to be convert the data to US format if it can before applying the format function and goes off the rails when the day is between 1 to 12.
 
Upvote 0
i agree with the observations of @Alex Blakenburg in #13.
VBA Code:
Sub Test()

     Set pt = Sheets("tcd non recus").PivotTables("PT_non_recu")
     Set pf = pt.PivotFields("expected date")

     For Each Pi In pf.PivotItems
          If Pi.Value <> "NULL" And Pi.Value <> "#N/A" And Not IsError(Pi.Value) Then
               mydate = Convert_US_Date(Pi.Value)
               MsgBox Pi.Value & vbLf & Format(mydate, "long date")
          End If
     Next Pi

End Sub

Function Convert_US_Date(US_Date As String) As Date
     sp = Split(Replace(Replace(US_Date, ".", "/"), "-", "/"), "/")
     Convert_US_Date = DateSerial(sp(2), sp(0), sp(1))
End Function
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,549
Members
449,089
Latest member
davidcom

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