I am looking for a code to use an index match formula with 2 criteria. Index data is on sheet "cpk_mola_pivot" in pivot table called "cpk_mola_pivottable".
Results to be returned are in the pivot table column 3.
Criteria 1 is on source sheet "comparison report" in Column J starting row 3 to last row. Criteria 1 is matched to Column A in the pivot table. Criteria 2 is fixed in cell $O$2.
Criteria 2 is matched to Column B in the pivot table. Results to be populated O3 to last row in the source sheet "comparison report".
When I run the code I am getting an object required at the resultsrange code line even though I specified the pivottable.
sub cpkMola_indexmatch2
Dim wsComparisonReport As Worksheet
Dim wsCPK_Mola_Pivot As Worksheet
Dim mypivotTable As pivotTable
Dim lastRowComparison As Long, lastRowPivot As Long
Dim criteria1 As Range, criteria2 As Variant
Dim resultRange As Range
Dim i As Long
' Set the references to the worksheets
Set wsComparisonReport = ThisWorkbook.Worksheets("comparison report")
Set wsCPK_Mola_Pivot = ThisWorkbook.Worksheets("cpk_mola_pivot")
' Set the reference to the pivot table
Set mypivotTable = wsCPK_Mola_Pivot.PivotTables("cpk_mola_pivottable")
' Find the last rows in the Comparison Report and Pivot Table sheets
lastRowComparison = wsComparisonReport.Cells(wsComparisonReport.Rows.Count, "J").End(xlUp).Row
lastRowPivot = wsCPK_Mola_Pivot.Cells(wsCPK_Mola_Pivot.Rows.Count, "A").End(xlUp).Row
' Set the ranges for Criteria 1 and Criteria 2
Set criteria1 = wsComparisonReport.Range("J3:J" & lastRowComparison)
criteria2 = wsComparisonReport.Range("$O$2").value
' Set the result range in the Comparison Report sheet
Set resultRange = wsComparisonReport.Range("O3:O" & lastRowComparison)
' Loop through each row in the Comparison Report sheet and use Index-Match to populate the results
For i = 1 To criteria1.Rows.Count
resultRange.Cells(i, 1).value = Application.Index(mypivotTable.DataBodyRange.Columns(1).Resize(, 3), _
Application.Match(criteria1.Cells(i, 1).value, mypivotTable.DataBodyRange.Columns(1), 0), _
Application.Match(criteria2.value, mypivotTable.DataBodyRange.Columns(2), 0))
Next i
End Sub