+ Reply to Thread
Results 1 to 5 of 5

Simple question to find min value

  1. #1
    Registered User
    Join Date
    08-18-2005
    Posts
    22

    Simple question to find min value

    I'm running the macro below to find the minimum value, but the compiler keeps giving me an error at 'If cell = MinNum Then' . Can someone see what's wrong with this?

    Function MinAddress(rng)

    Set rng = Columns(22)
    ' Sets variable equal to maximum value in the input range.
    MinNum = Application.Min(rng)
    ' Loop to check each cell in the input range to see if equals the
    ' MaxNum variable.

    For Each cell In rng
    If cell = MinNum Then
    ' If the cell value equals the MaxNum variable it
    ' returns the address to the function and exits the loop.
    MinAddress = cell.Address
    Exit For
    End If
    Next cell

    End Function

    Thanks
    Trevor

  2. #2
    Bernie Deitrick
    Guest

    Re: Simple question to find min value

    Trevor,

    There's a lot that's not optimal with your code, but it fails because you are looping through using
    columns, which don't have a value property.

    Change

    For Each cell In rng
    (which really means For each column in the columns of range rng )

    to

    For Each cell In rng.Cells

    HTH,
    Bernie
    MS Excel MVP


    "Tre_cool" <[email protected]> wrote in message
    news:[email protected]...
    >
    > I'm running the macro below to find the minimum value, but the compiler
    > keeps giving me an error at 'If cell = MinNum Then' . Can someone see
    > what's wrong with this?
    >
    > Function MinAddress(rng)
    >
    > Set rng = Columns(22)
    > ' Sets variable equal to maximum value in the input range.
    > MinNum = Application.Min(rng)
    > ' Loop to check each cell in the input range to see if equals the
    > ' MaxNum variable.
    >
    > For Each cell In rng
    > If cell = MinNum Then
    > ' If the cell value equals the MaxNum variable it
    > ' returns the address to the function and exits the loop.
    > MinAddress = cell.Address
    > Exit For
    > End If
    > Next cell
    >
    > End Function
    >
    > Thanks
    > Trevor
    >
    >
    > --
    > Tre_cool
    > ------------------------------------------------------------------------
    > Tre_cool's Profile: http://www.excelforum.com/member.php...o&userid=26416
    > View this thread: http://www.excelforum.com/showthread...hreadid=397876
    >




  3. #3
    Bernie Deitrick
    Guest

    Re: Simple question to find min value

    Trevor,

    Here's a better way to do this, no looping, variables declared, function has value passed rather
    than set:

    Sub TryNow()
    MsgBox MinAddress(Columns(22))
    End Sub

    Function MinAddress(rng As Range) As String
    MinAddress = rng.Cells(Application.Match( _
    Application.Min(rng), rng, False)).Address
    End Function


    --
    HTH,
    Bernie
    MS Excel MVP


    "Bernie Deitrick" <deitbe @ consumer dot org> wrote in message
    news:ONpzG%[email protected]...
    > Trevor,
    >
    > There's a lot that's not optimal with your code, but it fails because you are looping through
    > using columns, which don't have a value property.
    >
    > Change
    >
    > For Each cell In rng
    > (which really means For each column in the columns of range rng )
    >
    > to
    >
    > For Each cell In rng.Cells
    >
    > HTH,
    > Bernie
    > MS Excel MVP
    >
    >
    > "Tre_cool" <[email protected]> wrote in message
    > news:[email protected]...
    >>
    >> I'm running the macro below to find the minimum value, but the compiler
    >> keeps giving me an error at 'If cell = MinNum Then' . Can someone see
    >> what's wrong with this?
    >>
    >> Function MinAddress(rng)
    >>
    >> Set rng = Columns(22)
    >> ' Sets variable equal to maximum value in the input range.
    >> MinNum = Application.Min(rng)
    >> ' Loop to check each cell in the input range to see if equals the
    >> ' MaxNum variable.
    >>
    >> For Each cell In rng
    >> If cell = MinNum Then
    >> ' If the cell value equals the MaxNum variable it
    >> ' returns the address to the function and exits the loop.
    >> MinAddress = cell.Address
    >> Exit For
    >> End If
    >> Next cell
    >>
    >> End Function
    >>
    >> Thanks
    >> Trevor
    >>
    >>
    >> --
    >> Tre_cool
    >> ------------------------------------------------------------------------
    >> Tre_cool's Profile: http://www.excelforum.com/member.php...o&userid=26416
    >> View this thread: http://www.excelforum.com/showthread...hreadid=397876
    >>

    >
    >




  4. #4
    Bob Phillips
    Guest

    Re: Simple question to find min value

    It works fine if you remove this line

    Set rng = Columns(22)

    What is it for?

    --

    HTH

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


    "Tre_cool" <[email protected]> wrote in
    message news:[email protected]...
    >
    > I'm running the macro below to find the minimum value, but the compiler
    > keeps giving me an error at 'If cell = MinNum Then' . Can someone see
    > what's wrong with this?
    >
    > Function MinAddress(rng)
    >
    > Set rng = Columns(22)
    > ' Sets variable equal to maximum value in the input range.
    > MinNum = Application.Min(rng)
    > ' Loop to check each cell in the input range to see if equals the
    > ' MaxNum variable.
    >
    > For Each cell In rng
    > If cell = MinNum Then
    > ' If the cell value equals the MaxNum variable it
    > ' returns the address to the function and exits the loop.
    > MinAddress = cell.Address
    > Exit For
    > End If
    > Next cell
    >
    > End Function
    >
    > Thanks
    > Trevor
    >
    >
    > --
    > Tre_cool
    > ------------------------------------------------------------------------
    > Tre_cool's Profile:

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




  5. #5
    Registered User
    Join Date
    08-18-2005
    Posts
    22
    Thanks Bernie...worked great and much more efficient!

+ 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