How do I get code to continue if object is not present this time but could be next time??????

rkol297

Board Regular
Joined
Nov 12, 2010
Messages
131
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a macro that builds a Pivot Table however when it gets to the lines of code:

Code:
Sub BuildPivotsAOBstatsonly()
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
        .DisplayAlerts = False
        .EnableEvents = False
    End With       
'###PIVOT TABLE BUILDS###
Sheets("STATS-DATA").Select
Range("A1").Select
Dim objTable As PivotTable, objField As PivotField
    ActiveWorkbook.Sheets("STATS-DATA").Select
    Range("A1").Select
    Set objTable = Sheets("STATS-DATA").PivotTableWizard
    Set objField = objTable.PivotFields("TRC")
    Set objField2 = objTable.PivotFields("MEDCO MAIL OR AOB")
    objField.Orientation = xlColumnField
    objField2.Orientation = xlColumnField
    Set objField = objTable.PivotFields("DAY")
    objField.Orientation = xlRowField
    objField.PivotItems( _
        "FUTURE SHIP").Position = 1
    objField.PivotItems( _
        "NEXT DAY").Position = 1
    objField.PivotItems( _
        "SAME DAY").Position = 1
    objField.PivotItems( _
        "PREVIOUS SHIP").Position = 1
    objField.PivotItems( _
        "SUNDAY SHIP").Position = 1
    objField.PivotItems( _
        "SATURDAY SHIP").Position = 1
    Set objField = objTable.PivotFields("GROUPER")
    Dim varItemList() As Variant
    Dim strItem1 As String
    Dim i As Long
    Application.ScreenUpdating = False
    varItemList = Array("REVA SUSP", "PAH INH", "PAH ORALS", "PAH INJ", "ZYMES", "HAE", "ALPHA-IG", "ACTI")
    strItem1 = varItemList(LBound(varItemList))
    With objTable.PivotFields("GROUPER")
        .PivotItems(strItem1).Visible = True
        For i = 1 To .PivotItems.Count
            If .PivotItems(i) <> strItem1 And _
                  .PivotItems(i).Visible = True Then
                .PivotItems(i).Visible = False
            End If
        Next i
        For i = LBound(varItemList) + 1 To UBound(varItemList)
            .PivotItems(varItemList(i)).Visible = True
        Next i
    End With
    objField.Orientation = xlRowField
    objField.PivotItems( _
        "ACTI").Position = 1
    objField.PivotItems( _
        "PAH ORALS").Position = 1
    objField.PivotItems( _
        "PAH INH").Position = 1
    objField.PivotItems( _
        "PAH INJ").Position = 1
    objField.PivotItems( _
        "ALPHA-IG").Position = 1
    objField.PivotItems( _
        "HAE").Position = 1
    objField.PivotItems( _
        "ZYMES").Position = 1
    objField.PivotItems( _
        "REVA SUSP").Position = 1
    Set objField = objTable.PivotFields("THERAPY TYPE")
    objField.Orientation = xlRowField
    Set objField = objTable.PivotFields("RX HOME ID #")
    objField.Orientation = xlDataField
    objField.Function = xlCount
    objTable.RowAxisLayout xlOutlineRow
    objTable.TableStyle2 = "PivotStyleMedium9"
    Sheets("Sheet12").Select
    Sheets("Sheet12").Name = "STATS"
    ActiveWorkbook.ShowPivotTableFieldList = False
    objTable.ShowTableStyleRowStripes = True
    objTable.ShowTableStyleColumnStripes = True
    objTable.MergeLabels = False
    objTable.PivotFields("GROUPER").ShowDetail = False
        With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationManual
        .DisplayAlerts = True
        .EnableEvents = True
        End With
End Sub

I'm experiencing an error with this section of the code:
Code:
    objField.PivotItems( _
        "FUTURE SHIP").Position = 1
    objField.PivotItems( _
        "NEXT DAY").Position = 1
    objField.PivotItems( _
        "SAME DAY").Position = 1
    objField.PivotItems( _
        "PREVIOUS SHIP").Position = 1
    objField.PivotItems( _
        "SUNDAY SHIP").Position = 1
    objField.PivotItems( _
        "SATURDAY SHIP").Position = 1

If SATURDAY SHIP isnt on the report it will error out but sometimes the value is on the report and it works fine. The report that this is run on changes all the time and wont necessarily have each of the values in " ". Actually at any given day any of SATURDAY, SUNDAY, PREVIOUS SHIP, NEXT DAY, FUTURE SHIP may not be present but I still need the code to execute. Is there a way to make the code continue to run if it can't execute a line of code say...

Code:
objField.PivotItems( _
        "SATURDAY SHIP").Position = 1

OR

Code:
objField.PivotItems( _
        "SUNDAY SHIP").Position = 1
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Since you know why the error occurs, Try this:

Code:
On Error Resume Next
objField.PivotItems( _
"FUTURE SHIP").Position = 1
objField.PivotItems( _
"NEXT DAY").Position = 1
objField.PivotItems( _
"SAME DAY").Position = 1
objField.PivotItems( _
"PREVIOUS SHIP").Position = 1
objField.PivotItems( _
"SUNDAY SHIP").Position = 1
objField.PivotItems( _
"SATURDAY SHIP").Position = 1
On Error GoTo 0
Code:
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,790
Members
452,942
Latest member
VijayNewtoExcel

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