Results 1 to 9 of 9

Function which defines n-dimensional array, function inputs as variable.

Threaded View

  1. #4
    Forum Contributor
    Join Date
    02-05-2015
    Location
    czech
    MS-Off Ver
    2010
    Posts
    172

    Re: Function which defines n-dimensional array, function inputs as variable.

    Quote Originally Posted by xlnitwit View Post
    Will you ever have more than 3 dimensions?
    Yes I would like to have that support and support for freely swaping, deleting (row, columns,elements...), combining, reshaping arrays.

    Two ways for dynamical creation of n-dimensional array
    1. Using VBA Project object model
    2. Very long ifs structure like bellow:

    Function narr(str As String)
    '''''''''''''''''''''''''''''''''''''''
    ' Declare n-dimensional array
    '''''''''''''''''''''''''''''''''''''''
    Dim barr() As String
    Dim rarr() As Variant 'resulting array
    Dim nd As Long
    barr = Split(str, ",")
    nd = size(barr) 'number of elements in splited array (perhaps there is native function which counts all elements of an array)
        If nd = 0 Then
            narr = rarr
        End If
        
        If nd = 1 Then
            ReDim rarr(barr(0))
        ElseIf nd = 2 Then
            ReDim rarr(barr(0), barr(1))
        ElseIf nd = 3 Then
            ReDim rarr(barr(0), barr(1), barr(2))
        End If
    narr = rarr
    End Function
    
    Sub tnarr()
    '''''''''''''''''''''''''''''''''''''''
    ' test function narr
    '''''''''''''''''''''''''''''''''''''''
    Dim sr As String
    Dim rar() As Variant
    sr = "10,10,20"
    rar = narr(sr)
    End Sub
    
    Function size(arr As Variant)
    '''''''''''''''''''''''''''''''''''''
    ' Get number of elements in array
    '''''''''''''''''''''''''''''''''''''
    ' using bounds may (should) be faster
    Dim cl As Variant
    Dim i As Long
        For Each cl In arr
            i = i + 1
        Next cl
    size = i
    End Function

    I found some ways how to implement array manipulation, none of which is pretty.
    1. Using loops for copying one by one to another array (usable but slow)
    2. Using sheet ranges (extremely slow)
    3. Using Join and Split, for each dimension different separator (works only on String arrays, probably slow too)
    4. Using nested arrays like Array(Array(1, 1, 1), Array(1, 1, 1)).
    This is probably dead end, since Application.Evaluate("Array(Array(1, 1, 1), Array(1, 1, 1))(1)") yields Error 2029
    5. Perhaps user defined Class?
    6. Using external dll VarPtr http://stackoverflow.com/questions/7...tain-value-vba

    Option 6. Is interesting, though there may be problem on systems where particular dll is missing and with restricted user rights. That is big drawback. Perhaps I can reference dll outside system folders or bundle all my modules and dlls in one file?
    Last edited by jakopak; 11-12-2016 at 07:13 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Returning Two Dimensional Array from a Function
    By mahmud1280 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 05-18-2015, 12:50 PM
  2. Using variable inputs within an array.
    By Doug24 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-05-2014, 04:28 PM
  3. WorkSheet Function with Multi Dimensional Array
    By lopiner in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-04-2010, 02:54 PM
  4. Insert an array in a 1-dimensional function
    By Numerator in forum Excel General
    Replies: 2
    Last Post: 11-13-2010, 07:46 PM
  5. Enter variable into popup box, variable defines cell
    By invisible777 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2007, 06:31 PM
  6. [SOLVED] Using Array variable in FIND function
    By GreenInIowa in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-01-2005, 04:09 PM
  7. 2 dimensional array and freq function?
    By dabith in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-06-2005, 04:56 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1