Solved

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

759 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

21 Experts available now in Live!

Get 1:1 Help Now