Link to home
Start Free TrialLog in
Avatar of myyis
myyis

asked on

Change the path of the data source in my Excel pivot table with macro

With a macro I need to change the path of the data source in my Excel pivot table

\Users\James\Downloads[exampleexcel.xls]

to:

\Users\currentuser\Downloads[exampleexcel.xls]

Please note that [exampleexcel.xls] is also dynamic.

Anybody can help me? Thank you very much.
Avatar of Koen
Koen
Flag of Belgium image

    MyPivotSource = "\Users\currentuser\Downloads\" & MyFilename
    ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:=MyPivotsource, Version _
        :=xlPivotTableVersion15)

Open in new window


Would this be what you are looking for?
Avatar of myyis
myyis

ASKER

Hi Koen,
Thank you for your answer, I see that  my question is not clear enough.
It looks good but the  "currentuser"  is also dynamic.
"currentuser" should  be the current windows user logged in.
ASKER CERTIFIED SOLUTION
Avatar of Koen
Koen
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of myyis

ASKER

With a manupulation I have solved
Thank you

Myfolder = Environ("Username")
MyPivotsrc = ActiveSheet.PivotTables("PivotTable1").SourceData

Dim str As String
Dim openPos As Integer
Dim closePos As Integer
Dim midBit As String


openPos = InStr(MyPivotsrc, "Users\")
closePos = InStr(MyPivotsrc, "\Downloads")
midBit = Mid(MyPivotsrc, openPos + 6, closePos - openPos - 6)
MyPivotsource = Replace(MyPivotsrc, midBit, Myfolder)
MyPivotsource = Replace(MyPivotsource, "'", "")
MyPivotsource = "C:" & MyPivotsource



ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
        MyPivotsource, _
        Version:=xlPivotTableVersion15)