3

Here is my problem, every bit of code, every alteration, every type, doesn't work. I'm using Office 360 at my work site (up to date), so it's excel 2016 and VBA 7.1.

What I'm looking to do is automate our end of shift reports. Here's the process:

We enter data into an excel sheet (Log) every hour. At the end of the day, at 5:00 AM, we save and close that log, open another excel sheet that IMPORTS the data into power pivot, and displays it on a PivotTable (formatting for printing for our bosses), and we choose the filter for the previous date using the filter drop down, and print it. We do this with three (3) reports: 2 PivotTables, and 1 PivotChart. Power Pivot imports ALL of the data from the Log sheet to reformat it for printing.

I've successfully managed to get and rewrite the code (beginner at this) for the automation process of: auto saving the log, closing the log, opening the Report workbook, refreshing the data, and printing the data, then closing the report. The only part I'm now missing is the auto-filtering.

The code I've tried is vast, but here's an example of what I've tried recently (I've erased and re-copied so many codes...)

Sub Filter_PivotField()
'Description: Filter a pivot table or slicer for a specific date or period
'Source: excelcampus.com/vba/filter-pivot-table-slicer-recent-date-period

Dim sSheetName As String
Dim sPivotName As String
Dim sFieldName As String
Dim sFilterCrit As String
Dim pi As PivotFields

    'Set the variables
    sSheetName = "EOS Report"
    sPivotName = "PivotTable1"
    sFieldName = "Date"
    sFilterCrit = "xlDateYesterday"
    'sFilterCrit = ThisWorkbook.Worksheets("EOS Report").Range("O1").Value
    
    With ThisWorkbook.Worksheets(sSheetName).PivotTables(sPivotName).PivotFields(sFieldName)
        'Clear all filter of the pivotfield
        .ClearAllFilters
        
        'Loop through pivot items of the pivot field
        'Hide or filter out items that do not match the criteria
        For Each pi In .PivotFields
            If pi.Name <> sFilterCrit Then
                pi.Visible = False
            End If
        Next pi
        
    End With

End Sub

To no avail....

When I record a macro doing the manual filter, I get this:

Sub manualfilter()
'
' manualfilter Macro
'

'
    ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "[Bi-Hourly Report].[Date].[Date]").VisibleItemsList = Array( _
        "[Bi-Hourly Report].[Date].&[2016-09-28T00:00:00]")
End Sub

But it fails when I try to re-run the same macro that I just recorded (after changing the date back). I've enabled and disabled multiple selection option, etc.

Not to mention, trying to auto-filter a chart is a nightmare because tables, yea there's tons of articles on it, but charts? not much comes up on researching.

Here's images of the filter button, because almost everything I've researched is to sort the COLUMN of the Table, not the filter itself with a PivotTable.

Table Filter

Chart Filter

I cannot post the actual excel spreadsheets as they are proprietary property of the company, but I can replicate the format with false data if needed.

eglease
  • 2,445
  • 11
  • 18
  • 28
DevilzEye
  • 31
  • 1
  • 4

1 Answers1

2

check this out.

Dim prev_date As String

prev_date = Month(Date - 1) & "/" & Day(Date - 1) & "/" & Year(Date - 1)
Thisworkbook.Sheets("Sheet1").Activate 
'change this line with your sheet where pivot table is present. Change Sheet name.
ActiveSheet.PivotTables("PivotTable1").RefreshTable
ActiveSheet.PivotTables("PivotTable1").PivotFields("Date").CurrentPage = prev_date
Aditya Pansare
  • 1,112
  • 9
  • 14
  • I modified my recorded macro and replaced with the code you posted, and under debug > show definition, it says Identifier under cursor not recognized, and the highlighted portion is the last 2 lines, with the cursor on Type:=xlDateYesterday – DevilzEye Oct 01 '16 at 11:56
  • In the pivot table, where you have dragged the "Date" column. It must be in "Rows" section for this code to work. I doubt it is in "Report Filter" Section. Please check. – Aditya Pansare Oct 01 '16 at 12:25
  • In the Field list, "Date" is in the "Filters" section. That is how the reports are set up because it is a day basis, just like the screenshot in the OP. I can take another screenshot if needed so you can see how it is designed. This report is for an end of day production type report for our boss. All of the VBA i've found manipulate the columns, and i have yet to find one to modify the "Filters" field of the PivotTable... :( – DevilzEye Oct 01 '16 at 12:38
  • I've updated the code as per your requirement. Please check. – Aditya Pansare Oct 01 '16 at 12:57
  • Hmm, now it says on the last line that "Cannot jump to ActiveSheet because it is hidden" and opens the Object Browser... no idea why it's saying that because there are no hidden sheets in this workbook :S – DevilzEye Oct 01 '16 at 13:11
  • Okay write a line to activate the sheet on which pivot table is present. check the code again. i've added one more line – Aditya Pansare Oct 01 '16 at 13:13
  • I updated the sheet name as per your instruction, and it throws the same error. The sheet name is "EOS Report" for reference. I copied/pasted and updated the name, and it does the same thing.. Oddly enough since there arent any hidden sheets, nor are there any workbooks open except the one in question. I greatly appreciate your assistance in this, however, because i've been researching this for a long time, and someone taking the time to assist me is greatly appreciated – DevilzEye Oct 01 '16 at 13:23
  • Could you please show me complete code you have written? – Aditya Pansare Oct 01 '16 at 13:26
  • I'm currently using only the code you've pasted before implementing it into my own, and using a Form button to trigger the macro. The remaining code basically just calls other macros for automating the entire thing. I have macros set up for opening the book, one for refreshing all of the tables, this one for filtering, which is being written now, and then a final macro to print everything. The code for the filtering is only what you've posted currently. I've copied so many from the internet searches that none of them worked, so to get it to just "work", im only using what you posted... – DevilzEye Oct 01 '16 at 13:28
  • check this workbook. and verify your code.... https://drive.google.com/open?id=0BwkzD11CZOYYLW5KNUlnQzJ1U3c .... exactly similar code works in this workbook. – Aditya Pansare Oct 01 '16 at 13:33
  • Ah see i think thats the problem, you input your sample data on Sheet1, and put the table on EOS Report, another sheet. In my workbook, i'm using PowerPivot (Ribbon tab Power Pivot > Manage) to import the data from another excel file, and then exporting that into a PivotTable into the sheet. The setup is basically the same, with the exception of the PowerPivot managing it. I'll see if i can upload it to Google drive for you to take a look at, but since this is a work machine, it may get blocked.... – DevilzEye Oct 01 '16 at 13:55
  • Ok, i've created a mimic of the setup we use here. This is just a replica with garble data in it. The setup we use is actually about 30 columns wide, and has thousands of rows (12 entries a day), and the report summarizes this. The report when printed is scaled down to fit on one page, and it's small print, if that gives an idea as to how much data is processed lol.... Log excel sheet: https://drive.google.com/open?id=0B9ji2xyhPSYScV9Fanpha3pzYVE Report excel sheet (PowerPivot imports from log): https://drive.google.com/open?id=0B9ji2xyhPSYSTnREUnZnSjJidFk Files are editable btw – DevilzEye Oct 01 '16 at 14:18
  • I would not be able to access source data. You can create a another pivot on your log to pull the report. Just a suggestion. And sorry for unable solve your problem completely. – Aditya Pansare Oct 01 '16 at 14:48
  • Thats why i uploaded 2 files, one is the log, and the other is the report. The log being the source, and the report pulling from it into power pivot. Also, I've updated the uploaded 2 files to version 2 and added some elements of our sheets. We also have a chart that needs the same auto filtering vba code.... – DevilzEye Oct 01 '16 at 14:48
  • Hi, Here is some workaround which could help. https://drive.google.com/open?id=0BwkzD11CZOYYXzdUVGh0cnZMZGM – Aditya Pansare Oct 01 '16 at 16:23