I seem to have found a bug in Excel, again.
Apparently it is not possible to return an array from a user defined function containing strings longer than 255 chars if the array also contains numbers.
I have asked this question in http://www.experts-exchange.com/Applicatio…Q_20659869.html%5B/url%5D .
It was confirmed by several people, if you want to see this for yourself, use this little demo routine:
Function cseStrDemo(strToRepeat As String, blnRedimAsString As Boolean) 'Demo to show there is a 255 char limit unless 'mixed' array declared as Str 'Att: Array function, hence the the ctrl+shift+enter reminder ' Setup for a size of 25 rows and 3 columns Dim arrStr As Variant, i As Integer, strTemp As String If blnRedimAsString = True Then ReDim arrStr(1 To 25, 1 To 3) As String Else ReDim arrStr(1 To 25, 1 To 3) End If For i = 1 To 25 strTemp = strTemp & strToRepeat arrStr(i, 1) = i arrStr(i, 2) = Len(strTemp) arrStr(i, 3) = strTemp Next i cseStrDemo = arrStr End Function
The work-around I (finally) came up with, using an array of arrays, does not work if I want to return the arrays column-wise.
I do not know what I am doing wrong (other than using Excel). The MSKB does not show anything other than returning
arrays to objects, an internet seach did not help either. And the Search here is not possible, so I might asking about
a known problem. Does anybody see a solution here or is this hopeless ?
Function cseStrDemo2(strToRepeat As String) 'Work-around for the 255 char limit for 'mixed' arrays 'Attention: Array function, hence the ctrl+shift+enter prefix as reminder ' The columnwise return does not work, why ? ' Using Transpose will *CRASH* Excel 97 if resulting string > 255 chars Dim arrArray As Variant, arr1 As Variant, arr2 As Variant, arr3 As Variant Dim i As Integer, strTemp As String ReDim arrArray(1 To 3) As Variant ReDim arr1(1 To 25) As Integer ReDim arr2(1 To 25) As Long ReDim arr3(1 To 25) As String For i = 1 To 25 strTemp = strTemp & strToRepeat arr1(i) = i arr2(i) = Len(strTemp) arr3(i) = strTemp Next i arrArray(1) = arr1 arrArray(2) = arr2 arrArray(3) = arr3 'Return values cseStrDemo2 = arrArray End Function