用VBA修改所有透视表的数据源 您所在的位置:网站首页 改数据透视表源数据的方法 用VBA修改所有透视表的数据源

用VBA修改所有透视表的数据源

2024-03-17 02:50| 来源: 网络整理| 查看: 265

Public Sub Update_PivotTables_Source()

Dim currWS As Worksheet

Dim currPT As PivotTable

Dim strName As String

Dim strMsg As String

Dim Res

On Error Resume Next

Filename = ThisWorkbook.Sheets("修改数据源").Cells(3, 3)

Workbooks.Open (Filename)

For Each currWS In Application.Worksheets

    For Each currPT In currWS.PivotTables

        currPT.SourceData = CutFilename(currPT.SourceData)

        currPT.RefreshTable

    Next currPT

Next currWS

MsgBox "所有透视表数据源更新完毕!"

End Sub

Private Function CutFilename(strSource As String) As String

Dim intPosition As Integer

Dim intStrLen As Integer

Dim blnFound As Boolean

Dim intFileStart As Integer

Dim intFileEnd As Integer

Dim chrCurr As String

strSource = Trim(strSource)

CutFilename = strSource

intPosition = 0: intStrLen = Len(strSource)

intFileStart = 0: intFileEnd = 0

blnFound = False

Do While (Not (blnFound) And (intPosition < intStrLen))

    intPosition = intPosition + 1

    chrCurr = Mid(strSource, intPosition, 1)

    Select Case chrCurr

        Case "["

            intFileStart = intPosition

        Case "]"

            intFileEnd = intPosition

            blnFound = True

    End Select

Loop

If blnFound Then CutFilename = Mid(strSource, 1, intFileStart - 1) & Mid(strSource, intFileEnd + 1, intStrLen)

End Function



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有