Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Issue with a Excel Pivot Table

Posted on 2013-01-30
4
Medium Priority
?
255 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

916 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