+ Reply to Thread
Results 1 to 5 of 5

split the code while argument is not affected

Hybrid View

  1. #1
    Registered User
    Join Date
    02-23-2013
    Location
    earth
    MS-Off Ver
    Excel 2010
    Posts
    21

    split the code while argument is not affected

    try to develop user-define function named abc(),due to the error "procedure too large" , i was forced to split the code and now the function return 0 instead expected result "3"

    Function abc(op1 As Double, op2 As Double, _
    Figure_1 As Long, Figure_2 As Long)
    
    Dim p1 As Variant, p2 As Variant, p As Variant
    
    op1 = 100 'input for user define function
    op2 = 100 'input for user define function
    Figure_1 = 1 'input for user define function
    Figure_2 = 2 'input for user define function
    
    abc = p1 + p2
    
    End Function
    ----------------------------------------------------------
    Private Function index100(op1 As Double, op2 As Double, _
     Figure_1 As Long, Figure_2 As Long)
    
    Dim p1 As Variant, p2 As Variant, p As Variant
    
    
    If op1 >= 100 Or op2 >= 100 Then
    p = Evaluate(" (" & Figure_1 & ")+(" & Figure_2 & ") ")
    If op1 >= 100 Then p1 = p
    If op2 >= 100 Then p2 = p
    End If
    
    
    End Function
    ----------------------------------------------------------
    Private Function index200(op1 As Double, op2 As Double, _
     Figure_1 As Long, Figure_2 As Long)
    
    Dim p1 As Variant, p2 As Variant, p As Variant
    
    
    If op1 >= 200 Or op2 >= 200 Then
    p = Evaluate(" (" & Figure_1 & ")+(" & Figure_2 & ") ")
    If op1 >= 200 Then p1 = p
    If op2 >= 200 Then p2 = p
    End If
    
    
    End Function

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: split the code while argument is not affected

    Your functions are not connected to each other, nor are they currently open to feeding parameters one to another.

    Function abc(op1 As Double, op2 As Double, _
    Figure_1 As Long, Figure_2 As Long)
    
    Dim p1 As Variant, p2 As Variant, p As Variant
    
    op1 = 100 'input for user define function
    op2 = 100 'input for user define function
    Figure_1 = 1 'input for user define function
    Figure_2 = 2 'input for user define function
    
    abc = p1 + p2
    
    End Function
    In this function you are setting abc = to p1 +p2, but in the code above that in the function there is no code to give values to P1 or P2. So you are adding 0 + 0. You'll need to use INDEX100 to resolve p1 & P2 first.

    I've merged that into one function that would appear to do the correct thing:
    Function abc(op1 As Double, op2 As Double, Figure_1 As Long, Figure_2 As Long)
    
    op1 = 100 'input for user define function
    op2 = 100 'input for user define function
    Figure_1 = 1 'input for user define function
    Figure_2 = 2 'input for user define function
    
    If op1 >= 100 Or op2 >= 100 Then abc = Figure_1 + Figure_2
    
    End Function
    I'm assuming the lines in red will actually be removed and you will use the function to feed in the 4 parameters via cell reference.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-23-2013
    Location
    earth
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: split the code while argument is not affected

    Quote Originally Posted by JBeaucaire View Post
    Your functions are not connected to each other, nor are they currently open to feeding parameters one to another.
    that what i am looking for, how to make the code connected to each other or feeding parameters to another???

    the code you merged for me is not what i need,because i had lots of calculation in my code under one function which causing error"procedure too large" and now i forced to split the code.

    yeah,the line in red will remove and will feed the 4 parameters via cell reference

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: split the code while argument is not affected

    How long was the procedure too large?

    When creating a function, the parameters in the () denote the values that will be used in the function. When calling that function from another function, be sure to insert those parameters into the call, the same way you fill out a formula's parameters as you enter it into a cell.


    In your original code above, the INDEX100 could be used to bring back a single result. But it looks like you need results to resolve the values of P1 and P2. I would suggest creating functions for those.

    Here's a very simple example of what I mean. Here I've created a GetP1 and a GetP2 function, within those functions you can do whatever routines you need, but the end result is a single value fed back as the result of GetP1. In the main macro, that result is stored into a variable and used as needed.

    Option Explicit
    
    Function abc(op1 As Double, op2 As Double, Figure_1 As Long, Figure_2 As Long)
    Dim p1 As Long, p2 As Long, p As Long
    
    p1 = GetP1(op1, Figure_1, Figure_2)
    p2 = GetP2(op2, Figure_1, Figure_2)
    
    abc = p1 + p2
    
    End Function
    
    Private Function GetP1(Op1 As Double, Figure_1 As Long, Figure_2 As Long) As Long
    
        If op1 >= 300 Then
            GetP1 = Figure_1 + Figure_2
        ElseIf op1 >= 200 Then
            GetP1 = Figure_1
        ElseIf op1 >= 100 Then
            GetP1 = Figure_2
        Else
            GetP1 = Figure_1 - Figure_2
        End If
    
    End Function
    
    Private Function GetP2(Op2 As Double, Figure_1 As Long, Figure_2 As Long) As Long
    
        If op2 >= 300 Then
            GetP2 = Figure_1 + Figure_2
        ElseIf op2 >= 200 Then
            GetP2 = Figure_1
        ElseIf op2 >= 100 Then
            GetP1 = Figure_2 * 3
        Else
            GetP2 = Figure_1 / Figure_2
        End If
    
    End Function
    Last edited by JBeaucaire; 05-14-2013 at 01:09 PM.

  5. #5
    Registered User
    Join Date
    02-23-2013
    Location
    earth
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: split the code while argument is not affected

    my code is over 64 kb in a single function without code splitting,that why the error pop out.

    that what i am looking for,thank you very much.
    Last edited by ck_123; 05-15-2013 at 09:56 AM.

+ 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