+ Reply to Thread
Results 1 to 4 of 4

Call a Function

  1. #1
    Forum Contributor
    Join Date
    11-20-2005
    Posts
    256

    Call a Function

    Hi all,

    I can call a Macro like

    'Call TestCode
    or
    'Run TestCode
    or
    TestCode
    or use Select Case
    etc

    If i have a function named TestCode why can i not call it in this way?
    Thx
    Dave
    "The game is afoot Watson"

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Desert

    You can call the function the same way unless you are expecting the function to return data back to your macro or if you are passing variable data onto the function

    Sub Macro1()
    call Function1
    End Sub

    Function MyFunction1()
    function code here
    End Function

    or
    Sub Macro2()
    dim MyVariable As String
    MyVariable = MyFunction2
    End Sub

    Function MyFunction2() as String
    MyFunction2 = "Hi"
    End Function

    or

    Sub Macro3()
    dim MyVariable As Integer
    MyVariable = MyFunction3(4)
    End Sub

    Function MyFunction3(iVariable As Integer) as Integer
    MyFunction3 = iVariable + 6
    End Function

    or

    If the function is on a worksheet module then you will need to use the full sheet name as well as the function name

    eg
    call Sheet1.Function1

  3. #3
    Norman Jones
    Guest

    Re: Call a Function

    Hi Dave,

    > If i have a function named TestCode why can i not call it in this way?


    You can. However, if the function returns a value, you may not observe any
    response.

    For demonstaryion purposes, I include a msgbox in the function:


    Try:

    '=============>>
    Public Function ABC(aVal As Double)
    ABC = aVal * 2
    MsgBox ABC
    End Function
    '<<=============

    '=============>>
    Sub TestIt()
    ABC (5)
    Call ABC(100)
    End Sub
    '<<=============


    ---
    Regards,
    Norman



    "Desert Piranha"
    <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hi all,
    >
    > I can call a Macro like
    >
    > 'Call TestCode
    > or
    > 'Run TestCode
    > or
    > TestCode
    > or use Select Case
    > etc
    >
    > If i have a function named TestCode why can i not call it in this way?
    >
    >
    > --
    > Desert Piranha
    >
    >
    > ------------------------------------------------------------------------
    > Desert Piranha's Profile:
    > http://www.excelforum.com/member.php...o&userid=28934
    > View this thread: http://www.excelforum.com/showthread...hreadid=522044
    >




  4. #4
    Forum Contributor
    Join Date
    11-20-2005
    Posts
    256
    Hi Mudraker & Norman,

    Thank You both for contributing to this.

    This is over my head. Im quiting on this for now, as i can't seem to work with it.
    Its for personal use, and i have some stuff to do for work.
    I will keep your stuff for future.

    Thx again Norman and Mudraker

+ 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