Excel VBA-函数范围的最小值和最大值
我有两个非常相似的函数,它们在我将代码切换到显式选项(Explicit用于调试pupose)之前工作(成功!)。从那时起,Excel VBA-函数范围的最小值和最大值,vba,excel,Vba,Excel,我有两个非常相似的函数,它们在我将代码切换到显式选项(Explicit用于调试pupose)之前工作(成功!)。从那时起,Max函数不再起作用,我无法详细说明原因,也无法将其作为xl vba完美noob解决 最大值功能(不工作): 运行时错误: 我在运行时收到“error 91”,Xmax值为:“Nothing” 错误91表示:未定义对象或带有块变量 min功能(工作) 如何调用这两个函数: Set rng = ws_source.Range("3:3") X_min = MinAdd
Max
函数不再起作用,我无法详细说明原因,也无法将其作为xl vba完美noob解决
- 最大值功能(不工作):
- 运行时错误: 我在运行时收到“error 91”,Xmax值为:“Nothing” 错误91表示:未定义对象或带有块变量
- min功能(工作)
Set rng = ws_source.Range("3:3")
X_min = MinAddress(rng)
X_max = MaxAddress(rng) ' returns : X_max = Nothing
数据在第3行,包含格式化的数字和文本。不确定min为什么工作,但我相信应该是这样的
Application.WorksheetFunction.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
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
我不能复制这个错误。当然它看起来很棘手。如果你把X_max当作零,那么X_max是一个不合适的对象变量。使用
Dim X_max作为字符串
Right和Thx,您得到了:-)
Application.WorksheetFunction.Max
Application.WorksheetFunction.Min
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