Ask Experts Questions for FREE Help !
Ask
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #1

    Jan 6, 2011, 10:14 AM
    Question about using a date variable in a Pivot table
    I'm trying to tweak a piece of my code to allow me to use a date as a variable in selecting information in my pivot table. Here's my code:

    Sub RS_IBOB_Open_Select()
    '
    ' RS_IBOB_Open_Select Macro
    '

    '
    SelectDate = Format(Date - 1, "YYYYMMDD")
    WShtDate = Format(Date - 1, "mm.dd.yy")
    BOCM = Format(DateSerial(Year(Date), Month(Date), 1), "mm.yyyy") 'Begin of Curr Month


    Application.DisplayAlerts = False


    Workbooks.Open Filename:= _
    "http://mminet/sites/fcmgt/Michael%20Cagan/RS%20Day-Week-Month/Updated%20Daily/Right%20Start%20Inbound-Outbound/RS%20Inbound-Outbound%20Tracking%20" & BOCM & ".xlsx"


    Sheets(WShtDate).Select

    Workbooks.Open Filename:="H:\BI Reports\RS IB - OB.xlsx"

    ActiveSheet.PivotTables("PivotTable1").PivotCache. Refresh

    ActiveSheet.PivotTables("PivotTable1").PivotFields ("[Date].[Day].[Day]"). _
    VisibleItemsList = Array("[Date].[Day].&[20110105]")


    End Sub


    The very last row is the one I am interested in making a variable:

    VisibleItemsList = Array("[Date].[Day].&[20110105]")

    Can I make the 20110105 a variable? I've tried to play around with the code to do something like

    SelectData = format(Date - 1, "yyyymmdd")

    VisibleItemsList = Array("[Date].[Day].&[SelectData]")

    That didn't work so I tried

    VisibleItemsList = Array("[Date].[Day].&[" & SelectData &"]")


    Any thoughts?
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #2

    Jan 8, 2011, 07:23 AM

    I would have suggested your last line of code, but if that doesn't work, I don't know off the top of my head. I do very little work with Pivot Tables. (shame)

    If you can provide a sample workbook with your macro installed, as well as that external reference file, I can take a look and see if I can suss it out.
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #3

    Jan 11, 2011, 09:05 AM
    Quote Originally Posted by JBeaucaire View Post
    I would have suggested your last line of code, but if that doesn't work, I don't know off the top of my head. I do very little work with Pivot Tables. (shame)

    If you can provide a sample workbook with your macro installed, as well as that external reference file, I can take a look and see if I can suss it out.
    Jerry - thanks for offering to take a stab at it... I would send you the file but it's sensitive company info... gosh, it's hard to find an answer to this question on other forums, too; I guess pivot tables are not popular :(

    Appreciate you, man.
    JBeaucaire's Avatar
    JBeaucaire Posts: 5,426, Reputation: 997
    Software Expert
     
    #4

    Jan 11, 2011, 03:13 PM

    It usually takes less than 3 minutes to desensitize a copy of your workbook. FOr names, put NAME1 in the first name cell, then double-click the lower right corner to copy it down, causing all the names to change to:

    NAME1
    NAME2
    NAME3
    etc...

    Now repeat that with ADDR1, and CITY1, COMPANY1.

    You get the idea. It shouldn't be THAT hard to desensitize a wb to the point it can be worked on.
    jakester's Avatar
    jakester Posts: 582, Reputation: 165
    Senior Member
     
    #5

    Jan 12, 2011, 10:07 AM
    Quote Originally Posted by JBeaucaire View Post
    It usually takes less than 3 minutes to desensitize a copy of your workbook. FOr names, put NAME1 in the first name cell, then double-click the lower right corner to copy it down, causing all the names to change to:

    NAME1
    NAME2
    NAME3
    etc...

    Now repeat that with ADDR1, and CITY1, COMPANY1.

    You get the idea. It shouldn't be THAT hard to desensitize a wb to the point it can be worked on.
    Jerry - I figured it out... with a little help, though. Here's what I did (in an abbreviated form of my earlier code:

    Sub Macro1()
    '
    ' Macro1 Macro
    '

    Dim dateselect As String

    mydate = Format(Date - 2, "yyyymmdd")

    dateselect = "[Date].[Day].&[" & mydate & "]" 'pivotfield string



    ActiveSheet.PivotTables("PivotTable1").PivotFields ("[Date].[Day].[Day]"). _
    VisibleItemsList = Array(dateselect)

    End Sub

    Anyway, just thought I'd share the solution with you in case you came across something like it again.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search


Check out some similar questions!

Question about Dynamic Range for Pivot Tables - Excel 2007 [ 1 Answers ]

I have tried writing a module that will create a pivot table in a named worksheet with a range of data that changes from day to day. I was able to do with easily in Excel 2003 but when I tried to duplicate that in Excel 2007, it doesn't work. It fails with a Run-time error 5: Invalid procedure...

Get max of date field in a table for a particular month [ 3 Answers ]

Hi, I have data in a table as follows: Key date 101 4/25/2006 101 4/26/2006 101 4/27/2006 101 4/28/2006 101 5/25/2006

Using fuction "Mode" within a Pivot Table? Or calculating the mode with conditional? [ 4 Answers ]

I want to know which number appears most frequently within a table by instructor code. For example, I want to produce a report that searches column A for an instructor code (for example, any instructor code "ba*") and then finds the mode of the corresponding cell in column B. I know I could...

Excel - frequency / mode / pivot table. [ 0 Answers ]

Hi there, I'm struggling to achieve my end goal in Excel; I have a list of dates (presently the list of dates is divided into individual worksheets with 20 rows and 5 columns of dates per worksheet. I have 30-40 worksheets). My objective is to obtain a list of the dates that match...

Question on placed-in-service date [ 1 Answers ]

Hello, I have a question regarding the placed-in-service date for rental property. I had a room ready as a rental in which the renter moved-in during the same month. Their first rental payment was the beginning of the next month. Therefore, is the placed-in-service date when the room was...


View more questions Search