XLA routines: EE_CreatePivotTableFromRange

Nick's picture
EE_CreatePivotTableFromRange creates a pivot table from a range
Function EE_CreatePivotTableFromRange(rngPivotSource As range, rngPivotTarget As range) As Boolean
    Dim wbk             As Workbook
    Dim objPivotCache   As PivotCache
    Dim objPivotTable   As PivotTable
 
'http://excelexperts.com/xla-routines-eeCreatePivotTableFromRange    for updates on this function
    On Error Resume Next
        Set wbk = rngPivotSource.Parent.Parent
 
        Set objPivotCache = wbk.PivotCaches.Create(xlDatabase, rngPivotSource.Address(1, 1, xlA1, True))
        Set objPivotTable = objPivotCache.CreatePivotTable(rngPivotTarget, "Pivot" & wbk.PivotCaches.Count + 1, True)
    On Error GoTo 0: Err.Clear: On Error GoTo -1
 
    EE_CreatePivotTableFromRange = Not (objPivotTable Is Nothing)
 
    Set wbk = Nothing
    Set objPivotCache = Nothing
    Set objPivotTable = Nothing
End Function