+ Reply to Thread
Results 1 to 9 of 9

Cannot get Range to pass into Function

Hybrid View

dewittig Cannot get Range to pass into... 06-11-2009, 03:10 PM
shg Re: Cannot get Range to pass... 06-11-2009, 03:19 PM
dewittig Re: Cannot get Range to pass... 06-11-2009, 06:44 PM
shg Re: Cannot get Range to pass... 06-11-2009, 07:04 PM
Leith Ross Re: Cannot get Range to pass... 06-11-2009, 09:06 PM
romperstomper Re: Cannot get Range to pass... 06-12-2009, 03:42 AM
  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.

    'This code is from the cell  **doesn't work, i get "#NAME?"
    =BS(JuneAll,3)
    'also tried:
    =BS("A1:G26",3)
    
    'I have also tried calling it from a button_click:
    Private Sub CommandButton1_Click()
        Range("V8") = BS(Range("A1:G26"), 3)
    End Sub
    
    
    Function BS(rng As Range, iRow As Integer)
        'Test operations
        countt = 0
        With Sheets("Utilization").Range(rng).Rows(iRow).Select
            For Each c In Selection
                If c.Interior.ColorIndex = 37 Then countt = countt + 1
            Next
        End With
        BS = countt
    End Function
    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:
    =BS(Range("A1:G26"), 3)

    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.
    Function BS(rng As Range, iRow As Integer)
      Dim c As Range
      Dim countt As Long
        'Test operations
        With rng.Rows(iRow)
          For Each c In .Cells
            Addx = c.Address
            If c.Interior.ColorIndex = 37 Then countt = countt + 1
          Next
        End With
        BS = countt
    End Function
    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,980

    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.

+ 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