Macro's errors

Oakey

New Member
Joined
Jan 9, 2017
Messages
49
Office Version
  1. 365
Is it possible to create a Macro that will sort some data, then used this sorted Data to create a Pivot table, then using the results of the pivot table, pasting the results as values then creating another pivot table? All this is done on the same sheet.

I have tried to create this macro and run I but I get an error message straight after sorting the data, before the pivot table is run

Error message is

Run time error 5
invalid procedure call or argument

Macro is below (Text in bold is where the problem starts)

Sub MD()
'
' MD Macro
'

'
Range("A:A,B:B,D:G,I:I").Select
Range("I1").Activate
Selection.Delete Shift:=xlToLeft
Columns("A:D").Select
ActiveWorkbook.Worksheets("MD Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("MD Data").Sort.SortFields.Add2 Key:=Range( _
"B2:B4094"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("MD Data").Sort
.SetRange Range("A1:D4094")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"MD Data!R1C1:R1048576C4", Version:=6).CreatePivotTable TableDestination:= _
"MD Data!R1C6", TableName:="PivotTable1", DefaultVersion:=6

Sheets("MD Data").Select
Cells(1, 6).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Order number")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Amount"), "Count of Amount", xlCount
Range("H1").Select
ActiveCell.FormulaR1C1 = "Name"
Range("H2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-2],C[-7]:C[-4],4,FALSE)"
Range("H2").Select
Selection.AutoFill Destination:=Range("H2:H1278")
Range("H2:H1278").Select
Columns("F:H").Select
Selection.Copy
Range("J1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"MD Data!R1C10:R1048576C12", Version:=6).CreatePivotTable TableDestination _
:="MD Data!R1C14", TableName:="PivotTable2", DefaultVersion:=6
Sheets("MD Data").Select
Cells(1, 14).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Name")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Count of Amount"), "Sum of Count of Amount", xlSum
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Row Labels"), "Count of Row Labels", xlCount
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
What's the error message?

Also, why sort before creating the pivot?

You should be able to sort the pivot itself.
 
Upvote 0
Error message is

Run time error 5
invalid procedure call or argument

Not sure what the error means.

Before i was sorting before the pivot table as i only wanted certain information, but now thinking about it i may not have to sort.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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