Solved

Issue with a Excel Pivot Table

Posted on 2013-01-30
4
236 Views
Last Modified: 2013-02-01
Hi Experts,

I have issue with attached report I like to create a pivot table for MIS porpose.

the issue I want to make fliter the product type using 2010 slicer.


Note a customer may have more than one product.
28013726-new.xlsx
0
Comment
Question by:obad62
  • 3
4 Comments
 
LVL 26

Expert Comment

by:redmondb
ID: 38835478
Hi, obad62.

That sheet is not suitable for a pivot table that includes Product Type. So I have written a macro that takes that data, reformats it into a new sheet and builds a quick pivot from that.

Before I post it here, I need to check that you're happy that the "Account Outstanding Balance" will not be include in the new sheet (and consequently not in the pivot). This is because, if it was included then it would effectively be multiplied by the number of Product Types.

Thanks,
Brian.
0
 

Author Comment

by:obad62
ID: 38838702
It ok.
0
 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
ID: 38839464
Thanks, obad62. Please see the code below.

I was concerned in case your original file contained some live data, so I created a test file and Netminder (one of the Mod's) kindly replaced your old version with it. Was there live data in the file?

Regards,
Brian.
Option Explicit

Sub Reformat_Data()
Dim xLast_Row As Long
Dim xLast_Col As Long
Dim xSrce As Worksheet
Dim xDest As Worksheet
Dim xRow As Long
Dim i As Long

Set xSrce = Sheets("Page1_1")

If ActiveSheet.UsedRange.Cells(1, 1).Address <> "$A$1" Then
    MsgBox ("No data in Column A and/or Row 1 - run cancelled.")
    Exit Sub
End If
xLast_Col = xSrce.UsedRange.Columns.Count
xLast_Row = xSrce.UsedRange.Rows.Count

If xLast_Row < 3 Or xLast_Col <> 43 Then
    MsgBox ("Invalid no. of rows (" & xLast_Row & ") or columns (" & xLast_Col & ") in """ & xSrce.Name & """ - run cancelled.")
    Exit Sub
End If

For i = 0 To 12
    If i = 0 Then
        Set xDest = Sheets.Add
        xSrce.Range("A2:G" & xLast_Row - 1).Copy Destination:=xDest.Range("A1")
        xDest.Range("H1") = "Product Type"
        xDest.Range("H2:H" & xLast_Row - 2) = xSrce.Range("E1").Value
    Else
        xRow = 1 + xDest.Range("A1").SpecialCells(xlLastCell).Row
        xSrce.Range("A3:D" & xLast_Row - 1).Copy Destination:=xDest.Range("A" & xRow)
        xSrce.Range("A3:C" & xLast_Row - 1).Offset(, 4 + i * 3).Copy Destination:=xDest.Range("E" & xRow)
        xDest.Range("H" & xRow & ":H" & xRow + xLast_Row - 4) = xSrce.Range("A1").Offset(, 1 + i * 3).Value
    End If
Next

xDest.Columns("D:D").Delete Shift:=xlToLeft ' ... dropped from the new sheet as it would effectively be multiplied by the number of Product Types.

xDest.Range("A1:H1").Columns.AutoFit
Range("A2").Select
ActiveWindow.FreezePanes = True

Call Build_Pivot(xDest)
    
End Sub

Sub Build_Pivot(xInput As Worksheet)
Dim xPivot    As Worksheet
Dim xLast_Row As Long

xLast_Row = xInput.UsedRange.Rows.Count
    
Set xPivot = Sheets.Add

xInput.Parent.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        xInput.Name & "!R1C1:R" & xLast_Row & "C7", Version:=xlPivotTableVersion14).CreatePivotTable _
        TableDestination:=xPivot.Name & "!R3C1", TableName:="PivotTable1", DefaultVersion _
        :=xlPivotTableVersion14

With xPivot.PivotTables("PivotTable1").PivotFields("Product Type")
    .Orientation = xlRowField
    .Position = 1
End With

xPivot.PivotTables("PivotTable1").AddDataField xPivot.PivotTables( _
        "PivotTable1").PivotFields("Deal Outstanding Balance"), _
        "Deal O/S Balance", xlSum
        
End Sub

Open in new window

0
 
LVL 26

Expert Comment

by:redmondb
ID: 38844799
Thanks, obad62.

(Was there live data in the original file?  If so, please don't do this again - information posted here is public.)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you how to use shortcut menus in the Access run-time environment.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

920 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

14 Experts available now in Live!

Get 1:1 Help Now