Excel VBA-函数最小值最大值最大值

hor*_*bzz 5 excel vba excel-vba

我有2个非常相似的函数,在将代码切换到Option Explicit调试目的(成功!)之前,这些函数已经起作用。从那时起,该Max功能不再起作用,并且我无法解释将其作为xl vba完美菜鸟解决的原因。

  • Max函数(不起作用):

    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
    
    Run Code Online (Sandbox Code Playgroud)
  • 运行时错误

    我在运行时收到“错误91”,其Xmax值为:“无”错误91代表:未定义对象或带块变量

  • 函数(作品)

    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
    
    Run Code Online (Sandbox Code Playgroud)

我如何调用这两个函数:

Set rng = ws_source.Range("3:3")
X_min = MinAddress(rng)
X_max = MaxAddress(rng) ' returns : X_max = Nothing
Run Code Online (Sandbox Code Playgroud)

数据在第3行中,其中包含格式化的数字和文本。

Abe*_*old 5

不确定min为何起作用,但我认为应该是

Application.WorksheetFunction.Max
Run Code Online (Sandbox Code Playgroud)

Application.WorksheetFunction.Min
Run Code Online (Sandbox Code Playgroud)


why*_*heq 5

(不是答案,但太大,无法发表评论)

我在正常模块中具有以下内容,并且工作正常:

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
Run Code Online (Sandbox Code Playgroud)