Solved

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

Posted on 2016-10-03
4
34 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find word and 6 digit number 22 98
ActiveX Listbox Multi Select in Excel 2010 8 19
Sum iF  based on a null cell 11 29
NEED TO UPDATE DATA IN EXCEL 18 32
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

920 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now