Solved

Need help getting a date in a Pivot Table.

Posted on 2014-02-11
10
383 Views
Last Modified: 2014-02-12
This was too hard to explain, so I added a comment to the code were I needed an edit.

First is Ln6, I need the pivot table to use whatever the selection area is (like UsedRange).  It cannot be the same each time.

Second there are two places and I am trying to get a proper date in a pivot table.  Please see Ln43 and Ln46 below.

Could someone help with these edits?  Please advise and thanks. -R-


Sub AddNewPivotTable()
'
    Range("A1").Select
    Sheets.Add
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "CDPSRECRPT!R1C1:R5232C36", Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:="Sheet1!R3C1", TableName:="PivotTable3", DefaultVersion _
        :=xlPivotTableVersion12
    Sheets("Sheet1").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotFields( _
        "Short Description               ")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("End Date  ")
        .Orientation = xlRowField
        .Position = 3
    End With
    ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
        "PivotTable3").PivotFields("Oper. Qty"), "Sum of Oper. Qty", xlSum
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("End Date  ")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl").CurrentPage = _
        "(All)"
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl")
        .PivotItems("SERVICE PART").Visible = False
        .PivotItems("SERVICE PART  ").Visible = False
    End With
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl"). _
        EnableMultiplePageItems = True
    ActiveSheet.PivotTables("PivotTable3").PivotFields("End Date  ").PivotFilters. _
        Add Type:=xlBefore, Value1:="3/16/2014"  'Need this to be 6 weeks out from the past Sundays date.
        
    Range("C4").Select
'this date has to be whatever last Sundays date was?

    Selection.Group Start:=41679, End:=True, Periods:=Array(False, False, False _
        , True, True, False, False)
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Chairs"
    Range("C2").Select
End Sub

Open in new window

0
Comment
Question by:RWayneH
  • 7
  • 3
10 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
ID: 39852636
On line 6, if you just want what is selected then

SourceData:=Selection.Address

Open in new window


As for determining the dates, I would use the Application.WorksheetFunction and use if the WEEKDAY function

Last Sunday is:
LastSunday=Date() - Application.WorksheetFunction.weekday(Date()) + 1

Six weeks from that is
NextDate=LastSunday + (6 * 7)
0
 

Author Comment

by:RWayneH
ID: 39852823
Not sure how to edit line 6, when I start the pivot table all I did was select cell A1 and Excel put the rest in.  Can you assist in putting the edits into the procedure?  

If I understand the dates, if today is Wednesday the 12th it should enter the date of 2-9-2014, and if today is 2-19-2014 it would enter the date 2-16-2014  correct?  -R-
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39852928
Re line 6, change

"CDPSRECRPT!R1C1:R5232C36"
 to
Selection.Address

Note that the lack of quotes

========

If I understand the dates, if today is Wednesday the 12th it should enter the date of 2-9-2014, and if today is 2-19-2014 it would enter the date 2-16-2014  correct?  -

That's how I understand it. That's what the formulas that I mentioned week give you
0
 

Author Comment

by:RWayneH
ID: 39853332
I am still having issues adding these to the procedure... need help with this. -R-
0
 

Author Comment

by:RWayneH
ID: 39853342
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:RWayneH
ID: 39853416
I believe I need to set the pivot table range name.


Range("A1").Select
Range(Selection, Selection.End(xlDown)).Name = "PivotTableRange"  'added to set range

then reference the name in the create.
However this just uses a column, I need to do add a horizontal part of the range.
Selection.End(xlToRight)).Select  so the xldown and xlToRight are used to set the range?

How do I set the range.  After this I can work on the dates.
0
 

Author Comment

by:RWayneH
ID: 39853437
ok... I got the range to work by using:
 Range("A1").Select
    Range(Selection, Selection.End(xlDown).End(xlToRight)).Name = "PivotTableRange"  'added to set range
    Sheets.Add.Name = "Sheet1"
    Sheets("Sheet1").Activate
    Range("A1").Select
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "PivotTableRange", Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:="Sheet1!R3C1", TableName:="PivotTable3", DefaultVersion _
        :=xlPivotTableVersion12

Open in new window


Now the dates..
0
 

Author Comment

by:RWayneH
ID: 39853509
figured it out, all by myself. (with your help).  This is what worked.

Sub AddNewPivotTable()
'
    Range("A1").Select
    Range(Selection, Selection.End(xlDown).End(xlToRight)).Name = "PivotTableRange"  'added to set range
    Sheets.Add.Name = "Sheet1"
    Sheets("Sheet1").Activate
    Range("A1").Select
    
    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        "PivotTableRange", Version:=xlPivotTableVersion12).CreatePivotTable _
        TableDestination:="Sheet1!R3C1", TableName:="PivotTable3", DefaultVersion _
        :=xlPivotTableVersion12

    Sheets("Sheet1").Select
    Cells(3, 1).Select
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl")
        .Orientation = xlRowField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotFields( _
        "Short Description               ")
        .Orientation = xlRowField
        .Position = 2
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("End Date  ")
        .Orientation = xlRowField
        .Position = 3
    End With
    ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
        "PivotTable3").PivotFields("Oper. Qty"), "Sum of Oper. Qty", xlSum
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl")
        .Orientation = xlPageField
        .Position = 1
    End With
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("End Date  ")
        .Orientation = xlColumnField
        .Position = 1
    End With
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl").CurrentPage = _
        "(All)"
    With ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl")
        .PivotItems("SERVICE PART").Visible = False
        .PivotItems("SERVICE PART  ").Visible = False
    End With
    
    LastSunday = Date - Application.WorksheetFunction.Weekday(Date) + 1
    NextDate = LastSunday + (6 * 7)
    
    ActiveSheet.PivotTables("PivotTable3").PivotFields("Basic Matl"). _
        EnableMultiplePageItems = True
    ActiveSheet.PivotTables("PivotTable3").PivotFields("End Date  ").PivotFilters. _
        Add Type:=xlBefore, Value1:=NextDate  'Need this to be 6 weeks out from the past Sundays date.
        
    Range("C4").Select
'this date has to be whatever last Sundays date was?
'Used LastSunday set above  this was =41679
    Selection.Group Start:=LastSunday, End:=True, Periods:=Array(False, False, False _
        , True, True, False, False)
    Range("C1").Select
    ActiveCell.FormulaR1C1 = "Chairs"
    Range("C2").Select
End Sub

Open in new window

0
 

Author Closing Comment

by:RWayneH
ID: 39853514
Thanks. -R-
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39854441
Thanks for the points and glad you could figure It out while I was sleeping :-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This collection of functions covers all the normal rounding methods of just about any numeric value.
This article will show you how to use shortcut menus in the Access run-time environment.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now