简体   繁体   English

Excel VBA - 函数最小范围的最大值

[英]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 !). 我有两个非常相似的函数,在我将代码切换到Option Explicit以进行调试之前工作(成功!)。 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. 从那以后, Max功能不再起作用了,我无法详细解释原因并将其解决为xl vba完美的菜鸟。

  • 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 我在运行时收到“错误91”,Xmax值为:“Nothing”错误91代表:未定义对象或带块变量

  • 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. 数据位于第3行,包含格式化的数字和文本。

Not sure why min works, but I believe it's supposed to be 不知道为什么min工作,但我相信它应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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