Macro for create pivot table error -- PivotTable field name is not valid

miva0601

New Member
Joined
Mar 31, 2015
Messages
33
Can someone please help me debug the code in red? I was following Bill Jelen's Youtube tutorial episode 1211 on how to fix the hard coded parts of the macro -- the fact that pivot table went to new work sheet and that new sheet is called ".." -- I need to fix this hard coded part, as well as need Data Sheet as a variable.

Please help! I use the record macro function, and only need these two variables fixed. Thank you!


COPY / PASTED directly from <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym>:

Sub CreatePivot()
'
' CreatePivot Macro
'
' Keyboard Shortcut: Ctrl+r
'
DataSheet = ActiveSheet.Name
Sheets.Add
NewSheet = ActiveSheet.Name

Range("A4").Select
ActiveCell.FormulaR1C1 = "=TRIM(R[-1]C)"
Range("A4").Select
Selection.AutoFill Destination:=Range("A4:Z4"), Type:=xlFillDefault
Range("A4:Z4").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Rows("1:3").Select
Range("A3").Activate
Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
Cells.Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
DataSheet & "!R1C1:R1048576C26", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=NewSheet & "!R3C1", TableName:="PivotTable3", DefaultVersion _
:=xlPivotTableVersion14
Sheets(NewSheet).Select
Cells(3, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Order/ship reference")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Item")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Received")
.Orientation = xlRowField
.Position = 3
End With
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I think that should work ok as is - I trialled it and it is fine. The "DataSheet" you run the macro on - are you sure Row 1 has unique headings from A to Z to use for the PivotTable? What is the error message?
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,558
Latest member
aivin

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