+ Reply to Thread
Results 1 to 7 of 7

Newbie problem :(

  1. #1
    Registered User
    Join Date
    08-15-2005
    Posts
    4

    Question Newbie problem :(

    Hello people,

    im new to using VBA, and im having a rather stupid problem. I am trying to write a function that i could use inside the worksheet, e.g. A1 =Foo(). I tried
    Please Login or Register  to view this content.
    and several variations on that theme, but the only effect i'm getting is #NAME errors. Does a function have to be registered in some way before being used on a sheet? or do i have to use CALL in some way?

    Thanks for any help,

    Liosan

  2. #2
    Forum Expert dominicb's Avatar
    Join Date
    01-25-2005
    Location
    Lancashire, England
    MS-Off Ver
    MS Office 2000, 2003, 2007 & 2016 365
    Posts
    4,867

    Smile

    Good afternoon Liosan

    No, nothing like that. The code you provided returned 42 (the secret of the Universe, apparently) without problem. Where are you placing the code? If it is placed in a blank module within the VBE (Insert > Module) the formula =Foo() should work fine. Try it again and if you still have a problem post back and I'll upload a spreadsheet containing your code.

    HTH

    DominicB

  3. #3
    Registered User
    Join Date
    08-15-2005
    Posts
    4

    Talking

    Thanks dominicb, I just dug it up in some tutorial 2 minutes ago That seems to solve most of my VBA problems for the time.

    Thanks for the help,

    Liosan

  4. #4
    K Dales
    Guest

    RE: Newbie problem :(

    For newbies, no "stupid" questions - we all had to learn!
    You need to make the Function Public (and put it in a code module you
    insert, not in ThisWorkbook or the worksheet's code module):

    Public Function Foo()
    Foo = 42
    End Function

    Functions can only be used within their own code module unless you make them
    Public.
    --
    - K Dales


    "Liosan" wrote:

    >
    > Hello people,
    >
    > im new to using VBA, and im having a rather stupid problem. I am trying
    > to write a function that i could use inside the worksheet, e.g. A1
    > =Foo(). I tried
    > Code:
    > --------------------
    > Function Foo()
    > Foo = 42
    > End Function
    > --------------------
    > and several variations on that theme, but the only effect i'm getting
    > is #NAME errors. Does a function have to be registered in some way
    > before being used on a sheet? or do i have to use CALL in some way?
    >
    > Thanks for any help,
    >
    > Liosan
    >
    >
    > --
    > Liosan
    > ------------------------------------------------------------------------
    > Liosan's Profile: http://www.excelforum.com/member.php...o&userid=26296
    > View this thread: http://www.excelforum.com/showthread...hreadid=395765
    >
    >


  5. #5
    Tom Ogilvy
    Guest

    Re: Newbie problem :(

    Put your function in a General module (insert module in the VBE) rather than
    a sheet module or the Thisworkbook module.

    --
    Regards,
    Tom Ogilvy


    "Liosan" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Hello people,
    >
    > im new to using VBA, and im having a rather stupid problem. I am trying
    > to write a function that i could use inside the worksheet, e.g. A1
    > =Foo(). I tried
    > Code:
    > --------------------
    > Function Foo()
    > Foo = 42
    > End Function
    > --------------------
    > and several variations on that theme, but the only effect i'm getting
    > is #NAME errors. Does a function have to be registered in some way
    > before being used on a sheet? or do i have to use CALL in some way?
    >
    > Thanks for any help,
    >
    > Liosan
    >
    >
    > --
    > Liosan
    > ------------------------------------------------------------------------
    > Liosan's Profile:

    http://www.excelforum.com/member.php...o&userid=26296
    > View this thread: http://www.excelforum.com/showthread...hreadid=395765
    >




  6. #6
    Bob Phillips
    Guest

    Re: Newbie problem :(

    Function is Public by default, as is Sub.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "K Dales" <[email protected]> wrote in message
    news:[email protected]...
    > For newbies, no "stupid" questions - we all had to learn!
    > You need to make the Function Public (and put it in a code module you
    > insert, not in ThisWorkbook or the worksheet's code module):
    >
    > Public Function Foo()
    > Foo = 42
    > End Function
    >
    > Functions can only be used within their own code module unless you make

    them
    > Public.
    > --
    > - K Dales
    >
    >
    > "Liosan" wrote:
    >
    > >
    > > Hello people,
    > >
    > > im new to using VBA, and im having a rather stupid problem. I am trying
    > > to write a function that i could use inside the worksheet, e.g. A1
    > > =Foo(). I tried
    > > Code:
    > > --------------------
    > > Function Foo()
    > > Foo = 42
    > > End Function
    > > --------------------
    > > and several variations on that theme, but the only effect i'm getting
    > > is #NAME errors. Does a function have to be registered in some way
    > > before being used on a sheet? or do i have to use CALL in some way?
    > >
    > > Thanks for any help,
    > >
    > > Liosan
    > >
    > >
    > > --
    > > Liosan
    > > ------------------------------------------------------------------------
    > > Liosan's Profile:

    http://www.excelforum.com/member.php...o&userid=26296
    > > View this thread:

    http://www.excelforum.com/showthread...hreadid=395765
    > >
    > >




  7. #7
    Fred
    Guest

    Re: Newbie problem :(

    Liosan wrote:
    > Hello people,
    >
    > im new to using VBA, and im having a rather stupid problem. I am trying
    > to write a function that i could use inside the worksheet, e.g. A1
    > =Foo(). I tried
    > Code:
    > --------------------
    > Function Foo()
    > Foo = 42
    > End Function
    > --------------------
    > and several variations on that theme, but the only effect i'm getting
    > is #NAME errors. Does a function have to be registered in some way
    > before being used on a sheet? or do i have to use CALL in some way?
    >
    > Thanks for any help,
    >
    > Liosan
    >
    >
    > --
    > Liosan
    > ------------------------------------------------------------------------
    > Liosan's Profile: http://www.excelforum.com/member.php...o&userid=26296
    > View this thread: http://www.excelforum.com/showthread...hreadid=395765


    Start a new blank workbook
    >From the Visual Basic Editor, right click VBAProject(Book1.xls)

    Go to: Insert - Module
    When the new module window opens add your code. This is (General)
    (Declarations)
    This should now work, you will see it switch to (General) (Foo)

    Here is my first simple function I did awhile back. It is for
    converting meters to feet. Enter a value in meters in cell A1, say 100.
    Then in cell B1 type: =feet(A1)
    It will then convert A1 from meters to feet.

    Public Function Feet(meters) As Double
    Feet = meters * 3.2808399
    End Function


+ 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