Main Excel Sheet
As you can see in the image, there are some pivot tables on the Excel sheet. Although they are named PivotTable1 to PivotTable11, they aren't present in the same physical order in the sheet. I want to get the names of the pivot tables in the physical order in which they are present on the sheet.
I tried using ChatGPT, but it's not able to help. This is part of a bigger project where there are hundreds of pivot tables. Can anyone help with a VBA code to do the same?
I'm getting this
but I want this
The Code
Sub GetPivotTableRange()
'GetPivotTableRange
Dim pt As PivotTable
Dim ptRange As Range
Dim ptName As String
'Loop through all the PivotTables in the active worksheet
For Each pt In ActiveSheet.PivotTables
'Get the PivotTable name and range
ptName = pt.Name
Set ptRange = pt.TableRange1
'Display the range in the desired format
Range(ptRange.Cells(1, 1), ptRange.Cells(1, ptRange.Columns.Count)).Select
Dim rangeAddress As String
rangeAddress = ActiveWindow.Selection.Address
rangeAddress = Replace(rangeAddress, "$", "")
rangeAddress = Replace(rangeAddress, ":", ":")
rangeAddress = Replace(rangeAddress, "1", "")
rangeAddress = Replace(rangeAddress, "2", "")
rangeAddress = Replace(rangeAddress, "3", "")
rangeAddress = Replace(rangeAddress, "4", "")
rangeAddress = Replace(rangeAddress, "5", "")
rangeAddress = Replace(rangeAddress, "6", "")
rangeAddress = Replace(rangeAddress, "7", "")
rangeAddress = Replace(rangeAddress, "8", "")
rangeAddress = Replace(rangeAddress, "9", "")
rangeAddress = Replace(rangeAddress, "0", "")
'Display the PivotTable name and range address
Debug.Print ptName & "= " & rangeAddress
Next pt
End Sub