Macro to produce Pivot based on a Data set

ldashev

New Member
Joined
Mar 24, 2016
Messages
49
Hi,

Could someone help design a macro to design a pivot based on this piece of data:

The two columns that I need in the pivot are company and amount (but need to adjust number format field to $ sign) and introduce a new field that would reflect the ratio each company represents from gross amount. I will post what I'd like the pivot to look like in the next post.

DateIDREf #AmountCompany
2/27/2017111201701100IBM
2/27/2017222201702100HP
2/27/2017333201703500IBM
2/27/2017444201704200Ford
2/27/2017555201705300IBM
2/27/2017666201706600IBM
2/27/2017777201707200HP

<tbody>
</tbody>
 
I should have dim gt as double instead of long
This should run without errors.
Code:
Sub pivot()
Dim lr As Long
Dim gt As Double
Dim lrg As Long
lr = Sheets("Beneficiary").Cells(Rows.Count, 1).End(xlUp).Row
Application.DisplayAlerts = False
Dim worksha As Integer
worksha = Application.Sheets.Count
For x = 1 To worksha
If Worksheets(x).Name = "BenePivot" Then
Sheets("BenePivot").Delete
Exit For
End If
Next x
Sheets.Add.Name = "BenePivot"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Beneficiary!R1C1:R" & lr & "C5", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="BenePivot!R3C1", TableName:="PivotTable5", DefaultVersion _
:=xlPivotTableVersion14
Sheets("BenePivot").Select
Cells(2, 1).Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("Secondary Orig")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
"PivotTable5").PivotFields("Trxn Amount"), "Sum of Amount", xlSum
Range("B5").Select
With ActiveSheet.PivotTables("PivotTable5").PivotFields("Sum of Amount")
.NumberFormat = "$#,##0.00"
End With


ActiveSheet.PivotTables("PivotTable5").PivotFields("Secondary Orig").AutoSort _
xlAsending, "Sum of Amount", ActiveSheet.PivotTables("PivotTable5").PivotColumnAxis. _
PivotLines(1), 1
lrg = Sheets("Date").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("BenePivot").Activate
lrp = Sheets("BenePivot").Cells(Rows.Count, 1).End(xlUp).Row
gt = Application.WorksheetFunction.Sum(Sheets("Date").Range("d2:d" & lrg))
For i = 4 To lrp - 1
Cells(i, 3) = Cells(i, 2) / gt
Cells(i, 3).NumberFormat = "0.00%"
Next i


End Sub
 
Upvote 0

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Change xlAsending to xlDescending in the line
Code:
[COLOR=#333333]ActiveSheet.PivotTables("PivotTable5").PivotFields("Secondary Orig").AutoSort _
[/COLOR][COLOR=#ff0000]xlAsending[/COLOR][COLOR=#333333], "Sum of Amount", ActiveSheet.PivotTables("PivotTable5").PivotColumnAxis. _[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,215,878
Messages
6,127,506
Members
449,385
Latest member
KMGLarson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top