I have been working on a project whereby I have a template sheet in excel (to leep track of daily time spent)
I used the macro screen recording function on excel to duplicate the sheet and update the source data for the new pivot tables per sheet.
Within the VBA code, it is directly linking to the file path, however, this means when i move the file or share it with others then this code needs to be manually updated.
I was trying to see if i could link to the “current active workbook” or make it dynamic, however, i wasn’t coming right (especially with the workbook being saved to a sharepoint site on One Drive”)
Is there any advice you could possibly give me please?
Using “active workbook links”
Here is my code:
Sub Add_New_Week()
'
' Add_New_Week Macro
'
' Unprotect the sheet
Sheets("Template").Unprotect "Password"
Sheets("Template").Copy After:=Sheets(Sheets.Count)
Sheets("Template (2)").Select
Sheets("Template (2)").Name = "{RenameWeek}"
Sheets("{RenameWeek}").Select
Range("B34").Select
ActiveSheet.PivotTables("PivotTable_Activity_0").ChangePivotCache _
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"https://za[companyname]-my.sharepoint/personal/dylan_r_rees_[companyname]_com/Documents/1. Timesheets/[2025 Timesheets.xlsm]{RenameWeek}!R4C59:R1048576C63" _
, Version:=8)
Range("E34").Select
ActiveSheet.PivotTables("PivotTable5").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"https://za[companyname]-my.sharepoint/personal/dylan_r_rees_[companyname]_com/Documents/1. Timesheets/[2025 Timesheets.xlsm]{RenameWeek}!R4C59:R1048576C63" _
, Version:=8)
Range("CE6").Select
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ("PivotTable_Activity_0" _
)
Range("CQ6").Select
ActiveSheet.PivotTables("PivotTable2").ChangePivotCache ("PivotTable5")
Range("A4").Select
' Protect the sheet again
Sheets("Template").Protect "Password", UserInterfaceOnly:=True, AllowUsingPivotTables:=True
Sheets("{RenameWeek}").Protect "Password", UserInterfaceOnly:=True, AllowUsingPivotTables:=True
End Sub
I have been working on a project whereby I have a template sheet in excel (to leep track of daily time spent)
I used the macro screen recording function on excel to duplicate the sheet and update the source data for the new pivot tables per sheet.
Within the VBA code, it is directly linking to the file path, however, this means when i move the file or share it with others then this code needs to be manually updated.
I was trying to see if i could link to the “current active workbook” or make it dynamic, however, i wasn’t coming right (especially with the workbook being saved to a sharepoint site on One Drive”)
Is there any advice you could possibly give me please?
Using “active workbook links”
Here is my code:
Sub Add_New_Week()
'
' Add_New_Week Macro
'
' Unprotect the sheet
Sheets("Template").Unprotect "Password"
Sheets("Template").Copy After:=Sheets(Sheets.Count)
Sheets("Template (2)").Select
Sheets("Template (2)").Name = "{RenameWeek}"
Sheets("{RenameWeek}").Select
Range("B34").Select
ActiveSheet.PivotTables("PivotTable_Activity_0").ChangePivotCache _
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"https://za[companyname]-my.sharepoint/personal/dylan_r_rees_[companyname]_com/Documents/1. Timesheets/[2025 Timesheets.xlsm]{RenameWeek}!R4C59:R1048576C63" _
, Version:=8)
Range("E34").Select
ActiveSheet.PivotTables("PivotTable5").ChangePivotCache ActiveWorkbook. _
PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"https://za[companyname]-my.sharepoint/personal/dylan_r_rees_[companyname]_com/Documents/1. Timesheets/[2025 Timesheets.xlsm]{RenameWeek}!R4C59:R1048576C63" _
, Version:=8)
Range("CE6").Select
ActiveSheet.PivotTables("PivotTable1").ChangePivotCache ("PivotTable_Activity_0" _
)
Range("CQ6").Select
ActiveSheet.PivotTables("PivotTable2").ChangePivotCache ("PivotTable5")
Range("A4").Select
' Protect the sheet again
Sheets("Template").Protect "Password", UserInterfaceOnly:=True, AllowUsingPivotTables:=True
Sheets("{RenameWeek}").Protect "Password", UserInterfaceOnly:=True, AllowUsingPivotTables:=True
End Sub
Share
Improve this question
edited Feb 3 at 20:01
Dylan Rees
asked Feb 2 at 20:30
Dylan ReesDylan Rees
11 bronze badge
11
|
Show 6 more comments
1 Answer
Reset to default 0Untested, but this avoids having to include the path:
Sub Add_New_Week()
Const PW As String = "Password"
Const WK_NAME As String = "{RenameWeek}"
Dim wb As Workbook, ws As Worksheet, pc As PivotCache, wsWeek As Worksheet
Set wb = ThisWorkbook
With wb.Worksheets("Template")
.Unprotect PW
.Copy After:=wb.Worksheets(wb.Worksheets.count)
.Protect PW, UserInterfaceOnly:=True, AllowUsingPivotTables:=True
End With
Set ws = wb.Worksheets(wb.Worksheets.count)
ws.Name = WK_NAME
Set wsWeek = wb.Worksheets(WK_NAME)
Set pc = wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=wsWeek.Range("$BG$4:$BK$1048576"), Version:=8)
With wsWeek
.PivotTables("PivotTable_Activity_0").ChangePivotCache pc
.PivotTables("PivotTable5").ChangePivotCache pc
.PivotTables("PivotTable1").ChangePivotCache "PivotTable_Activity_0"
.PivotTables("PivotTable2").ChangePivotCache "PivotTable5"
.Protect "Password", UserInterfaceOnly:=True, AllowUsingPivotTables:=True
End With
End Sub
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257674a4619053.html
ActiveWorkbook.Path
orThisWorkbook.Path
is probably what you want. If neither of those work, then you should edit your post to explain why not and preferably add the code you need to adjust. – Tim Williams Commented Feb 2 at 21:28