onelifenoregret
New Member
- Joined
- Sep 13, 2013
- Messages
- 13
Let me preface this by saying that I am extremely new to VBA. I'm trying to automate tasks in Pivot Tables like setting date ranges so that instead of having to drill down through all of the menus I can just hit a button and it does it. I was using a Worksheet full of formulas before but I'm trying to get away from that. I need to sort date ranges from Thursday morning to Wednesday night as well as first to last day of the month. I was using the formula "=TEXT((TODAY()+(7-(N(WEEKDAY(TODAY(),14))))),"mm/dd/yy")" to get my dates, that formula returns 05/13/15 I would then concatenate and use left mid and right to turn that into [Dates].[Date].&[2015-05-13T00:00:00] etc. I'm sure I was not being efficient, I know some stuff about excel but not enough to make me efficient or really all that good. With a PivotField like that above my code would then look like
My questions are.... is there a thorough tutorial or walkthrough on how to use pivottables pivotfields cubefields etc. What is the easiest way to get those date ranges, and exactly how much of my life am I wasting by doing things the long hard way using fifty cells with fifty formulas to get one output?
Code:
Sub currentweek()
' macro to return the current week last Thursday to this Wednesday
Dim pt As PivotTable
Set pt = ActiveSheet.PivotTables("PivotTable1")
Dim dateArray As Variant
dateArray = Application.Transpose(Range("AB2:AB8").Value)
On Error Resume Next
pt.CubeFields(1).EnableMultiplePageItems = _
True
pt.PivotFields("[Dates].[Date].[Date]"). _
VisibleItemsList = dateArray
End Sub