Error when pivot table is empty

I have the following code in an excel spreadsheet.

Sub RefreshReport(control As IRibbonControl)
    Worksheets("Order Report").Activate
    ActiveSheet.PivotTables("Order Report").PivotSelect "'Item Description'[All]", _
        xlLabelOnly, True
    ActiveSheet.PivotTables("Order Report").PivotCache.Refresh
End Sub

Open in new window


The problem is, when this spreadsheet is first used, likely the pivot report would be empty.  This means when I run this code, I get an error stating, "Run-time error '1004':  The formula is not complete.  Make sure and ending square bracket ] is not missing."  I know this error is related to an empty pivot table because anytime I test it where the pivot report would show at least one line item, the error never appears.  On debug, lines 3 and 4 are highlighted.

Could anyone recommend some type of if/then statement that could check to see if the report would be empty and if YES, then maybe throw a message stating, "Report Empty"?

Please let me know if you need more information to help me solve this issue.  If it helps, the conditions for there to be a report is if in the tab called "Main" then Column Q from row 2 to 9999 must have a numerical value in it and cannot be blank.

I will apologize up front if I'm not using correct code or excel language.  I'm not very good with this stuff.
rmc71291Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use the following code...

Sub RefreshReport(control As IRibbonControl)

    Dim rng As Range
    Set rng = Sheets("Main").Range("Q2:Q9999")
    If Application.WorksheetFunction.Count(rng) > 0 Then
        Worksheets("Order Report").Activate
        ActiveSheet.PivotTables("Order Report").PivotSelect "'Item Description'[All]", _
                                                            xlLabelOnly, True
        ActiveSheet.PivotTables("Order Report").PivotCache.Refresh
    End If

End Sub

Open in new window


Saurabh...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rmc71291Author Commented:
Thanks much. Worked perfectly.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.