+ Reply to Thread
Results 1 to 6 of 6

#Value! question

  1. #1
    Registered User
    Join Date
    07-11-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    9

    #Value! question

    hey everyone,

    when I try to run the simple VBA code:

    Please Login or Register  to view this content.
    it gives me a #VALUE! error... any ideas whats wrong?

    Thanks a lot
    Last edited by Cutter; 07-12-2012 at 01:59 PM. Reason: Added code tags

  2. #2
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: #Value! question

    A function (user defined function) is used to return a value into the cell where the function is used (or within a program where the function is used). Your code is returning a value to cell A2. That would be a subroutine, not a function. Replace "Function" with "Sub" and it will work

    Edit: If it is not your intention to return 2 to cell A2, exactly what are you trying to accomplish?
    ChemistB
    My 2?

    substitute commas with semi-colons if your region settings requires
    Don't forget to mark threads as "Solved" (Edit First post>Advanced>Change Prefix)
    If I helped, Don't forget to add to my reputation (click on the little star at bottom of this post)

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: #Value! question

    following will return 5 to the caller. Like ChemistB pointed out, function can not act on cells other than the caller cell.
    Please Login or Register  to view this content.
    Regards,
    Vandan

  4. #4
    Registered User
    Join Date
    07-11-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: #Value! question

    hm, ok thank you for the input. I am trying to add this piece of code to a function to store a variable in a cell so I can call on it later even after the workbook is closed

  5. #5
    Forum Guru
    Join Date
    08-05-2004
    Location
    NJ
    MS-Off Ver
    365
    Posts
    13,582

    Re: #Value! question

    I am trying to add this piece of code to a function to store a variable in a cell so I can call on it later even after the workbook is closed
    You can place a "value" in a cell with either a subroutine or a UDF. You can use a subroutine to insert a constant or even a formula within a cell (as you were doing with your initial code). Case 1: a subroutine which you can call from the macro window or using a button. This one places the value of 2 into cell A2 of whichever sheet was active when the code was run
    Please Login or Register  to view this content.
    Case 2: a UDF (user defined function) will, as Vandan pointed out, will only return a value to the cell it is imbedded in. So with this code in a module, if you put in cell A2 =omg(), you'll return 2 to that cell.
    Please Login or Register  to view this content.
    Hope that helped.

  6. #6
    Registered User
    Join Date
    07-11-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: #Value! question

    yes thank you very much for the clarification!

+ 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