-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUpdate Macro.txt
More file actions
58 lines (47 loc) · 1.46 KB
/
Update Macro.txt
File metadata and controls
58 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
' VBS cannot handle Excel erros events, so we can launch refresh using VBA code to have more control
' This file contains VBA macro that will be embedded into Excel workbook from VBS
' Enable 'Trust access to the VBA project object model' at
' File - Options - Trust Center - Trust Center Settings - Macro Settings
Function UpdateConnections() as Integer
Dim cnct
Dim slc As SlicerCache
On Error Resume Next
Application.Calculate
ThisWorkbook.Model.Initialize
If Err.Number <> 0 Then
' handle error
Err.Clear
end if
For Each cnct In ThisWorkbook.Connections
Select case cnct.type
case xlconnectiontypeodbc
cnct.ODBCConnection.BackgroundQuery = False
case xlconnectiontypeoledb
cnct.OledbConnection.BackgroundQuery = False
end select
Next cnct
ThisWorkbook.RefreshAll
Application.Calculate
Application.CalculateUntilAsyncQueriesDone
' wait for refresh of cube formulas
Application.Wait(Now + TimeValue("0:00:10"))
If Err.Number <> 0 Then
' handle error
Err.Clear
UpdateConnections = 0
Exit Function
End If
' update cache after Model refresh
For Each slc In ThisWorkbook.SlicerCaches
slc.ClearManualFilter
slc.ClearAllFilters
Next slc
' wait for refresh of cube formulas
Application.Calculate
Application.CalculateUntilAsyncQueriesDone
If Not Application.CalculationState = xlDone Then
DoEvents
Application.Wait(Now + TimeValue("0:00:01"))
End If
UpdateConnections = 1
End Function