hornetbzz
hornetbzz

Reputation: 9357

Excel VBA - function min max of a range

I've got 2 very similar functions, that were working before I switched my code to the Option Explicit for debugging puposes (success !). Since then, the Max function does not work anymore and I can't elaborate the reason why and solve it as an xl vba perfect noob.

How I call both functions :

Set rng = ws_source.Range("3:3")
X_min = MinAddress(rng)
X_max = MaxAddress(rng) ' returns : X_max = Nothing

The data are in the row 3, containing formatted numbers and text.

Upvotes: 5

Views: 78744

Answers (2)

whytheq
whytheq

Reputation: 35557

(not an answer but too big for a comment)

I have the following in a normal module and it works fine:

Function MaxAddress(The_Range) As Variant
' See http://support.microsoft.com/kb/139574

Dim MaxNum As Variant
Dim cell As Range

  ' Sets variable equal to maximum value in the input range.
  MaxNum = Application.Max(The_Range)
  ' Loop to check each cell in the input range to see if equals the
  ' MaxNum variable.
  For Each cell In The_Range
     If cell.Value = MaxNum Then
        ' If the cell value equals the MaxNum variable it
        ' returns the address to the function and exits the loop.
        MaxAddress = cell.Address
        Exit For
     End If
  Next cell

End Function

Sub xxx()
Dim rng As Range
Dim X_max As String
Set rng = ThisWorkbook.Sheets(1).Range("3:3")
X_max = MaxAddress(rng)
MsgBox (X_max)
End Sub

Upvotes: 6

Abe Gold
Abe Gold

Reputation: 2347

Not sure why min works, but I believe it's supposed to be

Application.WorksheetFunction.Max

&

Application.WorksheetFunction.Min

Upvotes: 6

Related Questions