Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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


--
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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default 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" wrote in message
...

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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" wrote in
message ...

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default 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
...
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" wrote in message
...

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Simple question to find min value


Thanks Bernie...worked great and much more efficient!


--
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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Find and Replace Question Elessvie New Users to Excel 9 April 22nd 08 05:21 PM
IF formula-simple question; simple operator Rich D Excel Discussion (Misc queries) 4 December 6th 07 03:36 PM
simple find tcpteacher Excel Worksheet Functions 2 October 29th 05 02:09 PM
Simple Simple Excel usage question BookerW Excel Discussion (Misc queries) 1 June 23rd 05 10:06 PM
simple question, hopefully a simple answer! Matt B Excel Programming 5 January 13th 04 08:43 PM


All times are GMT +1. The time now is 02:02 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"