+ Reply to Thread
Results 1 to 12 of 12

Call Function by Name

  1. #1
    Registered User
    Join Date
    03-10-2005
    Posts
    9

    Call Function by Name

    Just wonder how to call the function if I have it's name in the variable String
    for example something like that:
    Please Login or Register  to view this content.

    maybe there are some other ways?
    Thank you for your help,
    arbus
    Last edited by arbus; 08-30-2007 at 09:56 AM.

  2. #2
    Forum Contributor
    Join Date
    12-12-2005
    Posts
    667
    I completed your example to illustrate it better:
    Please Login or Register  to view this content.
    Best regards,

    Ray

  3. #3
    Registered User
    Join Date
    03-10-2005
    Posts
    9
    Quote Originally Posted by raypayette
    I completed your example to illustrate it better:
    hi ray
    I am not sure that I understood you
    I used CallByName fucntion because there is such a fuction which executes a method of an object
    So I thought maybe it can be used for calling a function if u have its name saved in the string variable

    in my example I know that there is a function named "test" and I wanted to call that function
    Imagine u have a list of similar finctions: test1, test2, test3 etc
    you want to call them in the loop
    so inside the loop u will assign a string variable name="test"+cstr(i) and afterfards u want to call the function with name="test"+cstr(i)
    Last edited by arbus; 08-30-2007 at 09:59 AM.

  4. #4
    Forum Contributor
    Join Date
    12-12-2005
    Posts
    667
    I tried to work out the glitches. For example result = "test" will not use the function test, rather it defines it as the string test; I correct3ed it to use the Boolean function "test". Also you redefined name as a text when it was a Boolean value; I corrected it.

  5. #5
    Registered User
    Join Date
    03-10-2005
    Posts
    9
    any ideas?

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    Try this. In C1 enter the value xxx.

    Then run
    Please Login or Register  to view this content.
    The first result will be xxx which is the value in C1. The second answer will be $C$1 which is the address of C1.

    So unless your object has a method of test, then you cannot use this to call it.

    Does that help to make things clearer???


    rylo

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Maybe you mean

    Please Login or Register  to view this content.
    Or in the worksheet

    =test()
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  8. #8
    Registered User
    Join Date
    03-10-2005
    Posts
    9
    thats a brilliant idea, thanks!
    what I wanted - to be able in the loop to generate string variables with function names (functions in the module already exist)
    and afterwards to be able to call those functions (by their name stored in the string var) in the other module

    imagine - I have a list of test functions named test1(), test2() etc
    and I dont want to run all of them all the time
    there is a config txt file with names of the function to be run

    when macro is started it reads that file and has the list of names of the functions which I want to execute one by one

    so the problem now is to run the existing function with a specified name
    _________

    the only solution found (thanks to rylo and royUK) (maybe not very elegant but perfectly working) is to use a temporary cell and print in it "=test1()" (if function name is "test1")
    and to get the result of the function from that cell aftewards

    so far I dont see how it can be done inside vbe only without using excell cells as a temporary storage of the result
    Last edited by arbus; 08-31-2007 at 07:45 AM.

  9. #9
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Your example Function will be available whilst that workbook is open, because you actually set it to True. What is an example of a Function you ant to use?

  10. #10
    Registered User
    Join Date
    03-10-2005
    Posts
    9
    I didnt get ur question
    maybe the following code will help to understand the problem
    I would like not to use cells for storing the result but even as it is now - it works

    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    OK, same sort of thing as your example, but you don't have to use a cell to get the results

    Please Login or Register  to view this content.
    HTH

    rylo

  12. #12
    Registered User
    Join Date
    03-10-2005
    Posts
    9
    cool!
    thanks rylo

+ 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