Error Handling for Pivot Tables in VBA

kensandy1

New Member
Joined
Jan 7, 2019
Messages
19
Hi All,

I'm not familiar enough with error handling in VBA, so I'm trying to figure out the best way to handle this. I did a little searching and found an On Error Goto that might work but I don't think that will be the best solution here.

I have 18 worksheets, each worksheet as a Pivot table. In the pivot table, there are 24 hours, one for each hour of the day (midnight - 11pm). I have a function that will scan through a all 18 pivot tables looking for a specific hour of the day and spit out how many calls were received for that hour. The problem I have is if there's no calls during that hour, it doesn't appear on my Pivot Table so it gives me an error when running the code.

Here's what I have:

Code:
Sub Analyze_Hour()

Dim time As Date
Dim pvformula_value As Integer
Dim test As String
Dim pT As PivotTable

time = Worksheets("Dashboard").Cells(3, "R").Value
pvformula_value = Round((time * 1440 / 60 + 1), 0)


Set pT = Sheets("Calls by Day MTD").PivotTables("PivotTable1")
test = pT.GetPivotData("[Measures].[Calls Offer incl Short Abn]", "[TimeHalfHour].[TimeHalfHour]", "[TimeHalfHour].[TimeHalfHour].[HOUR].&[3]&[" + pvformula_value + "]")


MsgBox (test)


End Sub

If the hour is in the pivot table for sheet 'Calls by Day MTD' for whatever hour is selected in Cell R3, it works. If the time isn't in the pivot table, I want it to return a 0.

Using pseudo code, I want:

Code:
test will equal pT.GetPivotData("[Measures].[Calls Offer incl Short Abn]", "[TimeHalfHour].[TimeHalfHour]", "[TimeHalfHour].[TimeHalfHour].[HOUR].&[3]&[" + pvformula_value + "]")

Or it will equal 0 if time pvformula_value doesn't exist in this pivot table

Is there an error function in VBA that I can use to accomplish this?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I guess I can use the GoTo in a function, I just would think there was something easier than writing a function specifically for this.
Code:
Function timeExists(value As Integer, pT As PivotTable) As Boolean


    On Error GoTo foo
    
    timeExists = False
    
    If pT.GetPivotData("[Measures].[Calls Offer incl Short Abn]", "[TimeHalfHour].[TimeHalfHour]", "[TimeHalfHour].[TimeHalfHour].[HOUR].&[3]&[" + CStr(value) + "]") Then
        timeExists = True
    End If
foo:
    
End Function
 
Upvote 0

Forum statistics

Threads
1,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

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