简体   繁体   中英

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.

  • The Max function (does not work):

     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 
  • The runtime error :

    I receive "error 91" at the runtime, with the Xmax valuing : "Nothing" Error 91 stands for : undefined object or With block variable

  • The min function (works)

     Function MinAddress(The_Range) As Variant ' See http://support.microsoft.com/kb/139574 Dim MinNum As Variant Dim cell As Range ' Sets variable equal to maximum value in the input range. MinNum = Application.Min(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 = 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 

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.

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

Application.WorksheetFunction.Max

&

Application.WorksheetFunction.Min

(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM