+ Reply to Thread
Results 1 to 11 of 11

user defined function works in VBA but does not work inside worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    06-21-2008
    Posts
    8

    user defined function works in VBA but does not work inside worksheets

    Hi,
    i have written a code in VBA to interpolate the value of Y0 corresponding to X0 using a set of (X,Y) points. (I have written this in module1) . This function works when i call it through a Sub or another Function in VBA. but it does not work when I try to use it as a function in my excel worksheets (when I type : =interpolate(A1:A10,B1:B10,30) .... 30 is an arbitrary value). in this case I get #value! error

    The function is known in within the worksheets because when I start typing its name, the Auto Name Complete feature of excel, finds this function.

    I am new to VAB for excel. Please give me a hint to see my mistakes of if something is missing inside my code.

    The code is :

    PHP Code: 
    Public Function Interpolate(ByRef X() As DoubleByRef Y() As DoubleByRef X0 As Double) As Double
        Dim I 
    As IntegerSlope As DoubleNData As Integer
        
        NData 
    UBound(X)
        For 
    1 To UBound(X) - 1
            
    If (X(I) = X0Then
                Interpolate 
    Y(I)
                Exit Function
            ElseIf (
    X0 ListMax(X(I), X(1)) And X0 ListMin(X(I), X(1))) Then
                Slope 
    = (Y(I) - Y(1)) / (X(I) - X(1))
                
    Interpolate Y(1) + Slope * (X0 X(1))
                Exit Function
            
    End If
        
    Next I
            
    End 
    Function

    Public Function 
    ListMax(ParamArray ListItems() As Variant)

            
    Dim I As Integer
            ListMax 
    ListItems(0)
            For 
    0 To UBound(ListItems())
                If 
    ListItems(I) > ListMax Then ListMax ListItems(I)
            
    Next I

    End 
    Function

    Public Function 
    ListMin(ParamArray ListItems() As Variant)

            
    Dim I As Integer
            ListMin 
    ListItems(0)
            For 
    0 To UBound(ListItems())
                If 
    ListItems(I) < ListMin Then ListMin ListItems(I)
            
    Next I
    End 
    Function 


    I appreciate your help. Thanks
    Last edited by HabibValil; 06-21-2008 at 05:31 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,444
    What does the code look like when you use the function in code?

    Can you post some test data with the expected result.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    06-21-2008
    Posts
    8
    I didnt understand your first question, but abut your latter question, some test data are as:

    Column A column B

    1 10
    2 20

    If my X0 is 1.5, I expect 15 as the result of linear interpolation. The function is able to calculate it correctly (in VAB) but in excel I get error as I said.

    In fact the spreadsheet realizez that there is a function named INTERPOLATE, but it can not call it properly (or its better to say that I can not call it properly)

  4. #4
    Registered User
    Join Date
    06-21-2008
    Posts
    8
    I didnt understand your first question, but abut your latter question (I have copied and pasted the code in my previous post), some test data are as:

    Column A....column B

    1...............10
    2...............20

    If my X0 is 1.5, I expect 15 as the result of linear interpolation. The function is able to calculate it correctly (in VAB) but in excel I get error as I said.

    In fact the spreadsheet realizes that there is a function named INTERPOLATE, but it can not call it properly (or its better to say that I can not call it properly)
    Last edited by HabibValil; 06-21-2008 at 07:05 AM.

  5. #5
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,444
    I assume when testing your code in VBA you used something like this.

    Sub x()
    
        Dim dblX(1 To 2) As Double
        Dim dblY(1 To 2) As Double
        
        dblX(1) = 1
        dblX(2) = 2
        dblY(1) = 10
        dblY(2) = 20
        
        MsgBox Interpolate(dblX, dblY, 1.5)
        
    End Sub
    Which passes arrays declared as double into the routine.

    Your problem is your are not passing arrays declared as double when using

    =interpolate(A1,A2,B1,B2,1.5)

    You need to adapt your routine to handle ranges or variants.

    Is your routine different to the TREND function?

    =TREND(B1:B2,A1:A2,1.5)

  6. #6
    Registered User
    Join Date
    06-21-2008
    Posts
    8
    Yes Andy I exactly defined my variable as double. and my Sub is something similar to what uv written. what can I do now?

  7. #7
    Registered User
    Join Date
    06-21-2008
    Posts
    8
    I just realized that I have a problem with Ubound(X) . This is the command that gives me the error. for testing this idea I wrote this function:

    PHP Code: 
    Public Function neww(X) As Double
    neww 
    UBound(X)
    End Function 
    and this gives me #value error. This happened when I called the function through wpeadsheet and X is a range:
    X
    1
    2
    3

    What is not correctly set?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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