2014-04-02 128 views
0

我有一個包含一個大型數據庫一個工作簿。透視表數據源

(讓我們稱之爲Basefile.xlsm)

我已經作出,根據部門數據排序的代碼,並使用此更新一些預先存在的數據透視表。

透視表和排序的數據,然後保存在每個部門一個單獨的文件。

(讓我們打電話給這些Department1.xls,Department2.xls等)。

我的問題是,每個新文件的數據透視表仍然指向原始工作簿,而不是新的。

(所以Department1.xls樞軸表應該從Department1.xls片得到它的數據,但目前所有的數據透視表仍使用Basefile.xlsm作爲數據源)

是否有辦法糾正這個? - 沒有使用代碼製作所有數據透視表?

Sub Selectdata() 


    Application.ScreenUpdating = False2 



' filters for nivå2 enhet, cuts and pastes data into a sheet named after nivå2 - ready for creating pivot table 

Dim i As Integer 
Dim WS As Worksheet 

For i = Worksheets("Department").Range("g4").Value To Worksheets("Department").Range("h4").Value 

Sheets("Basefile 2014").Select 
ActiveSheet.Range("A:O").AutoFilter Field:=15, Criteria1:= _ 
    Worksheets("Department").Range("b" & i).Value 

Cells.Select 
Range("A29619").Activate 
Selection.Copy 
Sheets("Metode").Select 
Set WS = Sheets.Add 
ActiveSheet.Paste 
WS.Name = "RawData 2014" 

'shows all the data in the new worksheet 
WS.Select 
ActiveSheet.Range("A:O").AutoFilter Field:=15 
    Columns("l:l").Select 

' repeats proceedure for 2013 
Sheets("Basefile 2013").Select 
ActiveSheet.Range("A:O").AutoFilter Field:=15, Criteria1:= _ 
Worksheets("Department").Range("b" & i).Value 
Cells.Select 
Range("A29619").Activate 
Selection.Copy 
Sheets("Metode").Select 
Set WSD = Sheets.Add 
ActiveSheet.Paste 
WSD.Name = "RawData 2013" 

'shows all the data in the new worksheet 
WSD.Select 
ActiveSheet.Range("A:O").AutoFilter Field:=15 
    Columns("l:l").Select 

'Refreshes all the pivot table data 
ActiveWorkbook.RefreshAll 

Application.ScreenUpdating = True 

Filename = Worksheets("Department").Range("c" & i).Value & "2014" 
Fname = Worksheets("Department").Range("c" & i).Value & "2013" 


Sheets(Array("Funn 2013", "Pivot 2013", "RawData 2013")).Copy 

With ActiveWorkbook 
.SaveAs "F:\X Simulation\test\" & Fname 
.Close 
End With 


Sheets(Array("Pivot 1.", "Pivot 2.", "Pivot 3.", "Pivot 4.", "Funn 2014", "RawData 2014")).Copy 



With ActiveWorkbook 
.SaveAs "F:\X Simulation\test\" & Filename 
.Close 
End With 

Application.DisplayAlerts = False 
Worksheets("RawData 2014").Delete 
Worksheets("RawData 2013").Delete 
Application.DisplayAlerts = True 


Next i 

End Sub 

回答

0

這可以通過更改數據透視表的SourceData屬性來實現。

Worksheets("PivotTable").PivotTables(1).SourceData = Worksheets("SourceData").Range("A1:Z999").Address 

您將需要編輯工作表的參數,當然,數據範圍從您的源數據來源。

您還可以記錄適當的步驟,即編輯透視表的來源,你的Department1.xls片,與宏錄製,並查看生成的代碼。

相關問題