-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.bas
189 lines (136 loc) · 5.05 KB
/
utils.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
' Miscellaneous functions and subs to make VBA a bit less painful
' Note: requires "Microsoft Scripting Runtime" reference to be selected
Attribute VB_Name = "utils"
Option Base 0
Option Explicit
Function GetFP()
' returns the path of the file selected using a dialog box
Dim intChoice As Variant
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
intChoice = Application.FileDialog(msoFileDialogOpen).Show
If intChoice <> 0 Then
GetFP = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)
Else
MsgBox "No File Selected."
GetFP = Empty
Exit Function
End If
End Function
Function sheetExists(sheetToFind As String, Optional wb As Workbook) As Boolean
' returns True or False if the sheet exists in a workbook
Dim sht As Variant
sheetExists = False
If wb Is Nothing Then Set wb = ThisWorkbook
' .Sheets used to include chart sheets
For Each sht In wb.Sheets
If sheetToFind = sht.Name Then
sheetExists = True
Exit Function
End If
Next sht
End Function
Function append(arr As Variant, item As Variant) As Variant
' function that returns the input array with the item appended at the end
' handles empty arrays, but assumes option base 0
On Error GoTo emptyarr
ReDim Preserve arr(UBound(arr) + 1) As Variant
arr(UBound(arr)) = item
append = arr
Exit Function
emptyarr:
On Error GoTo -1
ReDim arr(0) As Variant
arr(0) = item
append = arr
End Function
Function ColumnLetter(ColumnNumber As Long) As String
' Converts an integer column index to its string representation
' source: https://stackoverflow.com/a/15366979
Dim n As Long
Dim c As Byte
Dim s As String
n = ColumnNumber
Do
c = ((n - 1) Mod 26)
s = Chr(c + 65) & s
n = (n - c) \ 26
Loop While n > 0
ColumnLetter = Trim(s)
End Function
Public Function PosFormat(ParamArray arr() As Variant) As String
' Primitive "{0} {1}..."" style formatting using positions
' positions, starting from 0, are required
' https://stackoverflow.com/a/31730589
Dim i As Long
Dim temp As String
temp = CStr(arr(0))
For i = 1 To UBound(arr)
temp = Replace(temp, "{" & i - 1 & "}", CStr(arr(i)))
Next
PosFormat = temp
End Function
Public Function DictFormat(mystr As String, my_dict As Scripting.Dictionary) As String
' Primitive "{key0} {key1}..."" style formatting using labels instead of positions
' https://stackoverflow.com/a/31730589
Dim ctr As Variant
Dim temp As String
temp = mystr
For Each ctr In my_dict
temp = Replace(temp, "{" & ctr & "}", my_dict(ctr))
Next ctr
DictFormat = temp
End Function
Function parseR1C1(r1c1_ad)
' get the string address and returns an array for coordinates Array(row_ind, col_ind)
Dim temp
temp = Split(Right(r1c1_ad, Len(r1c1_ad) - 1), "C")
parseR1C1 = Array(CInt(temp(0)), CInt(temp(1)))
End Function
Sub deleteSheet(sht_name As String, Optional wb As Workbook)
' procedure deletes sheet if it exists with no prompt
Dim ada_setting As Boolean
ada_setting = Application.DisplayAlerts
Application.DisplayAlerts = False
If wb Is Nothing Then Set wb = ThisWorkbook
If sheetExists(sht_name, wb) Then
wb.Worksheets(sht_name).Delete
End If
Application.DisplayAlerts = ada_setting
End Sub
Public Sub Update_Module(strModuleName as String, Optional modulePath as String)
' Notes:
' - requires "Microsoft Visual Basic for Applications Extensibility" reference
' - Macro Settings must be set to "Enable all macros"; with "Trust access to the VBA project object model" checked
' sources:
' https://www.mrexcel.com/forum/excel-questions/150819-import-module-into-vba-using-vba-macro.html
' https://answers.microsoft.com/en-us/office/forum/office_2007-access/using-vba-to-check-if-a-module-exists/82483c2c-406b-4b2b-882f-96e4612ef6fb
Dim VBProj As Object
Dim myFileName As String
Dim mdl As Variant
if modulePath is Nothing Then modulePath = ActiveWorkbook.Path
myFileName = modulePath & "\" & strModuleName & ".bas"
Set VBProj = Nothing
On Error Resume Next
Set VBProj = ActiveWorkbook.VBProject
On Error GoTo 0
If VBProj Is Nothing Then
MsgBox "Update_Module FAILED! -- Workbook is probably not trusted!" & Chr(10) & "Please update module manually."
Exit Sub
End If
If Dir(myFileName, vbDirectory) = vbNullString Then
Msgbox myFileName & " does not exist!"
Exit Sub
End If
With VBProj
For Each mdl In .vbcomponents
If mdl.Name = strModuleName And mdl.Type <> vbext_ct_Document Then
.vbcomponents.Remove mdl
DoEvents
Exit For
End If
Next mdl
Application.StatusBar = "Importing " & myFileName
.vbcomponents.Import myFileName
Application.StatusBar = ""
End With
End Sub