Excel主表
如图所示,Excel工作表上有一些数据透视表。虽然它们被命名为PivotTable1到PivotTable11,但它们在工作表中的物理顺序并不相同。我想按照透视表在工作表中的物理顺序获取透视表的名称。
我试过使用ChatGPT,但它无法帮助。这是一个更大的项目的一部分,其中有数百个数据透视表。任何人都可以用VBA代码来做同样的事情吗?
我来搞定
但我想要这个
法典
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
1条答案
按热度按时间lnlaulya1#
试试这个:它将所有Pivottables及其最左边的列号收集到一个Collection中,然后根据列号对该集合进行排序。