+ Reply to Thread
Results 1 to 15 of 15

Change Pivot Items via VBA does not work

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Change Pivot Items via VBA does not work

    Hi Guys,

    i have Source data with dates from 01.01.2020 until 30.12.2020.

    From this i created pivot table and what want to do is to change all Pivot Items names to corresponding month.

    Using this code:

    Option Explicit
    
    Sub test()
    
    Dim pt As PivotTable
    
    With Sheet2
    
    Set pt = .PivotTables(1)
    
    Dim pi As PivotItem
    
    With pt
      For Each pi In .PivotFields("Dates").PivotItems
        
        Dim monthNaming As String
        Dim MonthFormat As String
        On Error Resume Next
        
            MonthFormat = Format(pi.Name, "yyyy-dd-mm")
            monthNaming = StrConv(MonthName(CLng(Month(CDate(MonthFormat)))), vbProperCase)
            If Err.Number = 0 Then
                pi.Name = monthNaming
            End If
        On Error GoTo 0
        
      Next pi
    End With
      
    End With
    
    End Sub
    and this is working only for first row!
    Why? Can anybody help?
    Very strange.

    Best,
    Jacek
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    04-01-2015
    Location
    The Netherlands
    MS-Off Ver
    2003/2007/2010/2016/office 365
    Posts
    880

    Re: Change Pivot Items via VBA does not work

    Take this away.
    On Error resume Next
    The name of a pivot item must be unique.

  3. #3
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Change Pivot Items via VBA does not work

    Thank you!

    what i am trying to do here is gruop by months:

    Screenshot_70.png

    but when i am running code after grouping it is not working....

    Can you please help?
    Jacek

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Change Pivot Items via VBA does not work

    That code wouldn't make any sense after grouping by month as far as I can see.
    Remember what the dormouse said
    Feed your head

  5. #5
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Change Pivot Items via VBA does not work

    yes this is not having sense at all.

    But Rory, it is not possible to change 3 letter month to defined by user besides changing XML file ;/

    And i do not want to do this...

    Jacek

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Change Pivot Items via VBA does not work

    I don't know what you're talking about I'm afraid.

  7. #7
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Change Pivot Items via VBA does not work

    Sorry Rory,

    i am talking about this issue which i though that will solve with the topic approach:

    https://www.excelcampus.com/pivot-ta...ld-formatting/

    To change Jan name after grouping date by month to January.
    But Excel uses default which is saved in xml file.

    Best,
    Jacek

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Change Pivot Items via VBA does not work

    Yes, but as shown in that link, you can change the month names after grouping. Not sure why you would want to though.

  9. #9
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Change Pivot Items via VBA does not work

    The code from link is not working because you can not have duplicates and you are changing 01.01.2020 to Jan, and want to change also 02.02.2020 but here you will get error because of duplicate.
    It is not looping through months but for detailed date.

    Best,
    Jacek

  10. #10
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Change Pivot Items via VBA does not work

    Like I said, you can change the month name after grouping. It wouldn't make sense to do it beforehand.

  11. #11
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Change Pivot Items via VBA does not work

    Hi Rory!

    Thank you for saying that, can you help with it?

    The code from link is not working from me. How can i do this?

    Jacek

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Change Pivot Items via VBA does not work

    What exactly do you want as the result (and why)?

  13. #13
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Change Pivot Items via VBA does not work

    I want to have instead of image above months names get:

    Sty - Styczeń (Jan - January)
    Lut - Luty (Feb - February)

    So after grouping by month i want to change labels for months.
    As default you get 3 letters for month. I do not want that - this is ugly and want to have nice name - fully name.

    Jacek

  14. #14
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Change Pivot Items via VBA does not work

    Do you have a particular reason for not just adding a month name column to your source data/query? It makes life a lot easier.

    If you really need code, just group the months then use something like:

    Option Explicit
    
    Sub test()
    
    Dim pt As PivotTable
    
    With Sheet2
    
    Set pt = .PivotTables(1)
    
    Dim pi As PivotItem
    
    With pt
      For Each pi In .PivotFields("Dates").PivotItems
        
        Dim monthNaming As String
        Dim MonthFormat As String
        On Error Resume Next
        
            
            monthNaming = Format(CDate("1900-" & pi.Name & "-1"), "mmmm")
            If Err.Number = 0 Then
                pi.Name = monthNaming
            End If
        On Error GoTo 0
        
      Next pi
    End With
      
    End With
    
    End Sub
    Last edited by romperstomper; 07-09-2020 at 04:24 AM.

  15. #15
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,940

    Re: Change Pivot Items via VBA does not work

    thank you!!!

    Working!

    Jacek

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pivot charts no longer work correctly at change of year
    By SJMaye in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 01-15-2016, 06:54 AM
  2. Filter pivot items using pivot items from another table
    By DKolev in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-16-2014, 08:49 PM
  3. Listbox, Need to change number of items in List (not the items)
    By Kalithro in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2013, 09:23 PM
  4. [SOLVED] retrieve child items from visible pivot items.
    By MarMo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2013, 10:24 AM
  5. Replies: 1
    Last Post: 09-03-2012, 10:03 PM
  6. [SOLVED] Macro has to run twice to change correct pivot items
    By fratello in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 07-28-2012, 10:58 PM
  7. Change Pivot table Filter Based on Cell Value *Multiple Filter items* Possible?
    By Flydd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2012, 06:57 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1