title: vba "Application-defined or object-defined error" and "expression not defined in context" when watched
body: <p>I have this <code>VBA</code> code that worked perfectly fine before yet suddenly not anymore. It returns </p>
  
  <blockquote>
    <p>application-defined or object defined error</p>
  </blockquote>
  
  <p>now, when I watched the failed line, it says </p>
  
  <blockquote>
    <p>expression not defined in context</p>
  </blockquote>
  
  <p>Basically, what this macro does is to let people choose pivot table filters by several times in a group, rather than one by one.  Below is the code, any help will be strongly appreciated. </p>
  
  <pre><code>Sub AdvancedFilter1()
      Dim ws
      Set ws = Workbooks("Bayer IB.xlsm").Worksheets("Pivots")
      ws.Activate
      Dim pv3
      Dim pv4
      Set pv3 = ws.PivotTables("PivotTable3").PivotFields("Shipped Date FY")
      Set pv4 = ws.PivotTables("PivotTable4").PivotFields("Shipped Date FY")
      Workbooks("Bayer IB.xlsm").Worksheets("Pivots").Activate
     If ws.Range("B1").Activate Then
          If ActiveCell.Value = "10" Then
          With pv3
          .PivotItems("2007").Visible = True
          .PivotItems("2008").Visible = True
          .PivotItems("2009").Visible = True
          .PivotItems("2010").Visible = True
          .PivotItems("2011").Visible = True
          .PivotItems("2012").Visible = True
          .PivotItems("2013").Visible = True
          .PivotItems("2014").Visible = True
          .PivotItems("2015").Visible = True
          .PivotItems("2016").Visible = True
             End With
  
          ElseIf ActiveCell.Value = "2" Then
          With pv3
          .PivotItems("2007").Visible = False
          .PivotItems("2008").Visible = False
          .PivotItems("2009").Visible = False
          .PivotItems("2010").Visible = False
          .PivotItems("2011").Visible = False
          .PivotItems("2012").Visible = False
          .PivotItems("2013").Visible = False
          .PivotItems("2014").Visible = False
          .PivotItems("2015").Visible = True
          .PivotItems("2016").Visible = True
             End With
  
  
      End If
    End If
  
  End Sub
  
  Private Sub Worksheet_Change(ByVal Target As Range)
  
    Select Case Target.Address
  
      Case "$B$1"
          If Target = "10" Then
          Call AdvancedFilter1
  
  
          ElseIf Target = "2" Then
          Call AdvancedFilter1
  
          ElseIf Target = "3" Then
          Call AdvancedFilter1
  
  
          ElseIf Target = "5" Then
          Call AdvancedFilter1
  
           ElseIf Target = "7" Then
          Call AdvancedFilter1
  
          End If
          'TO PREVENT AUTOMATIC REDIRECT TO 'J3'
          Range("B1").Select
  
  
    End Select
  
  End Sub
  </code></pre>