-
Notifications
You must be signed in to change notification settings - Fork 21
/
LibPQ_ThisWorkbook.bas
89 lines (70 loc) · 2.27 KB
/
LibPQ_ThisWorkbook.bas
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
Attribute VB_Name = "LibPQ_ThisWorkbook"
Option Explicit
'
' This module allows you to access metadata about current workbook from Power
' Query.
'
' To create the record with neccessary data, run `LibPQ_UpdateThisWorkbook`
'
' To update workbook information automatically call that macro from
' `Workbook_Open` event
'
Public Sub LibPQ_UpdateThisWorkbook()
' Update ThisWorkbook query with information about current workbook
Const Query As String = "ThisWorkbook"
Call ReplaceQuery(Query, ThisWorkbookData())
End Sub
Private Function ThisWorkbookData() As String
' Gather information about current workbook and present it as M record
Dim Fields As New Collection
Dim Values As New Collection
Dim WB As Excel.Workbook
Set WB = ThisWorkbook
Fields.Add "Directory"
Values.Add WB.Path
Fields.Add "FullPath"
Values.Add WB.FullName
Fields.Add "Filename"
Values.Add WB.Name
ThisWorkbookData = MakeRecord(Fields, Values)
End Function
Private Function ReplaceQuery(Name As String, Code As String)
' Replace the code of the PQ Query
Dim CurrentWorkbook As Excel.Workbook
Dim Query As Excel.WorkbookQuery
Set CurrentWorkbook = ThisWorkbook
Set Query = Nothing
On Error Resume Next
Set Query = CurrentWorkbook.Queries(Name)
On Error GoTo 0
If Query Is Nothing Then
Set Query = CurrentWorkbook.Queries.Add(Name, Code)
Else
Query.Formula = Code
End If
Set ReplaceQuery = Query
End Function
Private Function MakeRecord(Keys, Values) As String
' Translate collections (or arrays) of keys and values into Power Query M Language record
Dim i
Dim IndexStart As Long
Dim IndexEnd As Long
IndexStart = -1
IndexEnd = -1
On Error Resume Next
' try array
IndexStart = LBound(Keys)
IndexEnd = UBound(Keys)
On Error GoTo 0
If IndexStart = -1 Or IndexEnd = -1 Then
' if not array then it's a collection
IndexStart = 1
IndexEnd = Keys.Count
End If
MakeRecord = "["
For i = IndexStart To IndexEnd
MakeRecord = MakeRecord & vbCrLf & " " & CStr(Keys(i)) & " = " & """" & CStr(Values(i)) & """"
If i <> IndexEnd Then MakeRecord = MakeRecord & ","
Next
MakeRecord = MakeRecord & vbCrLf & "]"
End Function