How validate if pivot table data exists or not?

sourabh_ajmera

New Member
Joined
Jul 17, 2014
Messages
36
I want to find out if a particular field, say a month from Jan-14 to Dec-14, exists in 'pivot field list' before adding it to the pivot table?

can you please help me out on this as I have tried couple of things but doesnt work.

Here is my code:
Code:
   Sub Test()
 
    If (FieldItemExists("Feb - 14") = True) Then
    ActiveSheet.PivotTables("usdPivotTable").AddDataField ActiveSheet.PivotTables( _
        "usdPivotTable").PivotFields("Feb-14"), "Sum of Feb-14", xlSum
    End If
    
End Sub


Function FieldItemExists(strName As String) As Boolean
  
  Dim strTemp As String
  On Error Resume Next
  strTemp = ActiveSheet.PivotTables("usdPivotTable").PivotFields(strName)
  If Err = 0 Then
    FieldItemExists = True
  Else
    FieldItemExists = False
    End If
End Function


This always returns false. Ain't able to figure out another way. Please Help

Thanks in advance!!
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
My bad....found out the silly mistake I had been doing...Code works perfectly now

How did I not see it...was wrecking my head on it...thinking it would have been some big mistake in the code.

Anyways...If (FieldItemExists("Feb - 14") = True) Then <---- this has space in between instead of "Feb-14".


Any other easier method would also be appreciated. Thanks!


 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

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