Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VBA - function min max of a range

Tags:

excel

vba

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.

like image 739
hornetbzz Avatar asked Sep 19 '14 15:09

hornetbzz


2 Answers

(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
like image 162
whytheq Avatar answered Oct 04 '22 14:10

whytheq


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

Application.WorksheetFunction.Max

&

Application.WorksheetFunction.Min
like image 33
Abe Gold Avatar answered Oct 04 '22 13:10

Abe Gold