title: Run time error 13 - While changing the pivotcache
body: <p>Im new on this forum. I wanted to ask if anyone can help me with this. I wrote a macro that changes all the pivottables sources on which if the name of the pt ends with 1 it applies one source from another xlsx file and if its 2 it gives another one from another file. I keep both books open during the macro to make sure it doesnt break on that side. But in "case 2" it always throws the run time error 13 : type missmatch . Can anyone help me fix this. Thanks before hand!</p>
  
  <pre><code>Sub origen_tablas_fwb()
  
  Dim hoja As Worksheet
  Dim pt As PivotTable
  Dim a1, a2, j As Long
  Dim libro1, libro2 As String
  
  a2 = Workbooks("Global ETdlc.xlsx").Worksheets("Comando").Range("A7").Value
  a1 = a2 - 1
  Windows("Global ETdlc.xlsx").Activate
  Sheets("Comando").Activate
  j = Sheets("Comando").Range("N1", Range("N1").End(xlDown)).Rows.Count
  libro2 = Workbooks("Global ETdlc.xlsx").Worksheets("Comando").Range("B8").Value
  For i = 1 To j
      If Cells(i, 14).Value = libro2 Then
      libro1 = Cells(i, 15).Value
      End If
  Next i
  
  'Workbooks.Open Filename:="C:\Respaldo Reservas\FEEDS+FWB\" &amp; a1 &amp; "\" &amp; libro1
  'Workbooks.Open Filename:="C:\Respaldo Reservas\FEEDS+FWB\" &amp; a2 &amp; "\" &amp; libro2
  
  'Workbooks.Open Filename:="C:\Informes\Forward Bookings"
  Windows("Forward Bookings").Activate
  For Each hoja In ActiveWorkbook.Worksheets
      For Each pt In Worksheets(hoja.Name).PivotTables
          Select Case Right(pt.Name, 1)
          Case 1
          'cambiar cache año anterior
          ActiveSheet.pt.ChangePivotCache ActiveWorkbook. _
          PivotCaches.Create(SourceType:=xlDatabase, _
          SourceData:=Workbooks(libro1).Sheets("Input").Range("A1").CurrentRegion)
          Case 2
          'cambiar cache año actual
          ActiveSheet.pt.ChangePivotCache ActiveWorkbook. _
          PivotCaches.Create(SourceType:=xlDatabase, _
          SourceData:=Workbooks(libro2).Sheets("Input").Range("A1").CurrentRegion)
          End Select
      Next pt
  Next hoja
  Windows(libro1).Close
  Windows(libro2).Close
  End Sub
  </code></pre>