Solved

Issue with a Excel Pivot Table

Posted on 2013-01-30
4
235 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

746 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