+ Reply to Thread
Results 1 to 9 of 9

Cannot get Range to pass into Function

Hybrid View

  1. #1
    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)
    Entia non sunt multiplicanda sine necessitate

  2. #2
    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!)

  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

    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?

  4. #4
    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.

  5. #5
    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