title: Pivot table with if condition
body: <p>I want to develop a pivot table. </p>
  
  <p>I have tried a code that can do it as well. </p>
  
  <p>but my requirement is , The code should look only for "AB", if the column AB contains Ontime, then it should look for the values in AD accordingly.</p>
  
  <p>Could any one help me how I can do it. </p>
  
  <pre><code>  Sub mismatch2()
  
      Dim ws1 As Worksheet
      Dim pc1 As PivotCache
      Dim pt1 As PivotTable
      Dim ct1 As Integer
      Dim TableName As String
  
      Set ws1 = Sheets("Mismatch")
      ' get the pivot cache for the pivot table
      Set pc1 = ActiveWorkbook.PivotCaches.Create(xlDatabase, "'BW'!R4C1:R1048576C32")
      ' get the pivot table range
      Set pt1 = pc1.CreatePivotTable(ws1.Range("A28"), TableName:="PivotTable15")
      ' get the parameter to count
      pt1.AddDataField pt1.PivotFields("Mismatch"), "Sum of Mismatch", xlCount
  
      With pt1
  
          ' declare the parameter for row and arrange the values in descending order
          With .PivotFields("Location in full form")
              .Orientation = xlRowField
              .Position = 1
              .AutoSort xlDescending, "Sum of Mismatch"
              .PivotItems("(blank)").Visible = False
          End With
  
          ' declare the parameter in the column and alighn the values to center
          With .PivotFields("Mismatch")
              .Orientation = xlColumnField
              .Position = 1
              .PivotItems("(blank)").Visible = False
              .PivotItems("G70 OK").Visible = False
              .PivotItems("G70 OK(R)").Visible = False
              .PivotItems("N/A").Visible = False
  
              pt1.TableRange2.Offset(0, 1).HorizontalAlignment = xlCenter
          End With
  
      End With
  End Sub
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/E2krf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/E2krf.png" alt="this is the sample data . So, the column AB I have mentioned is now in the second column of this sample data sheet. there you could find both ontime and forecast."></a></p>
  
  <p><a href="https://i.stack.imgur.com/Wf2jX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wf2jX.png" alt="this is how my pivot output should look like. the row field is my column A and then it should look only for Ontime in Column B and give the count values of column C"></a></p>