HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Ranking a variant array with variable dimensions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
variantwitharraydimensionsrankingvariable

Problem

I'm doing VBA macros and need to use Arrays of values extensively. After the below help from @TonyDallimore on Stack Overflow, I've decided to use nested variant arrays.

VBA chrashes when trying to execute generated sub

I use multidimensional, jagged arrays to SELECT data from DB's and efficiently perform calculations and write to a Worksheet. The data is sometimes a single value (Rank:0) sometimes a row of values (Rank:1) sometimes a table of values with some cells containing rows of values (Rank:3). I use the function below to determine what kind of operations are possible and should be performed to such arrays.

This function, along with all my array related functions reside in a module: modCustomArrayFunctions.

```
'*'
'Returns the rank of the passed array '
'Parameters: '
' Arr: The array to be processed '
'Returns: '
' The rank of the array '
'*'
Public Function Rank(ByRef Arr As Variant) As Byte
'Declarations **'
Dim MaxRank As Byte 'Maximum rank of the elements of the array '
Dim i As Integer
'*'
If IsArray(Arr) Then
If IsArrInitialized(Arr) Then
MaxRank = 0
For i = LBound(Arr) To UBound(Arr)
Rank = Rank(Arr(i)) + 1
If Rank > MaxRank Then MaxRank = Rank
Next i
Rank = MaxRank
Else
Rank = 0
End If
Else
Rank = 0
End If
End Function
'****

Solution

From above:you have a lesser problem, but a problem still: in VBA anything Public declared in a code module (.bas) is accessible as a "macro" - a Public Function in Excel VBA could even be used as a cell formula, so naming is very important to avoid clashes with existing/"native" functions.

To keep your internal public subs and functions from appearing in the user macro list, define them with an optional parameter, even though it is never used.

Public Sub YourName(Optional Dummy As Variant = Nothing)
    ' Your code goes here.
End Sub

Code Snippets

Public Sub YourName(Optional Dummy As Variant = Nothing)
    ' Your code goes here.
End Sub

Context

StackExchange Code Review Q#46174, answer score: 3

Revisions (0)

No revisions yet.