-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCommonUtils.bas
52 lines (48 loc) · 1.43 KB
/
CommonUtils.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
Attribute VB_Name = "CommonUtils"
Option Explicit
'Description: collection of functions to be used in various VBA projects
'@Method Incr - incriments number by one and return it
Public Function Incr(ByRef num As Long) As Long
num = num + 1
Incr = num
End Function
'@Method Decr - decriments number by one and return it
Public Function Decr(ByRef num As Long) As Long
num = num - 1
Decr = num
End Function
'Method ToBool - force convertion to Boolean
Public Function ToBool(ByRef val As Variant) As Boolean
On Error GoTo CatchError
Select Case VarType(val)
Case vbNull
ToBool = Not IsNull(val)
Case vbString
ToBool = (val <> vbNullString)
Case vbObject
ToBool = Not val Is Nothing
Case Is > vbArray
On Error Resume Next
ToBool = UBound(val) > 0
If Err.Number = 9 Then ToBool = False
Case vbBoolean
ToBool = val
Case Else
ToBool = CBool(val)
End Select
Exit Function
CatchError:
Err.Raise Err.Number, Err.Source, Err.Description
End Function
'Method StringFormat - replace placeholders in input text with parameters values
Function StringFormat(ByVal text As String, ParamArray vals() As Variant)
Dim str As Variant, i As Long
On Error GoTo CatchError
For Each str In vals
i = i + 1
text = Replace(text, "%" & i, str)
Next str
StringFormat = text
CatchError:
If Err.Number <> 0 Then Err.Raise Err.Number, "Utils.StringFormat", Err.Description
End Function