+ Reply to Thread
Results 1 to 9 of 9

Cannot get Range to pass into Function

  1. #1
    Registered User
    Join Date
    06-11-2009
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2003
    Posts
    4

    Cool Cannot get Range to pass into Function

    Hello, thank you for your help.

    I am attempting to call a Function from a cell and pass it a range and an Integer. The Integer works fine, but I cannot get the Range to pass into the function correctly.

    Please Login or Register  to view this content.
    If I remove the ".Range(rng)" the code works but for the entire Row and not just the portion of the row inside my Range.

    If I replace ".Range(rng)" with ".Range("A1:G26")" it works correctly.

    Part 2:

    When it is working through the button_click, it still does not work when called directly from a cell why is that?

    Herre is the code referenced in the cell:
    Please Login or Register  to view this content.

    Can anyone help? thx

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Cannot get Range to pass into Function

    A UDF cannot, with some arcane exceptions, do anything other than return a value to the cell in which it appears. That limitation extends to any procedure invoked by a UDF.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    06-11-2009
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Cannot get Range to pass into Function

    Do you have any other ideas how to accomplish this?

    Even if I kept it within 1 function (to return 1 value), is there a way I can get the Range to pass from the cell when the function is called into the function?

    Thanks for your help, appreciate it!

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Cannot get Range to pass into Function

    You can pass a range to a UDF as a named range, or directly:

    =BS(A1:G26, 3)

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Cannot get Range to pass into Function

    Hello dewittig,

    Your macro works correctly as UDF when rewritten as shown here. It also will work correctly in VBA. When specifying a range in VBA you will need use the word Range, e.g. Range("C1:G25"). If you are referencing a range that is on a worksheet other than the ActiveSheet, you will need to prefix the range with the worksheet name.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  6. #6
    Registered User
    Join Date
    06-11-2009
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Cannot get Range to pass into Function

    Thank You both for your help. Your code works correctly, however I am still having problems calling the code from within a cell, but it works when called through a button.

    Here's the code within the button:
    Range("w16") = BS(Range("A1:G26"), 3)

    Here's the code within the cell:
    =BS(A1:G26,3)
    I have also tried:
    =BS(Range("A1:G26"),3)
    =BS(Range(A1:G26),3)

    The button returns "2" (which is correct)
    The cell says "#REF!"

    Any ideas?

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,979

    Re: Cannot get Range to pass into Function

    Are you using exactly the code that Leith provided? It should work fine if you use:
    =BS(A1:G26,3)
    in a cell.
    Everyone who confuses correlation and causation ends up dead.

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Cannot get Range to pass into Function

    Hello dewittig,

    The macro I wrote works fine in both instances. Your syntax is correct for the button and the first syntax for cell is also correct. Did you place the macro in a Standard VBA module? If not, the macro will not be visible to the worksheet.

  9. #9
    Registered User
    Join Date
    06-11-2009
    Location
    las vegas, nv
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Cannot get Range to pass into Function

    Thank You Leith!

    After a good nights sleep all is working great!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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