Results 1 to 20 of 20

Thread: [RESOLVED] Problem with VB Macro in Excel for Pivot Table

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Resolved [RESOLVED] Problem with VB Macro in Excel for Pivot Table

    Hi

    I am generating a pivot table out of worksheet named: Reporting

    I recorded an excel macro of the steps to produce the pivot table:
    VB Code:
    1. ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
    2.         "Reporting!R1C1:R378C31"
    3.     ActiveSheet.PivotTables("PivotTable3").SmallGrid = False
    4.     ActiveSheet.PivotTables("PivotTable3").AddFields RowFields:=Array("Severity", _
    5.         "Open/Closed"), ColumnFields:="Area"
    Because the range of cells changes I have produced theses variables:

    LastRow
    LastCol

    I am trying to use those in the code above such that:
    VB Code:
    1. LastRow = ":R" & LastRow
    2.  
    3.     ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:=“Reporting!R1C2” & LastRow + “C” + LastCol
    4.     ActiveSheet.PivotTables("PivotTable3").SmallGrid = False
    5.     ActiveSheet.PivotTables("PivotTable3").AddFields RowFields:=Array("Severity", _
    6.         "Open/Closed"), ColumnFields:="Area"
    When in this format the code doesn't work.

    Cheers

    Dave
    Last edited by Hack; Apr 13th, 2006 at 08:38 AM. Reason: Added [vbcode] [/vbcode] tags for more clarity.

  2. #2
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Problem with VB Macro in Excel for Pivot Table

    This is not Visual Basic, but Excel-VBA. These are not the same. Please ask at the proper place..


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Problem with VB Macro in Excel for Pivot Table

    Excel VBA question moved to Office Development

  4. #4
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    You need to add a full-colon : between the row and column references

    VB Code:
    1. ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:=“Reporting!R1C2” & LastRow + “[B]:[/B]C” + LastCol
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    Tried this but when I run the macro the

    :C

    part of the code is highlighted and an error "Sub or Function not Defined"


    ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:=“Reporting!R1C2” & LastRow + “:C” + LastCol

  6. #6
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    OK, lets take a step back.

    Using the wizard in your code is probably not the best idea.

    Do you need to create a new PT every time the code is run, or do you just need to update the source range for an existing PT?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    The wizard was in there because that was the route through which I went whilst the Macro was recording.

    A new PT is created every time the macro is re-run and it is likely that the range of the spreadsheet will change.

  8. #8
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    Is it a requirement that a new table is created, or is it just because that is the way the macro was recorded?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    It is a requirement because every time the macro is run it will have new source data therefore the table will be different

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    As long as the Fields (Columns) are staying the same and it is just the Data (Rows) that are changing, then there is no reason to creat a new PT, you can just change the source range of your existing table and refresh the pivot cache.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    How can the source range be set automatically every time the macro is run?

  12. #12
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    OK
    In the following code I have made some assumptions. that you may need to change, apart from these the code will show you how to change the source data and refresh your existing PT.

    Assumptions
    1/ You data is contained in a worksheet called "Reporting"
    2/ Your data in the "reporting" worksheet starts in cell A1
    3/ There is nothing else in the "reporting" worksheet, i.e no totals, no analysis below or to the right of the datatable.
    4/ Your pivot table is in a worksheet called "PivotSheet" (you will definetly need to change this one)...
    5/ It is the only PT on that sheet

    VB Code:
    1. Sub UpdatePivot()
    2. Dim MyPivot As PivotTable
    3. Dim rngSource As Range
    4.    
    5.     'Get a reference to the Pivot Table
    6.     Set MyPivot = ThisWorkbook.Worksheets("PivotSheet").PivotTables(1)
    7.    
    8.     'Get a reference to the source data table
    9.     Set rngSource = ThisWorkbook.Worksheets("Reporting").UsedRange
    10.    
    11.     With MyPivot
    12.        
    13.         'Update the Source data of the PT
    14.         .SourceData = "Reporting!" & rngSource.Address(ReferenceStyle:=xlR1C1)
    15.        
    16.         'Refresh the dat in the PT
    17.         .RefreshTable
    18.        
    19.     End With
    20.    
    21.     'Clear Object Variables
    22.     Set rngSource = Nothing
    23.     Set MyPivot = Nothing
    24. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    Run time error:1004

    The pivot table field name is not valid. To create a pivot table you must use data that is organised as a list with labelled columns.

  14. #14
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    Are all of my assumptions valid?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    2/ Your data in the "reporting" worksheet starts in cell A1
    A1 is colum header name - i thought this was needed?

    5/ It is the only PT on that sheet
    - there are more than 1 PT one that sheet. The PT is number 3 so I set as follows:

    Set MyPivot = ThisWorkbook.Worksheets("PivotSheet").PivotTables(3)

  16. #16
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    Sorry, when I said data I was including the Column Headers, so yes you do need the headers in Row 1.
    Can you upload the worbook or is it confidential?

    My best guess right now is that the problem is with the UsedRange property of the worksheet.
    Can you try the following. Its the same code, but instead of using the UsedRange property, I want you to insert the code you previously used to determine the Last Row and Column numbers.

    VB Code:
    1. Sub UpdatePivot()
    2. Dim LastRow As Long
    3. Dim LastCol As Long
    4. Dim MyPivot As PivotTable
    5. Dim rngSource As Range
    6.    
    7.     'Get a reference to the Pivot Table
    8.     Set MyPivot = ThisWorkbook.Worksheets("PivotSheet").PivotTables(1)
    9.    
    10.     'Insert whatever code you were using to get
    11.     'the last row and column numbers here
    12.    
    13.    
    14.     'Get a reference to the source data table
    15.     With ThisWorkbook.Worksheets("Reporting")
    16.         Set rngSource = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
    17.     End With
    18.     With MyPivot
    19.        
    20.         'Update the Source data of the PT
    21.         .SourceData = "Reporting!" & rngSource.Address(ReferenceStyle:=xlR1C1)
    22.        
    23.         'Refresh the dat in the PT
    24.         .RefreshTable
    25.        
    26.     End With
    27.    
    28.     'Clear Object Variables
    29.     Set rngSource = Nothing
    30.     Set MyPivot = Nothing
    31. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    Sub MacroMainPivot()

    Dim LastRow As Long
    Dim LastCol As Long
    Dim MyPivot As PivotTable
    Dim rngSource As Range

    'Get a reference to the Pivot Table
    Set MyPivot = ThisWorkbook.Worksheets("Statistical analysis").PivotTables(3)

    'Insert whatever code you were using to get
    'the last row and column numbers here

    ' Error-handling is here in case there is not any
    ' data in the worksheet

    On Error Resume Next

    With ws

    ' Find the last real row

    LastRow = Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByRows).Row
    MsgBox "" & LastRow
    ' Find the last real column

    LastCol = Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByColumns).Column
    MsgBox "" & LastCol
    End With

    'Get a reference to the source data table
    With ThisWorkbook.Worksheets("Reporting")
    Set rngSource = .Range(.Cells(1, 1), .Cells(LastRow, LastCol))
    End With
    With MyPivot

    'Update the Source data of the PT
    .SourceData = "Reporting!" & rngSource.Address(ReferenceStyle:=xlR1C1)

    'Refresh the dat in the PT
    .RefreshTable

    End With

    'Clear Object Variables
    Set rngSource = Nothing
    Set MyPivot = Nothing
    End Sub

    Code above executes but

    the table doesn't update

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    Hold on that may be it,

    refresh button was not pressed,

    Ill get back to you

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Apr 2006
    Posts
    22

    Re: Problem with VB Macro in Excel for Pivot Table

    That works , awesome help,

    can I leave feedback in this forum

  20. #20
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Problem with VB Macro in Excel for Pivot Table

    Just glad I could help.
    Don't forget to mark this thread as resolved (its under Thread Tools at the top of the page)
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width