2014-09-26 30 views
0

我需要定期从SQL数据库的不同工作簿中刷新多个数据透视表。一次刷新多个活动工作簿中的数据透视表

这样做最简单的方法是什么?一次刷新不同工作簿中的数据透视表?它是好的,如果它是在VBA格式。

+0

*工作簿对象*有* RefreshAll *方法刷新所有连接,枢轴,公式等在VBA形式的工作簿(“Workbookname”)。RefreshAll' – L42 2014-09-26 00:58:36

回答

0

OK。下面的代码会问你在哪里的Excel文件存在的文件夹路径(这需要刷新。)

Source :

Sub LoopAllExcelFilesInFolder() 

'PURPOSE: To loop through all Excel files in a user specified folder and perform a set task on them 
'SOURCE: www.TheSpreadsheetGuru.com 

Dim wb As Workbook 
Dim myPath As String 
Dim myFile As String 
Dim myExtension As String 
Dim FldrPicker As FileDialog 

'Optimize Macro Speed 
    Application.ScreenUpdating = False 
    Application.EnableEvents = False 
    Application.Calculation = xlCalculationManual 

'Retrieve Target Folder Path From User 
    Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker) 

    With FldrPicker 
     .Title = "Select A Target Folder" 
     .AllowMultiSelect = False 
     If .Show <> -1 Then GoTo NextCode 
     myPath = .SelectedItems(1) & "\" 
    End With 

'In Case of Cancel 
NextCode: 
    myPath = myPath 
    If myPath = "" Then Exit Sub 

'Target File Extension (must include wildcard "*") 
    myExtension = "*.xls" 

'Target Path with Ending Extention 
    myFile = Dir(myPath & myExtension) 

'Loop through each Excel file in folder 
    Do While myFile <> "" 
    'Set variable equal to opened workbook 
     Set wb = Workbooks.Open(Filename:=myPath & myFile) 

    'Refresh the workbook 
     wb.refreshall 

    'Save and Close Workbook 
     wb.Close SaveChanges:=True 

    'Get next file name 
     myFile = Dir 
    Loop 

'Message Box when tasks are completed 
    MsgBox "Task Complete!" 

'Reset Macro Optimization Settings 
    Application.ScreenUpdating = True 
    Application.EnableEvents = True 
    Application.Calculation = xlCalculationAutomatic 

End Sub 
0

这是真棒!它的工作..现在我需要做的是观察刷新后的结果..如果它的工作100%在输出方面,我需要..

+0

请使用您的问题上的编辑链接添加额外的信息。后回答按钮应该只用于问题的完整答案。 – Luke 2014-09-26 07:10:37

+0

抱歉,我只是一个初学者 – Istian 2014-09-26 07:23:53

相关问题