Pivot Table VBA Question

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
769
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

Is there a way to use two cell references (A1 From Date & A2 To Date) to update a filter in a pivot table without actually showing the filter itself?

I have used the following code to align a name of the person who loads the file so they can only see their data, but I am insure how I would get the
"ActiveSheet.PivotTables("AgentManager").PivotFields("Call Date")" to look between the first date ("A1") and the final date ("A2"), is this something that can be done.

Code:
Sub AgentManagerAlign()

Dim pvFld As PivotField
Dim strFilter As String

   Set pvFld = ActiveSheet.PivotTables("AgentManager").PivotFields("Team Manager")
   strFilter = ActiveWorkbook.Sheets("LiveStaffLoader").Range("B5").Value
   pvFld.CurrentPage = strFilter

End Sub

thanks in advance
Gavin
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
I haver tried the following but I get an error "Run-time error '1004': Application-defined or object-defined error on the last line and I havent a clue where to go

Code:
DateFrom = Worksheets("Sheetname").Range("B12").Value
DateTo = Worksheets("Sheetname").Range("B13").Value
    
ActiveSheet.PivotTables("tablename").PivotCache.Refresh
ActiveSheet.PivotTables("tablename").PivotFields("FilterFieldDate").ClearAllFilters
ActiveSheet.PivotTables("tablename").PivotFields("FilterFieldDate").PivotFilters.Add Type:=xlDateBetween, Value1:=DateFrom, Value2:=DateTo
 
Upvote 0
sorry @SpillerBD what do you mean in the "Date From" part Text(worksheets("sheetname").range("").Value?
 
Upvote 0
I have tried the following as when I try Text I get an error Compile error : sub or function not defined
With the code below I am back at the 1004 error

Code:
DateFrom = Format(Worksheets("LiveStaffLoader").Range("B12").Value, "Short Date")
DateTo = Format(Worksheets("LiveStaffLoader").Range("B13").Value, "Short date")

thanks in advance
 
Upvote 0
FORMAT can work, but I think your defining the short date with the wrong terminology.
VBA Code:
DateFrom = Format(Worksheets("LiveStaffLoader").Range("B12").Value, vbShortDate)
TEXT is worksheet function and need to called like... (I make this poor choice because it is sometimes easier to expect others to recognize the Excel function versus the VBA function.
VBA Code:
DateTo = WorksheetFunction.Text(Worksheets("LiveStaffLoader").Range("B13").Value, "MM/DD/YYYY")

I guess another potential issue is if the cell your dates are going into are already formatted as Text, then trying to use functions in the VBA expecting dates will be a problem in reverse.
 
Upvote 0
afternoon @SpillerBD,

I am thinking instead of trying to do a to and from date just choose a week number either setting a slicer when the update is done that way I can adjust a few tables in 1 go?

Do you think this would be a good idea, but whats the best way to do this?
I have seen the below code, but I am unsure how to get the CWN value to be select as that would be the current week for the filter to align to as well as the CYear for the correct year
Code:
Dim pf As PivotField

CYear = Worksheets("LiveStaffLoader").Range("B11").Value
CWN = Worksheets("LiveStaffLoader").Range("B12").Value
PWN = Worksheets("LiveStaffLoader").Range("B13").Value

PivotTables("PivotTable2").ClearAllFilters
Set pf = ActiveSheet.SlicerCaches("Year") _
     .VisibleSlicerItemsList = VisibleItemsList(Select)

thanks in advance
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

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