Code error while creating dynamic pivot table

bunty

New Member
Joined
Feb 18, 2011
Messages
20
Hi,

Please help me to create a dynamic pivot table.While running this code I am getting error "One of your data columns has a blank heading".But all the column has headings.Please find the below code with sample set of data.

Code:
Sub AdjustPivotDataRange()Dim Data_sht As Worksheet
Dim Pivot_sht As Worksheet
Dim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String


  Set Data_sht = ThisWorkbook.Worksheets("Sheet1")
  Set Pivot_sht = ThisWorkbook.Worksheets("Sheet2")




  PivotName = "PivotTable1"


  Set StartPoint = Data_sht.Range("A1")
  Set DataRange = Data_sht.Range(StartPoint, StartPoint.SpecialCells(xlLastCell))
  
  NewRange = Data_sht.Name & "!" & _
    DataRange.Address(ReferenceStyle:=xlR1C1)


  If WorksheetFunction.CountBlank(DataRange.Rows(1)) > 0 Then
    MsgBox "One of your data columns has a blank heading." & vbNewLine _
      & "Please fix and re-run!.", vbCritical, "Column Heading Missing!"
    Exit Sub
  End If


  Pivot_sht.PivotTables(PivotName).ChangePivotCache _
    ThisWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=NewRange)


  Pivot_sht.PivotTables(PivotName).RefreshTable


  MsgBox PivotName & "'s data source range has been successfully updated!"


End Sub

Data:
SL#NameOpen DateClosed DateStatus
123Paul
21/11/2014

<tbody>
</tbody>
1/1/2015

<tbody>
</tbody>
Closed
435Sidd
20/01/2015

<tbody>
</tbody>
WIP
2rtRajiv
11/11/2014

<tbody>
</tbody>
14/01/2015

<tbody>
</tbody>
Closed
354Sidd
31/12/2014

<tbody>
</tbody>
WIP
657Paul
22/01/2015

<tbody>
</tbody>
WIP
786Maddy
13/09/2014

<tbody>
</tbody>
21/01/2015

<tbody>
</tbody>
Closed
536Sunny
22/01/2015

<tbody>
</tbody>
549Rohit
23/01/2015

<tbody>
</tbody>
864Anya
25/01/2015

<tbody>
</tbody>

<tbody>
</tbody>


Could you please help me to resolve this issue and how i can group the name in a group (example:In pivot chart name should replace with some group name like Ayna,Paul,Rajiv in one GroupA and Rohit.Sidd in GroupB and rest in GroupC).So in place of name team name should display in Pivot table.

Thanks you all and Kindly help to resolve this issue...:)
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
VBA won't be lying. Does this return what you would expect?

Code:
MsgBox Data_sht.Range(StartPoint, StartPoint.SpecialCells(xlLastCell))

Maybe you can use:

Code:
Set DataRange = StartPoint.CurrentRegion
 
Upvote 0
Hi Andrew,

Thanks for a quick response..

I tried that but its giving error "Application defined or object defined error".


Thanks...
 
Upvote 0
It's a run time error 1004 (Application-define or object defined error in below script
Code:
Pivot_sht.PivotTables(PivotName).ChangePivotCache _            ThisWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=NewRange)
 
Upvote 0
Hi Andrew,

This is the part of same code which I share and made a changes as you suggested.After this change while running the code I was getting this run time error.
Please find below the code:
Code:
Dim Pivot_sht As WorksheetDim StartPoint As Range
Dim DataRange As Range
Dim PivotName As String
Dim NewRange As String


  Set Data_sht = ThisWorkbook.Worksheets("Sheet1")
  Set Pivot_sht = ThisWorkbook.Worksheets("Sheet2")




  PivotName = "PivotTable1"


  Set StartPoint = Data_sht.Range("A1")
  'Set DataRange = Data_sht.Range(StartPoint, StartPoint.SpecialCells(xlLastCell))
  Set DataRange = StartPoint.CurrentRegion
  NewRange = Data_sht.Name & "!" & _
    DataRange.Address(ReferenceStyle:=xlR1C1)


  If WorksheetFunction.CountBlank(DataRange.Rows(1)) > 0 Then
    MsgBox "One of your data columns has a blank heading." & vbNewLine _
      & "Please fix and re-run!.", vbCritical, "Column Heading Missing!"
    Exit Sub
  End If


  Pivot_sht.PivotTables(PivotName).ChangePivotCache _
    ThisWorkbook.PivotCaches.Create( _
    SourceType:=xlDatabase, _
    SourceData:=NewRange)


  Pivot_sht.PivotTables(PivotName).RefreshTable


  MsgBox PivotName & "'s data source range has been successfully updated!"


End Sub



Once again thanks for your response and looking for your support..
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,054
Latest member
juliecooper255

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