Avatar of rmc71291
rmc71291
 asked on

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.
Microsoft ExcelVisual Basic Classic

Avatar of undefined
Last Comment
rmc71291

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Saurabh Singh Teotia

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
rmc71291

ASKER
Thanks much. Worked perfectly.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck