Results 1 to 6 of 6

Code works in the macro but not in a separate function. "Sub or function not defined."

Threaded View

tahi.laci Code works in the macro but... 05-28-2014, 06:14 AM
nathansav Re: Code works in the macro... 05-28-2014, 06:55 AM
Norie The array GeneTable is out of... 05-28-2014, 07:10 AM
tahi.laci Re: Code works in the macro... 05-28-2014, 08:16 AM
nathansav Re: Code works in the macro... 05-28-2014, 08:38 AM
tahi.laci Re: Code works in the macro... 05-28-2014, 12:38 PM
  1. #1
    Registered User
    Join Date
    04-16-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    52

    Code works in the macro but not in a separate function. "Sub or function not defined."

    Basically I have a piece of code (5 lines commented out toward the end) that runs fine as part of the macro, however I wanted to use the exact same piece of code (only changing one number to a variable) run as a separate function, to make the code look tidier. However when I run that code as a separate function being called I get the error message. "Sub or function not defined."

    To see what the program is supposed to do uncomment the block starting with Dim Phenotype... and comment the the line Result(0).
    (The last few lines are the important bit, the rest are just declarations)

    Sub Basic_example()
    'Declarations
    'parameters
    GeneTable = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "")
    Const Chromolen = 5
    Const CrossRate = 0.7
    Const MutRate = 0.01
    Const poolSize = 40
    Const Solution = 42
    'others
    Dim i As Integer
    Dim k As Integer
    Dim Pool(poolSize, Chromolen) As Integer
    Dim Fitness(poolSize) As Double
    Dim GeneNumber As Integer: GeneNumber = UBound(GeneTable)
    
    'fill with random population
    For i = 0 To poolSize
    For k = 0 To Chromolen
    Pool(i, k) = Int(Rnd() * GeneNumber)
    Next
    Next
    
    
    
    'Dim Phenotype As String: Phenotype = vbNullString
    'For y = 0 To Chromolen
    'Phenotype = Phenotype & GeneTable(Pool(0, y))
    'Next
    'MsgBox Phenotype
    
    Result (0)
    
    End Sub
    
    Function Result(x)
    Dim Phenotype As String: Phenotype = vbNullString
    For y = 0 To Chromolen
    Phenotype = Phenotype & GeneTable(Pool(x, y))
    Next
    MsgBox Phenotype
    End Function
    Last edited by tahi.laci; 05-28-2014 at 12:40 PM. Reason: solved

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Match function works while the "=" says True! Driving me crazy!
    By tony.nz in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-21-2014, 02:17 PM
  2. Code to disable print function and "email as attachment" function
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-31-2013, 01:59 PM
  3. Replies: 3
    Last Post: 04-02-2013, 08:41 AM
  4. [SOLVED] Using the MOD function in a "User" defined macro
    By Vba Novice in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-11-2013, 04:20 PM
  5. Trouble with "Excel 4.0 function stored in defined names" error
    By rkjudy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2011, 01:54 PM

Tags for this Thread

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