|
-
Jun 18th, 2008, 07:36 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [Excel 2003] Pivot Table GetData problem
My Pivot Table contains a number of Rows which are Regions, and a number
of Columns which are Types.
At first I noticed that I had a problem if I was trying to get the value for a
Region or Type that was not included in the Pivot Table it crashed, so I came
up with the following to return 0 if the Row or COlumn heading was not
found, which works fine.
However I have now encountered a new problem, if the Row and Column
headings are there, but the spreadsheet halts with the following error:
Run-Time error '1004'
Application-defined or object-defined error.
when it tries to retrieve a value in the GetData method, if there is no value for
that Row and Column.
Any suggestions as to why and how to get around this.
ResSht is a Worksheet variable.
Code:
Function Pivot(ByVal RowField As String, ByVal ColField As String) As Long
Pivot = 0
If InPivotRow(RowField) And InPivotCol(ColField) Then
Pivot = ResSht.PivotTables(PivotTableName).GetData("'Count of Region' '" & _
RowField & "' '" & ColField & "'")
End If
End Function
Function InPivotRow(ByVal FieldName As String) As Boolean
' True if FieldName in Row Fields
Dim Count As Long
Dim PvtField As PivotField
InPivotRow = False
For Each PvtField In ResSht.PivotTables(PivotTableName).RowFields
For Count = 1 To PvtField.PivotItems.Count
If PvtField.PivotItems(Count).Name = FieldName Then
InPivotRow = True
Exit Function
End If
Next Count
Next PvtField
End Function
Function InPivotCol(ByVal FieldName As String) As Boolean
' True if FieldName in Col Fields
Dim Count As Long
Dim PvtField As PivotField
InPivotCol = False
For Each PvtField In ResSht.PivotTables(PivotTableName).ColumnFields
For Count = 1 To PvtField.PivotItems.Count
If PvtField.PivotItems(Count).Name = FieldName Then
InPivotCol = True
Exit Function
End If
Next Count
Next PvtField
End Function
Last edited by Torc; Jun 18th, 2008 at 07:40 AM.
Signature Under Construction 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|