You are here:

Excel/Creating PivotTable in VBA

Advertisement


Question
Private Sub CreatePivotGapWalk()   

Dim WSD As Worksheet
Dim WSDT As Worksheet
Dim PTCache As PivotCache
Dim pt As PivotTable
Dim Prange As Range
Dim i As Long
Dim FieldType As String
Dim pf As PivotField
Dim Pindx As Integer        ' Page index
Dim indx As Long
Dim Rindx As Integer        ' Row index
Dim Cindx As Integer        ' Column index
Dim Dindx As Integer        ' Data Index

Pindx = 1
Rindx = 1
Cindx = 1
Dindx = 1

objXLApp.ActiveWorkbook.Activate

   Dim shtSrc As Worksheet, shtDest As Worksheet
   Dim pc As PivotCache

   Set shtSrc = objXLBook.Worksheets("GapWalkRaw")
   
   Set shtDest = shtSrc.Parent.Sheets.Add()
   shtDest.NAME = "GapWalk_pvt"
     
' Delete any prior pivot tables
For Each pt In shtSrc.PivotTables
   pt.TableRange2.Clear
Next pt
For Each pt In shtDest.PivotTables
   pt.TableRange2.Clear
Next pt

  
Set pc = objXLBook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=shtSrc.Range("A1").CurrentRegion)
pc.CreatePivotTable TableDestination:=shtDest.Range("A10"), TableName:="PivotTable1" ' This line fails

'Code works fine on the first iteration fails on 2nd time. Any Ideas??

Answer
sp,

From what I see (and all I can see is your code) I would expect the code to error on the second run right here:

shtDest.NAME = "GapWalk_pvt"

on the second run, I would guess you are trying to create a duplicate sheet name since that sheet was created on the first run.  You would need to test the sheet name and if it is found, then give it the new sheet a different name.   

now you say it fails on the last line.  I would not see that as being a problem unless the object shtDest was undefined.  

I did test your code and it error'd where I said it would error.  When I modified the code so it didn't create a new sheet thus avoiding the error I predicted, then the code ran fine the second time.

--
regards,
Tom Ogilvy

About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2016 About.com. All rights reserved.