+ Reply to Thread
Results 1 to 6 of 6

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

  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)

    Please Login or Register  to view this content.
    Last edited by tahi.laci; 05-28-2014 at 12:40 PM. Reason: solved

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

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

    Have you put the code in a module? Also try declaring as public sub, you need to put your costants at the top level also so available to complete module.

    Please Login or Register  to view this content.
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    The array GeneTable is out of scope in the function Result, so VBA is looking for a sub/function named GeneTable.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    04-16-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    52

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

    That explains the problem but offers no solution. If I just write public in front of the declarations I get another error message. I do intend all the arrays to work in the whole module, I don't know how to achieve that however.

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

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

    you need to put your costants at the top level also so available to complete module and your array. This is the solution.

    Const Chromolen = 5
    Const CrossRate = 0.7
    Const MutRate = 0.01
    Const poolSize = 40
    Const Solution = 42

    GeneTable = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "")

    Public Sub Basic_example()
    'others
    Dim i As Integer

  6. #6
    Registered User
    Join Date
    04-16-2013
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    52

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

    Thanks that helped. It does work if I declare my constants and arrays in the top level, however I got an error message when trying to initialize the array at the top level so I only declared it and filled it in the sub. By the time I had a working code I had made some other changes. (Now I use variables instead of constants, but I think it would work with constants as well)

    Please Login or Register  to view this content.

+ 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. 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