Results 1 to 4 of 4

Calling function inside function. (aka nested function)

Threaded View

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

    Calling function inside function. (aka nested function)

    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
    Last edited by jakopak; 11-02-2015 at 05:52 AM. Reason: Clarification and corrected typo

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 11
    Last Post: 06-01-2015, 02:21 AM
  2. [SOLVED] Help Requested with Nested Small Function Inside Index/Match Formula
    By trandle in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-26-2015, 10:07 AM
  3. Calling Procedure or Function inside another Procedure - variables problem
    By Rudo123 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-22-2015, 03:12 PM
  4. Replies: 5
    Last Post: 09-15-2014, 12:41 AM
  5. [SOLVED] Exclude Zero's from Min Function nested inside an index+Match... all within CELL funtion
    By clemsoncooz in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 08-05-2014, 05:16 PM
  6. Replies: 0
    Last Post: 01-19-2013, 01:35 PM
  7. [SOLVED] RIGHT Function nested inside of IF
    By jakeisbill in forum Excel General
    Replies: 6
    Last Post: 10-16-2012, 12:01 PM

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