Results 1 to 7 of 7
  1. #1
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    56

    Exporting Data to Excel and forcing Pivot Table Columns Even if Data Doesn't Exist

    Good morning.




    I have a routine that is exporting data to Excel and creating a pivot table.


    The column header for the pivot is "Age Group" and should always be "<5 Days", "5 - 10 Days", "11 - 20 Days", and ">20 Days" even if there is no data for the age group.


    Excel obviously doesn't order those columns correctly, so I have the following code to make sure they are in the correct order


    Code:
    .PivotTables(spivTableName).PivotFields("AgeGroup").ShowAllItems = True
    .PivotTables(spivTableName).NullString = "0"
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("<5 Days").Position = 1
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("5 - 10 Days").Position = 2
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("11 - 20 Days").Position = 3
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems(">20 Days").Position = 4

    Which works great as long as there is data for each of the columns; but if there is no data for one of the Age Groups, a 'Run-Time error 1004 Unable to get the PivotItem property of the PivotFields' is raised.


    How can I set the column headers to show even for no data in the data source range for that column?

  2. #2
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    The easiest way would be to ensure the export includes a blank data record for all those groups?
    I don't think you can force the Pivot to include missing data.

    The other answer would be to pivot the data in Access, fix the column headers and export the pivoted (Crosstab query) results directly.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I was wondering why they did not use a crosstab?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Office 365
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    56
    Exporting a crosstab as the summary loses all of the Excel pivot table functions.

    I have actually done the export crosstab, make links, etc... to mimic the pivot table functionality, but that takes exponentially longer. You also loss the ability to add slicers and other filters.

  5. #5
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2013 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    As Minty suggested, try to change the source query in Access to get you a 0 if no records for a certain category (using the Nz() function might be the easiest). A similar approach is used here but in Excel:
    https://www.youtube.com/watch?v=mVabPX6quQ0
    A have a free utility to do pivot tables from within Access, might come handy:
    https://www.forestby.mywhc.ca/ms-acc...able-designer/
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  6. #6
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 11 Office 365
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,538
    You could use Power Query to easily bring the data into the PQ Editor and then close and load to an excel pivot table.

    To connect Power Query with Access, follow these steps:

    1. Open Excel and go to the "Data" tab.
    2. Click on "Get Data" in the upper left corner.
    3. From the drop-down menu, select "From Database" and then "From Microsoft Access Database".
    4. Navigate to the location of your Access Database file, select it and click "Import".
    5. Power Query Editor will open, and you can select the tables or queries you want to load into Excel.
    6. Click "Load" to load the data into Excel.

  7. #7
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Office 365
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    56
    The following seems to be working as expected.

    Code:
    intCol = 1.PivotTables(spivTableName).PivotFields("AgeGroup").ShowAllItems = True
    .PivotTables(spivTableName).NullString = "0"
    On Error Resume Next
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("<5 Days").Position = intCol
    If Err.Number = 1004 Then
    	intCol = intCol
    Else
    	intCol = intCol + 1
    End If
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("5 - 10 Days").Position = intCol
    If Err.Number = 1004 Then
    	intCol = intCol
    Else
    	intCol = intCol + 1
    End If
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems("11 - 20 Days").Position = intCol
    If Err.Number = 1004 Then
    	intCol = intCol
    Else
    	intCol = intCol + 1
    End If
    .PivotTables(spivTableName).PivotFields("AgeGroup").PivotItems(">20 Days").Position = intCol
    If Err.Number = 1004 Then
    	intCol = intCol
    Else
    	intCol = intCol + 1
    End If
    On Error GoTo 0

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 51
    Last Post: 07-05-2022, 03:24 PM
  2. Replies: 2
    Last Post: 11-15-2016, 03:49 PM
  3. Replies: 4
    Last Post: 07-10-2015, 07:51 AM
  4. Replies: 2
    Last Post: 01-23-2015, 12:14 PM
  5. Multiple True/False columns, forcing behaviour
    By RoscreaMou in forum Access
    Replies: 8
    Last Post: 05-08-2012, 04:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums