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
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
it sees 1 april as 4 january, 2 april as 4 february, etc, untill 12 april as 4 december (the american way of showing the data as mmm/dd) and then 13 april is again normal ...

??? solution 2 of How to Change Date Formatting for Grouped Pivot Table Fields - Excel Campus

Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
Columns("L:L").NumberFormat = "mm/dd/yy;@"
Debug.Print "ran macro to set date format in pivot table"
End Sub


But are you sure all your dates in the source are really dates and a string looking like a date.
Can you group your date to a month for example ?
 
Last edited:
Upvote 0
Most likely some of your dates are "text looking like dates" ( left aligned in cells without manual alignment) rather than real dates ( right-aligned)).
Formatting is only the way XL shows numbers to the outside world and does not affect the underlying values
Perhaps write a piece of code to mimic the sequence : Select date range - click Data - Text to columns - Finish, or do it manually, which should turn the "text" into real dates
If not some other techniques will be needed
There might be better techniques in VBA but I'm not savvy in these matters.
 
Upvote 0
Upvote 0
Try this:
VBA Code:
MsgBox pi.Value & " - Day=" & Day(Format(pi.Value, "mm/dd/yyyy"))
 
Upvote 0
Try this:
VBA Code:
MsgBox pi.Value & " - Day=" & Day(Format(pi.Value, "mm/dd/yyyy"))
Thank you. This helps to get the day correctly.

Now, the problem is how to compare pi.value with another date. I tried
VBA Code:
            If Format(Pi.Value, "mm/dd/yyyy") < Format("22/03/2022", "mm/dd/yyyy") Then
                MsgBox Format(Pi.Value, "mm/dd/yyyy") & " inferior " & Format("22/03/2022", "mm/dd/yyyy")
            Else
                MsgBox Format(Pi.Value, "mm/dd/yyyy") & " superior " & Format("22/03/2022", "mm/dd/yyyy")
            End If

Unfortunately, it does not work. For some date it does not work properly. Here I compare April 1st and March 22nd

1648128586895.png
 
Upvote 0
use the numberformat YYYYMMDD to compare both dates,
but at first look, pi.value is what type ? date, long, integer ???
then make the 2nd date the same type and compare
something like (without testing)
VBA Code:
i=sgn(cint(pi.value)-cint(dateserial(2022,3,22))) '=the sign of the substraction of both integer values)
select case i
 case -1:msgbox "smaller"
case 0:msgbox "equal"
case +1 :msgbox "greater"
end select
 
Upvote 0
See if this works for you, for both the If statement comparison and getting the day.
(I prefer Debug.Print, change that to msgbox if you prefer that)

VBA Code:
        Dim dt As Date
        Dim dtPvt As Date
       
        dt = DateSerial(2022, 3, 26)
        dtPvt = Application.Text(pi.Value, "d/mm/yyyy")
       
        If dtPvt < dt Then
            Debug.Print dtPvt & "  inferior; " & dt & " day " & Day(dtPvt)
        Else
            Debug.Print dtPvt & "  superior; " & dt & " day " & Day(dtPvt)
        End If
 
Upvote 0
See if this works for you, for both the If statement comparison and getting the day.
(I prefer Debug.Print, change that to msgbox if you prefer that)

VBA Code:
        Dim dt As Date
        Dim dtPvt As Date
      
        dt = DateSerial(2022, 3, 26)
        dtPvt = Application.Text(pi.Value, "d/mm/yyyy")
      
        If dtPvt < dt Then
            Debug.Print dtPvt & "  inferior; " & dt & " day " & Day(dtPvt)
        Else
            Debug.Print dtPvt & "  superior; " & dt & " day " & Day(dtPvt)
        End If

That works perfectly. I still have a lot of things to learn in order to code properly :D

Thank you very much !
 
Upvote 0
use the numberformat YYYYMMDD to compare both dates,
but at first look, pi.value is what type ? date, long, integer ???
then make the 2nd date the same type and compare
something like (without testing)
VBA Code:
i=sgn(cint(pi.value)-cint(dateserial(2022,3,22))) '=the sign of the substraction of both integer values)
select case i
 case -1:msgbox "smaller"
case 0:msgbox "equal"
case +1 :msgbox "greater"
end select

pi.value is a string (field of my pivot table) but refers to a date in my source table. As I had an issue some time to get the day and month I tried to format the date like a number with YYYYMMDD format but it is just a workaround.

I tried your code but does not work due to pi.value type. However I do not know why I get an overflow with cint(dateserial(2022,3,22))
1648188846984.png

Anyway I got an answer from @Alex Blakenburg so no worry

Thank you to all who worked on my problem. Really appreciate !
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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