+ Reply to Thread
Results 1 to 4 of 4

Pivot Table Script Change

Hybrid View

  1. #1
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Pivot Table Script Change

    I have this pivot table script that was working fine in another workbook.

    But then I needed it to run only on the last sheet of another workbook. So I took out "For each ws in worksheets" now I am getting an error "object variable or with block not set"

    Not sure what happend?

    Any Idea's?

    Here is the code, Thanks for helping, Mike

    Sub Pivot2()
    
        Dim Ws As Worksheet, strSource As String
        Dim PT As PivotTable, pc As PivotCache, pf As PivotField
        
         With ActiveWorkbook.Worksheets(Sheets.Count)
           
            
                'For Each Ws In ActiveWorkbook.Worksheets
             ActiveWorkbook.ShowPivotTableFieldList = False
             Application.CommandBars("PivotTable").Visible = False
             Application.DisplayAlerts = False
             Application.ScreenUpdating = False
    
    
            strSource = Ws.Name & "!R1C1:R5000C4"
            ActiveWorkbook.Worksheets.Add      'new worksheet becomes the active sheet
            'ActiveSheet.Name = "Info"
            
            Set pc = ActiveWorkbook.PivotCaches.Add(xlDatabase, strSource)
            Set PT = pc.CreatePivotTable(Range("A1"))   'pivot table at A1 on new sheet
            
            Set pf = PT.PivotFields("Order Number")
    
            PT.AddDataField pf, "Count of Order Number", xlCount
            PT.AddFields RowFields:="Name"
            Application.DisplayAlerts = True
            Application.ScreenUpdating = True
                    'Next Ws
        
         End With
              
    End Sub
    Last edited by realniceguy5000; 12-18-2008 at 12:05 PM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Try this,

    Dim Ws As Worksheet, strSource As String
    Dim PT As PivotTable, pc As PivotCache, pf As PivotField
    
    Set Ws = ActiveWorkbook.Worksheets(Sheets.Count)
    With Ws
        ActiveWorkbook.ShowPivotTableFieldList = False
        With Application
            .CommandBars("PivotTable").Visible = False
            .DisplayAlerts = False
            .ScreenUpdating = False
    
            strSource = Ws.Name & "!R1C1:R5000C4"
            ActiveWorkbook.Worksheets.Add      'new worksheet becomes the active sheet
            'ActiveSheet.Name = "Info"
    
            Set pc = ActiveWorkbook.PivotCaches.Add(xlDatabase, strSource)
            Set PT = pc.CreatePivotTable(Range("A1"))   'pivot table at A1 on new sheet
    
            Set pf = PT.PivotFields("Order Number")
    
            PT.AddDataField pf, "Count of Order Number", xlCount
            PT.AddFields RowFields:="Name"
            
            .DisplayAlerts = True
            .ScreenUpdating = True
        End With
    End With
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951
    Seems so simple I wonder sometimes how I overlook such a thing...

    Thanks again Roy...

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    No problem

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1