0
Function GetPivotTableConflicts(wb As Workbook) As Collection
' returns a collection with information about pivottables that overlap or  intersect each other
Dim ws As Worksheet, i As Long, j As Long, strName As String
If wb Is Nothing Then Exit Function

Set GetPivotTableConflicts = New Collection
With wb
    For Each ws In .Worksheets
        With ws
            strName = "[" & .Parent.Name & "]" & .Name
            Application.StatusBar = "Checking PivotTable conflicts in " & strName & "..."
            If .PivotTables.Count > 1 Then
                For i = 1 To .PivotTables.Count - 1
                    For j = i + 1 To .PivotTables.Count
                        If OverlappingRanges(.PivotTables(i).TableRange2, .PivotTables(j).TableRange2) Then
                            GetPivotTableConflicts.Add Array(strName, "Intersecting", _
                                .PivotTables(i).Name, .PivotTables(i).TableRange2.Address, _
                                .PivotTables(j).Name, .PivotTables(j).TableRange2.Address)
                        Else
                            If AdjacentRanges(.PivotTables(i).TableRange2, .PivotTables(j).TableRange2) Then
                                GetPivotTableConflicts.Add Array(strName, "Adjacent", _
                                    .PivotTables(i).Name, .PivotTables(i).TableRange2.Address, _
                                    .PivotTables(j).Name, .PivotTables(j).TableRange2.Address)
                            End If
                        End If
                    Next j
                Next i
            End If
        End With
    Next ws
    Set ws = Nothing
    Application.StatusBar = False
End With
If GetPivotTableConflicts.Count = 0 Then Set GetPivotTableConflicts = Nothing
End Function

Function OverlappingRanges(objRange1 As Range, objRange2 As Range) As Boolean
OverlappingRanges = False
If objRange1 Is Nothing Then Exit Function
If objRange2 Is Nothing Then Exit Function

If Not Application.Intersect(objRange1, objRange2) Is Nothing Then
    OverlappingRanges = True
End If
End Function

Function AdjacentRanges(objRange1 As Range, objRange2 As Range) As Boolean
AdjacentRanges = False
If objRange1 Is Nothing Then Exit Function
If objRange2 Is Nothing Then Exit Function

With objRange1
    If .Top + .Height = objRange2.Top Then
        AdjacentRanges = True
    End If
    If .Left + .Width = objRange2.Left Then
        AdjacentRanges = True
    End If
End With
With objRange2
    If .Top + .Height = objRange1.Top Then
        AdjacentRanges = True
    End If
    If .Left + .Width = objRange1.Left Then
        AdjacentRanges = True
    End If
End With
End Function

Sub ShowPivotTableConflicts()
' creates a list with all pivottables in the active workbook that conflicts with each other
Dim coll As Collection, i As Long, varItems As Variant, r As Long
If ActiveWorkbook Is Nothing Then Exit Sub

Set coll = GetPivotTableConflicts(ActiveWorkbook)
If coll Is Nothing Then
    MsgBox "No PivotTable conflicts in the active workbook!", vbInformation
Else
    Workbooks.Add ' create a new workbook
    Range("A1").Formula = "Worksheet:"
    Range("B1").Formula = "Conflict:"
    Range("C1").Formula = "PivotTable1:"
    Range("D1").Formula = "TableAddress1:"
    Range("E1").Formula = "PivotTable2:"
    Range("F1").Formula = "TableAddress2:"
    Range("A1").CurrentRegion.Font.Bold = True
    r = 1
    For i = 1 To coll.Count
        r = r + 1
        varItems = coll(i)
        Range("A" & r).Formula = varItems(0)
        Range("B" & r).Formula = varItems(1)
        Range("C" & r).Formula = varItems(2)
        Range("D" & r).Formula = varItems(3)
        Range("E" & r).Formula = varItems(4)
        Range("F" & r).Formula = varItems(5)
    Next i
    Range("A1").CurrentRegion.EntireColumn.AutoFit
    Range("A2").Select
    ActiveWindow.FreezePanes = True
    Range("A1").Select
End If
End Sub

Small update, could somebody help me turnaround this combination of functions and macro so that when spotting an overlapping pivot table it could insert rows until is fixed and then move on to the next pivot?

To mention that there are many pages with many pivots on each and it is done on daily basis.

Thank you in advance!

1
  • There's no code that can avoid overlap as placement of Pivot Tables are by designer. You will need to move Pivot Tables accordingly. Due to their dynamic nature, multiple pivots can pose this challenge. Always design them in a way to leave extra space below and to right or structure the column and row values to not expand or change but filter lower. Commented Dec 5, 2015 at 23:28

2 Answers 2

1

This blogpost has code addressing your issue: http://erlandsendata.no/?p=3733

Sign up to request clarification or add additional context in comments.

Comments

0

So this code will not adjust the position of the tables at all, it will just cycle through all worksheets in the workbook, refreshing and pivot tables. I suggest that you move the pivot tables (vba is not needed for this, just move the table in excel).

1 Comment

Cheers mate but I have 12 pages with at least 10 tables in each and I need to refresh them on weekly basis. Maybe somebody else can help me out?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.