+ Reply to Thread
Results 1 to 4 of 4

Calling function inside function. (aka nested function)

Hybrid 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

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Calling function inside function. (aka nested function)

    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 = N
    End Function
    What is N? This looks like it will always return the same value.


    In findLong, What is ar2? similarly ald?
    Public Function findLong(x() As Variant) As Integer
    
    If IsArray(x) = True Then
       nd = NDim(ar2) ''' determines dimension count
        If nd > 1 Then
            ReDim ald(nd) As Integer 
            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
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Calling function inside function. (aka nested function)

    You aren't dimensioning your variables and are using them outside of the procedure that they are scoped.
    Option Explicit will require all variables to be dimensioned and in scope.

    It looks like you might be using variable ar2 within findLong, when the array passed is x. (ar2 is an array scoped only to the Sub testr)

    I still don't see where N gets its value in NDim, or what ald is set in findLong.

    https://msdn.microsoft.com/en-us/library/y9341s4f.aspx
    Last edited by mikerickson; 11-01-2015 at 11:49 AM.

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

    Re: Calling function inside function. (aka nested function)

    Mikericson, I have corrected the typos in my code (Ndim=Ndx; nd = NDim(x)). Still It doesn't do what I would like to. Nesting vba function will greatly improve readability of my code. Otherwise I have to use very long function with many parts.

    -ald() creates one dimensional array to which string length 'ld' of the longest string from array 'ar2' column is stored. (maximum length for each column)
    Last edited by jakopak; 11-03-2015 at 03:14 AM.

+ Reply to Thread

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