Making pivot with macro recorder - understanding the basics

Sauermugg

New Member
Joined
Sep 15, 2014
Messages
5
Hi!

I just started using vba.

I`ve recorded a macro, that first make a pivot, and then do a calculation on the pivot result.

I understand the problem is the hard coding, especially when adding a New sheet etc. But how can I make this work; please explain the - I Guess this has alot of crap in it, because of the recorder. I need to understand the basics.

Code:
[COLOR=#333333] Sheets.Add[/COLOR]
<code style="margin: 0px; padding: 0px; font-style: inherit;">    ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _        "Ark 1!R1C1:R10301C54", Version:=xlPivotTableVersion14).CreatePivotTable _        TableDestination:="Ark1!R3C1", TableName:="Pivottabell1", DefaultVersion _        :=xlPivotTableVersion14    Sheets("Ark1").Select    Cells(3, 1).Select    With ActiveSheet.PivotTables("Pivottabell1").PivotFields("K_ID")        .Orientation = xlRowField        .Position = 1    End With    With ActiveSheet.PivotTables("Pivottabell1").PivotFields("K_NAVN")        .Orientation = xlRowField        .Position = 2    End With    With ActiveSheet.PivotTables("Pivottabell1").PivotFields("TARIFF")        .Orientation = xlRowField        .Position = 3    End With    ActiveSheet.PivotTables("Pivottabell1").AddDataField ActiveSheet.PivotTables( _        "Pivottabell1").PivotFields("VEKT"), "Summer av VEKT", xlSum    With ActiveSheet.PivotTables("Pivottabell1").PivotFields("VERDI")        .Orientation = xlRowField        .Position = 4    End With    ActiveSheet.PivotTables("Pivottabell1").RowAxisLayout xlTabularRow    ActiveSheet.PivotTables("Pivottabell1").RepeatAllLabels xlRepeatLabels    Range("F4").Select    ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-1]"    Range("F4").Select    Selection.AutoFill Destination:=Range("F4:F9679")    Range("F4:F9679").Select    Columns("F:F").Select    Selection.NumberFormat = "#,##0.00"    Range("F3").Select    ActiveCell.FormulaR1C1 = "Gj_snitt"    Range("F9").Select </code></pre>[COLOR=#333333]End Sub[/COLOR]
 

Some videos you may like

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Watch MrExcel Video

Forum statistics

Threads
1,122,418
Messages
5,596,028
Members
414,039
Latest member
southike

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
Top