+ Reply to Thread
Results 1 to 23 of 23

Pivot Table Code Modifications

Hybrid View

dwhite30518 Pivot Table Code Modifications 05-13-2012, 09:18 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 08:33 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 08:41 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 08:48 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 08:53 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 09:00 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 09:09 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 09:16 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 09:27 AM
JosephP Re: Pivot Table Code... 05-14-2012, 09:26 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 09:37 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 09:43 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 09:39 AM
JosephP Re: Pivot Table Code... 05-14-2012, 09:43 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 09:51 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 10:03 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 09:55 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 10:12 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 10:20 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 10:26 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 10:31 AM
shekhar1660 Re: Pivot Table Code... 05-14-2012, 10:36 AM
dwhite30518 Re: Pivot Table Code... 05-14-2012, 10:43 AM
  1. #1
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Pivot Table Code Modifications

    I have the following pivot table code. I want to be able to change it from it running on all sheets int he workbook to run on just the worksheet I am working with. The Code I currently have is...

    Sub CreateAPivotTable()

    Dim shtSource As Worksheet
    Dim rngSource As Range, rngDest As Range
    Dim pvt As PivotTable

    On Error GoTo ErrHandler

    'this prevents the screen from updating while the macro is running and
    'will make the code run faster
    Application.ScreenUpdating = False


    For Each shtSource In ActiveWorkbook.Worksheets

    If shtSource.Name <> "Summary" Then

    'Rather than have the pivot table use all rows in column A-N
    'just use what has actually been used.
    Set rngSource = shtSource.Range("A1").CurrentRegion

    'This is where the pivot table will be placed
    Set rngDest = shtSource.Range("E1")

    'This creates a pivot table. So rather than having to refer to PivotTables("PivotTable14") like before you can just refer to pvt
    Set pvt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSource, _
    Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=rngDest, DefaultVersion:=xlPivotTableVersion12)

    pvt.AddDataField pvt.PivotFields("Item"), "Count of Serial Rcvd", xlCount


    With pvt.PivotFields("Item")
    .Orientation = xlRowField
    .Position = 1
    End With

    'Formatting
    pvt.TableStyle2 = "PivotStyleDark7"
    With shtSource.Cells.Font
    .Name = "Calibri"
    .Size = 8
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
    End With

    ActiveWorkbook.ShowPivotTableFieldList = False


    End If


    Next shtSource

    'Turns screen updating back on - this line is critical otherwise
    'it will be turned off after the macro has finished.
    Application.ScreenUpdating = True

    Exit Sub

    'Simple error handler in case something goes wrong
    ErrHandler:
    Application.ScreenUpdating = True
    MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"


    End Sub

    What do I need to do in order to create a shortcut keys to run the code? How do I also change the code so that it only works on the current worksheet instead of creating pivot tables on all worksheets?

    Thanks!!!!

  2. #2
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Hi,

    For assigning Shortcut key:
    If you are using Excel 2003 then

    Go to Tools (Menu bar) > Macro > Macros...> Highlight the macro (macro name) > Click on Options > now you will be able to assign the shortcut key.


    As per my observation, this macro is creating pivot only when there is no worksheet available by the name of Summary.
    Please click on the * if resolution works for you

  3. #3
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    I am using excel 2010.

    This current code loops to include every worksheet within the workbook except the sheet labeled Summary. How do I change the code to only run on the worksheet I am currently working with. I do not want it to create pivots for every worksheet every time I run this macro. If I need to completely re-write the code, please point me in the right direction...I am mostly new with this and am learning as I go.

  4. #4
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Hi,
    Try this, I have updated your code:

    Sub CreateAPivotTable()
    
    Dim shtSource As Worksheet
    Dim rngSource As Range, rngDest As Range
    Dim pvt As PivotTable
    
    On Error GoTo ErrHandler
    
    'this prevents the screen from updating while the macro is running and
    'will make the code run faster
    Application.ScreenUpdating = False
    
    
    Activesheet.select
    
    'Rather than have the pivot table use all rows in column A-N
    'just use what has actually been used.
    Set rngSource = shtSource.Range("A1").CurrentRegion
    
    'This is where the pivot table will be placed
    Set rngDest = shtSource.Range("E1")
    
    'This creates a pivot table. So rather than having to refer to PivotTables("PivotTable14") like before you can just refer to pvt
    Set pvt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSource, _
    Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=rngDest, DefaultVersion:=xlPivotTableVersion12)
    
    pvt.AddDataField pvt.PivotFields("Item"), "Count of Serial Rcvd", xlCount
    
    
    With pvt.PivotFields("Item")
    .Orientation = xlRowField
    .Position = 1
    End With
    
    'Formatting
    pvt.TableStyle2 = "PivotStyleDark7"
    With shtSource.Cells.Font
    .Name = "Calibri"
    .Size = 8
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontMinor
    End With
    
    ActiveWorkbook.ShowPivotTableFieldList = False
    
    
    
    
    'Simple error handler in case something goes wrong
    ErrHandler:
    Application.ScreenUpdating = True
    MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
    
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    When I use this code i get the following error:

    "An error occurred: Object variable or With block variable not set"

    Am I missing something...it looks like this code has those parameters set?

  6. #6
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Are you running on Blank workbook or the workbook you have data?

  7. #7
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    I want to run it on a workbook with data and have the pivot table created on the same worksheet.

  8. #8
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Can you share the excel file?

  9. #9
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    Here is a sample of what I am working with.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Pivot Table Code Modifications

    corrected code
    Sub CreateAPivotTable()
    
        Dim shtSource        As Worksheet
        Dim rngSource        As Range
        Dim rngDest          As Range
        Dim pvt              As PivotTable
    
        On Error GoTo ErrHandler
    
        'this prevents the screen from updating while the macro is running and
        'will make the code run faster
        Application.ScreenUpdating = False
    
    
        Set shtSource = ActiveSheet
    
        If shtSource.Name <> "Summary" Then
    
            'Rather than have the pivot table use all rows in column A-N
            'just use what has actually been used.
            Set rngSource = shtSource.Range("A1").CurrentRegion
    
            'This is where the pivot table will be placed
            Set rngDest = shtSource.Range("E1")
    
            'This creates a pivot table. So rather than having to refer to PivotTables("PivotTable14") like before you can just refer to pvt
            Set pvt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSource, _
                                    Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=rngDest, _
                                    DefaultVersion:=xlPivotTableVersion12)
    
            pvt.AddDataField pvt.PivotFields("Item"), "Count of Serial Rcvd", xlCount
    
    
            With pvt.PivotFields("Item")
                .Orientation = xlRowField
                .Position = 1
            End With
    
            'Formatting
            pvt.TableStyle2 = "PivotStyleDark7"
            With shtSource.Cells.Font
                .Name = "Calibri"
                .Size = 8
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .ThemeFont = xlThemeFontMinor
            End With
    
            ActiveWorkbook.ShowPivotTableFieldList = False
    
    
        End If
    
        Application.ScreenUpdating = True
    
        Exit Sub
    
        'Simple error handler in case something goes wrong
    ErrHandler:
        Application.ScreenUpdating = True
        MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
    
    
    End Sub
    to assign a shortcut, press alt+f8, select macro in list and click the Options... button
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  11. #11
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Hey Joseph,

    Thanks so much for updating the code, however I think you missed something.
    I have updated that too

    Sub CreateAPivotTable()
    
        Dim shtSource        As Worksheet
        Dim rngSource        As Range
        Dim rngDest          As Range
        Dim pvt              As PivotTable
    
        On Error GoTo ErrHandler
    
        'this prevents the screen from updating while the macro is running and
        'will make the code run faster
        Application.ScreenUpdating = False
    
    
        Set shtSource = ActiveSheet
    
        If shtSource.Name <> "Summary" Then
    
            'Rather than have the pivot table use all rows in column A-N
            'just use what has actually been used.
            Set rngSource = shtSource.Range("A1").CurrentRegion
    
            'This is where the pivot table will be placed
            Set rngDest = shtSource.Range("E1")
    
            'This creates a pivot table. So rather than having to refer to PivotTables("PivotTable14") like before you can just refer to pvt
            Set pvt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSource, _
                                    Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=rngDest, _
                                    DefaultVersion:=xlPivotTableVersion12)
    
            pvt.AddDataField pvt.PivotFields("Item"), "Count of Serial Rcvd", xlCount
    
    
            With pvt.PivotFields("Item")
                .Orientation = xlRowField
                .Position = 1
            End With
    
            'Formatting
            pvt.TableStyle2 = "PivotStyleDark7"
            With shtSource.Cells.Font
                .Name = "Calibri"
                .Size = 8
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .ThemeFont = xlThemeFontMinor
            End With
    
            ActiveWorkbook.ShowPivotTableFieldList = False
        Else
        MsgBox ("Please remove existing sheet with the name of Summary")
    
        End If
    
        Application.ScreenUpdating = True
    
        Exit Sub
    
        'Simple error handler in case something goes wrong
    ErrHandler:
        Application.ScreenUpdating = True
        MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
    
    
    End Sub
    Updation is
    ' If shtSource.Name <> "Summary" Then
    Else
        MsgBox ("Please remove existing sheet with the name of Summary")

  12. #12
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    Actually...one more thing....if I wanted this pivot table to create an additional column for the count of serial numbers shipped...how should the code look for that?

  13. #13
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    Shekhar1660-Thanks! That works perfectly!!!!

    Thanks to all!!!!

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Pivot Table Code Modifications

    not sure why you need to remove a sheet called Summary?

  15. #15
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Hi,

    Just add
    pvt.AddDataField pvt.PivotFields("Item"), "Count of Serial Shipped", xlCount
    Below

    pvt.AddDataField pvt.PivotFields("Item"), "Count of Serial Rcvd", xlCount

  16. #16
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    I used this code but both columns contain the same data from the serial rcvd colomun...ideas???

  17. #17
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    I added that code and ran the macro but when the chart gets created, the rcvd and shipped columns are the same numbers even though there are some blanks cells within the records. Ideas?

  18. #18
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    My fault

    Kindly replace both the lines with
    pvt.AddDataField pvt.PivotFields("Serial Rcvd"), "Count of Serial Rcvd", xlCount
            pvt.AddDataField pvt.PivotFields("Serial Shipped"), "Count of Serial Shipped", xlCount

    If resolved please click on the * at the left side.

  19. #19
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    When I run this code, the two columns have the same data...the numbers do not reflect was is actually populated in the cells.

  20. #20
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Hi,

    I am getting the correct results while running the code.

    Please follow the instructions:
    Remove all of your existing modules
    create a new module and update it with:

    Sub CreateAPivotTable()
    
        Dim shtSource        As Worksheet
        Dim rngSource        As Range
        Dim rngDest          As Range
        Dim pvt              As PivotTable
    
        On Error GoTo ErrHandler
    
        'this prevents the screen from updating while the macro is running and
        'will make the code run faster
        Application.ScreenUpdating = False
    
    
        Set shtSource = ActiveSheet
    
        If shtSource.Name <> "Summary" Then
    
            'Rather than have the pivot table use all rows in column A-N
            'just use what has actually been used.
            Set rngSource = shtSource.Range("A1").CurrentRegion
    
            'This is where the pivot table will be placed
            Set rngDest = shtSource.Range("E1")
    
            'This creates a pivot table. So rather than having to refer to PivotTables("PivotTable14") like before you can just refer to pvt
            Set pvt = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngSource, _
                                    Version:=xlPivotTableVersion12).CreatePivotTable(TableDestination:=rngDest, _
                                    DefaultVersion:=xlPivotTableVersion12)
    
            pvt.AddDataField pvt.PivotFields("Serial Rcvd"), "Count of Serial Rcvd", xlCount
            pvt.AddDataField pvt.PivotFields("Serial Shipped"), "Count of Serial Shipped", xlCount
    
            With pvt.PivotFields("Item")
                .Orientation = xlRowField
                .Position = 1
            End With
    
            'Formatting
            pvt.TableStyle2 = "PivotStyleDark7"
            With shtSource.Cells.Font
                .Name = "Calibri"
                .Size = 8
                .Strikethrough = False
                .Superscript = False
                .Subscript = False
                .OutlineFont = False
                .Shadow = False
                .Underline = xlUnderlineStyleNone
                .ThemeColor = xlThemeColorLight1
                .TintAndShade = 0
                .ThemeFont = xlThemeFontMinor
            End With
    
            ActiveWorkbook.ShowPivotTableFieldList = False
        Else
        MsgBox ("Please remove existing sheet with the name of Summary")
    
        End If
    
        Application.ScreenUpdating = True
    
        Exit Sub
    
        'Simple error handler in case something goes wrong
    ErrHandler:
        Application.ScreenUpdating = True
        MsgBox "An error occurred: " & Err.Description, vbExclamation, "Error"
    
    
    End Sub

  21. #21
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    That works!! Thanks!!!

  22. #22
    Registered User
    Join Date
    05-07-2012
    Location
    Delhi
    MS-Off Ver
    Excel 2003 and Excel 2007
    Posts
    62

    Re: Pivot Table Code Modifications

    Please mark this post as Resolved

  23. #23
    Forum Contributor
    Join Date
    05-13-2012
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2010
    Posts
    378

    Re: Pivot Table Code Modifications

    That works!! Thanks!!!

+ 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