Solved

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

Posted on 2016-10-03
4
54 Views
Last Modified: 2016-10-04
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.
0
Comment
Question by:myyis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:Koen
ID: 41827124
    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?
0
 

Author Comment

by:myyis
ID: 41827169
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.
0
 
LVL 8

Accepted Solution

by:
Koen earned 500 total points
ID: 41827231
Myfolder = Environ("Username")
MyPivotSource = "\Users\" & Myfolder & "\Downloads\" & MyFilename
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ActiveWorkbook. _
        PivotCaches.Create(SourceType:=xlDatabase, SourceData:=MyPivotsource, Version _
        :=xlPivotTableVersion15)

Open in new window

0
 

Author Comment

by:myyis
ID: 41828079
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)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question