Cần giúp đỡ Code để Refresh All data và Pivot table

Liên hệ QC

echo07

Nguyệt Hà
Tham gia
24/8/07
Bài viết
422
Được thích
316
Chào các anh em.

Mình sử dụng Query để lấy dữ liệu từ nhiều file khác nhau sau đó dùng Pivot table từ Query này. Các file kia sẽ cập nhật hàng ngày và mỗi lần muốn lấy dữ liệu mới mình phải bấm Refresh All để load dữ liệu về bảng Query, sau đó refresh lại bảng Pivot table để có dữ liệu mới nhất. Mình đã thử code này và lặp lại mấy lần nhưng vẫn không được, mong mọi người giúp đỡ.

"Sub refresh()
Application.ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
ActiveWorkbook.RefreshAll
ActiveSheet.PivotTables("PivotTable1").PivotCache.refresh
Application.ActiveWorkbook.RefreshAll
Application.ScreenUpdating = True
ActiveWorkbook.RefreshAll
ActiveSheet.PivotTables("PivotTable1").PivotCache.refresh
End Sub"
 
Code này mình làm dùng để refresh query xong tới pivot. Nếu sheet được khóa hoặc ẩn thì sẽ được mở khóa, bỏ ẩn trước, refresh xong khóa lại như cũ. Bạn tham khảo

Sub RefreshSheets()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Dim ws As Worksheet
Dim WasHidden As XlSheetVisibility
Dim WasProtected As Boolean
Dim Password As String
Dim qt As Object
Dim pt As PivotTable
Dim lo As ListObject

Password = "Password" ' Change this to your actual password

' Loop through all worksheets to refresh the query tables
For Each ws In ActiveWorkbook.Worksheets
With ws
WasHidden = .Visible

If WasHidden = xlSheetHidden Or WasHidden = xlSheetVeryHidden Then
.Visible = xlSheetVisible
End If

If .ProtectContents Then
WasProtected = True
.Unprotect Password
End If

For Each qt In .QueryTables
qt.Refresh BackgroundQuery:=False
Next qt

For Each lo In .ListObjects
If lo.SourceType = 3 Then
lo.QueryTable.Refresh BackgroundQuery:=False
End If
Next lo

If WasProtected Then
.Protect Password
WasProtected = False
End If

.Visible = WasHidden
End With
Next ws

' Loop through all worksheets to refresh the pivot tables
For Each ws In ActiveWorkbook.Worksheets
With ws
WasHidden = .Visible

If WasHidden = xlSheetHidden Or WasHidden = xlSheetVeryHidden Then
.Visible = xlSheetVisible
End If

If .ProtectContents Then
WasProtected = True
.Unprotect Password
End If

For Each pt In .PivotTables
pt.RefreshTable
Next pt

If WasProtected Then
.Protect Password
WasProtected = False
End If

.Visible = WasHidden
End With
Next ws

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
 

File đính kèm

  • RefreshSheets.txt
    2.3 KB · Đọc: 11
Lần chỉnh sửa cuối:
Code này mình làm dùng để refresh query xong tới pivot. Nếu sheet được khóa hoặc ẩn thì sẽ được mở khóa, bỏ ẩn trước, refresh xong khóa lại như cũ. Bạn tham khảo

Sub RefreshSheets()

Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual

Dim ws As Worksheet
Dim WasHidden As XlSheetVisibility
Dim WasProtected As Boolean
Dim Password As String
Dim qt As Object
Dim pt As PivotTable
Dim lo As ListObject

Password = "Password" ' Change this to your actual password

' Loop through all worksheets to refresh the query tables
For Each ws In ActiveWorkbook.Worksheets
With ws
WasHidden = .Visible

If WasHidden = xlSheetHidden Or WasHidden = xlSheetVeryHidden Then
.Visible = xlSheetVisible
End If

If .ProtectContents Then
WasProtected = True
.Unprotect Password
End If

For Each qt In .QueryTables
qt.Refresh BackgroundQuery:=False
Next qt

For Each lo In .ListObjects
If lo.SourceType = 3 Then
lo.QueryTable.Refresh BackgroundQuery:=False
End If
Next lo

If WasProtected Then
.Protect Password
WasProtected = False
End If

.Visible = WasHidden
End With
Next ws

' Loop through all worksheets to refresh the pivot tables
For Each ws In ActiveWorkbook.Worksheets
With ws
WasHidden = .Visible

If WasHidden = xlSheetHidden Or WasHidden = xlSheetVeryHidden Then
.Visible = xlSheetVisible
End If

If .ProtectContents Then
WasProtected = True
.Unprotect Password
End If

For Each pt In .PivotTables
pt.RefreshTable
Next pt

If WasProtected Then
.Protect Password
WasProtected = False
End If

.Visible = WasHidden
End With
Next ws

Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub
Vâng, cảm ơn bác nhiều. Chính là nó đây rồi, để em xóa bớt mấy cái không cần thiết đi ạ.
 
Web KT
Back
Top Bottom