pivot table creation using

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Pivot table creation using VBA

Pivot table creation using VBA

resolvedResolved · Medium Priority · Version 2007

William has attended:
Excel Intermediate course
Excel VBA Intro Intermediate course

Pivot table creation using VBA

Hi there,

I am trying to automate a defect tracking system using VBA and what I have is a table of data listing defect found in a testing period.

From this data I look for the halfway date and then insert a blank row and populate the same headings from Row 1 of the spreadsheet.

I then recorded a macro to get the basis of the VBA code I need.

I then created a function that has 2 data input (The row number where I inserted the new row and the last row used)

In the function I set the last row of first pivot table data then create a For next loop that creates the first pivot table and then attempts to create the second one.

When the script tried to create the 2nd pivot table I get the following error message:-

Run-time error '1004:

The Pivot Table field name is not valid. To create a Pivot Table report, you must use data that is organized as a list with labelded column. If you are changing the name of a Pivot Table field, you must type a new name for the field.

======= Function code below ===========
Function CreatePivot(ByVal NewRow, ByVal LastRow)

'Set the row details for the first pivot table and the second pivot table

'Last row for the first pivot table
firstrow = NewRow - 1

For Table = 1 To 2

Sheets("Defects").Select

If Table = 1 Then

Range("A1:J" & firstrow).Select

Else

Range("A" & NewRow & ":J" & LastRow).Select

End If




If Table = 1 Then

Range("A1:J" & firstrow).Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Defects!R1C1:R" & firstrow & "C10", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="First_Half!R3C1", TableName:="First1", DefaultVersion _
:=xlPivotTableVersion10
Sheets("First_Half").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("First1").PivotFields("BG_SEVERITY")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("First1").PivotFields("BG_DETECTION_DATE")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("First1").AddDataField ActiveSheet.PivotTables( _
"First1").PivotFields("TOTAL_PER_DAY"), "Sum of TOTAL_PER_DAY", xlSum

ActiveWorkbook.ShowPivotTableFieldList = False

Else
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Defects!R" & NewRow & "C1:R" & LastRow & ":C10", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="Second_Half!R3C1", TableName:="Second2", DefaultVersion _
:=xlPivotTableVersion10
Sheets("Second_Half").Select
Cells(3, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("Second1").PivotFields("BG_SEVERITY_2")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("Second1").PivotFields("BG_DETECTION_DATE_2")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("Second1").AddDataField ActiveSheet.PivotTables( _
"Second1").PivotFields("TOTAL_PER_DAY_2"), "Sum of TOTAL_PER_DAY", xlSum
ActiveWorkbook.ShowPivotTableFieldList = False
End If

Next

End Function
=======================================

RE: Pivot table creation using VBA

Hi William

Thanks for getting in touch. That error sounds like one of the headings on the table doesn't have an entry, but it's difficult to diagnose going in blind.

Can you email the file to me to have a look? All data will be treated in the strictest confidence.

gary@stl-training.co.uk

When I've investigated I'll post an answer back on this forum.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Pivot table creation using VBA

Gary,

I feel so ashamed.

It was magic fingers.....

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Defects!R" & NewRow & "C1:R" & LastRow & ":C10" is wrong.

It should be:-

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Defects!R" & NewRow & "C1:R" & LastRow & "C10"

Regards

William

RE: Pivot table creation using VBA

Hi William

Good to hear from you. I was just replying with the same! Well done on spotting it.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

Thu 6 Dec 2012: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Creating charts in Excel 2010

Here's how to present your data in a chart format:

Highlight the data you wish to use in a chart

Click the Insert Ribbon

In the Charts Group, select Column Chart

Your chart will then appear on your work sheet.

View all Excel hints and tips


Server loaded in 0.08 secs.