I would like to create printing function which prints entire array or part of it in immediate window. First I have function which determines count of array dimensions. Second I have function which determines longest string in column. Third I want to test these functions, but the test gives me error. The problem is that function 1 is not evaluated in function 2. Is it possible to somehow nest functions? It would make my code much more readable.
1. Determines number of dimmensions
Public Function NDim(Arr As Variant) As Integer
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'returns the number of dimensions of an array.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim Ndx As Integer
Dim Res As Integer
On Error Resume Next
Do
Ndx = Ndx + 1
Res = UBound(Arr, Ndx)
Loop Until Err.Number <> 0
NDim = Ndx
End Function
2. find the longest string array column
Public Function findLong(x() As Variant) As Integer
If IsArray(x) = True Then
nd = NDim(x) ''' determines dimension count
If nd > 1 Then
ReDim ald(nd) As Integer ''' creates one dimensional array to which string length 'ld' of the longest string from array ar2 column is stored. (max length for each column)
For D = 1 To nd
ld = 0
For I = LBound(x, D) To UBound(x, D)
If Len(x(I, D)) > ld Then ld = Len(x(I, D))
Next I
ald(D) = ld
Next D
End If
End If
findLong = ald
End Function
3. Calling function
Sub testr()
Dim ar2(10, 10) As String
ar2(4, 1) = 1
ar2(4, 2) = 2
ar2(5, 1) = 15
ar2(5, 2) = 25
Dim a() As Integer
a = findLong(ar2)
debug.Print a(1) ''' results in error
End Sub
Bookmarks