-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathVBALib_FormulaFunctions.bas
152 lines (137 loc) · 4.94 KB
/
VBALib_FormulaFunctions.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
Attribute VB_Name = "VBALib_FormulaFunctions"
' Common VBA Library - FormulaFunctions
' Provides functions that are useful in Excel formulas.
Option Explicit
' Retrieves the given element of an array.
Public Function ArrayElement(arr As Variant, i1 As Variant, _
Optional i2 As Variant, Optional i3 As Variant, _
Optional i4 As Variant, Optional i5 As Variant) As Variant
If IsMissing(i2) Then
If IsObject(arr(i1)) Then
Set ArrayElement = arr(i1)
Else
ArrayElement = arr(i1)
End If
ElseIf IsMissing(i3) Then
If IsObject(arr(i1, i2)) Then
Set ArrayElement = arr(i1, i2)
Else
ArrayElement = arr(i1, i2)
End If
ElseIf IsMissing(i4) Then
If IsObject(arr(i1, i2, i3)) Then
Set ArrayElement = arr(i1, i2, i3)
Else
ArrayElement = arr(i1, i2, i3)
End If
ElseIf IsMissing(i5) Then
If IsObject(arr(i1, i2, i3, i4)) Then
Set ArrayElement = arr(i1, i2, i3, i4)
Else
ArrayElement = arr(i1, i2, i3, i4)
End If
Else
If IsObject(arr(i1, i2, i3, i4, i5)) Then
Set ArrayElement = arr(i1, i2, i3, i4, i5)
Else
ArrayElement = arr(i1, i2, i3, i4, i5)
End If
End If
End Function
' Splits a string into an array, optionally limiting the number
' of items in the returned array.
Public Function StringSplit(s As String, delim As String, _
Optional limit As Long = -1) As String()
StringSplit = Split(s, delim, limit)
End Function
' Joins an array into a string by inserting the given delimiter in between
' items.
Public Function StringJoin(arr() As Variant, delim As String) As String
StringJoin = Join(arr, delim)
End Function
' Returns a newline (vbLf) character for use in formulas.
Public Function NewLine() As String
NewLine = vbLf
End Function
' Returns an array suitable for using in an array formula. When this
' function is called from an array formula, it will detect whether or
' not the array should be transposed to fit into the range.
Public Function GetArrayForFormula(arr As Variant) As Variant
If IsObject(Application.Caller) Then
Dim len1 As Long, len2 As Long
Select Case ArrayRank(arr)
Case 0
GetArrayForFormula = Empty
Exit Function
Case 1
len1 = ArrayLen(arr)
len2 = 1
Case 2
len1 = ArrayLen(arr)
len2 = ArrayLen(arr, 2)
Case Else
Err.Raise 32000, Description:= _
"Invalid number of dimensions (" & ArrayRank(arr) _
& "; expected 1 or 2)."
End Select
If Application.Caller.Rows.Count > Application.Caller.Columns.Count _
And len1 > len2 Then
GetArrayForFormula = WorksheetFunction.Transpose(arr)
Else
GetArrayForFormula = arr
End If
Else
GetArrayForFormula = arr
End If
End Function
' Converts a range to a normalized array.
Public Function RangeToArray(r As Range) As Variant()
If r.Cells.Count = 1 Then
RangeToArray = Array(r.Value)
ElseIf r.Rows.Count = 1 Or r.Columns.Count = 1 Then
RangeToArray = NormalizeArray(r.Value)
Else
RangeToArray = r.Value
End If
End Function
' Returns the width of a column on a sheet. If the column number is
' not given and this function is used in a formula, then it returns
' the column width of the cell containing the formula.
Public Function ColumnWidth(Optional c As Integer = 0) As Variant
Application.Volatile
Dim s As Worksheet
If IsObject(Application.Caller) Then
Set s = Application.Caller.Worksheet
Else
Set s = ActiveSheet
End If
If c <= 0 And IsObject(Application.Caller) Then
c = Application.Caller.Column
End If
ColumnWidth = s.Columns(c).Width
End Function
' Returns the height of a row on a sheet. If the row number is
' not given and this function is used in a formula, then it returns
' the row height of the cell containing the formula.
Public Function RowHeight(Optional r As Integer = 0) As Variant
Application.Volatile
Dim s As Worksheet
If IsObject(Application.Caller) Then
Set s = Application.Caller.Worksheet
Else
Set s = ActiveSheet
End If
If r <= 0 And IsObject(Application.Caller) Then
r = Application.Caller.Row
End If
RowHeight = s.Rows(r).Height
End Function
' Returns the formula of the given cell or range, optionally in R1C1 style.
Public Function GetFormula(r As Range, Optional r1c1 As Boolean = False) _
As Variant
If r1c1 Then
GetFormula = r.FormulaR1C1
Else
GetFormula = r.Formula
End If
End Function