error of calling a Sub() in EXCEL 2010 in VBA on Win7

∥☆過路亽.° 提交于 2019-12-12 10:19:54

问题


i need to call a sub from WEXCEL 2010 VBAon win7.

find_max rng1:=rng  // error !!! ByRef argu mismatch 

Sub find_max(ByRef rng1 As Range)

 Dim dblM As Double
 dblM = -9E+307
 Dim maxCellAddress As String
 For Each Cell In rng
 If IsNumeric(c) Then
       If dblMax < CDbl(Cell.Value) And Cell.Value <> "" Then
           dblM = CDbl(Cell.Value)
           maxCellAddress = (Cell.Address)
       End If
 End If
 Next Cell
 End Sub

Any help would be appreciate !


回答1:


To summarize all of the comments try this:

Option Explicit

Sub test()
    Dim rng As Range
    Set rng = Range("A1:B10")

    Call find_max(rng)
End Sub

Sub find_max(rng1 As Range)
    Dim dblM As Double
    dblM = -9E+307

    Dim maxCellAddress As String
    Dim cell As Range

    For Each cell In rng1
       If IsNumeric(cell) Then
             If dblM < CDbl(cell.Value) And cell.Value <> "" Then
                 dblM = CDbl(cell.Value)
                 maxCellAddress = (cell.Address)
             End If
       End If
    Next cell

    MsgBox (maxCellAddress)
End Sub

If you want to return the max cell value change it to a function like this:

Option Explicit

Sub test()
    Dim rng As Range
    Set rng = Range("A1:B10")

    MsgBox (find_max(rng))
End Sub

Function find_max(rng1 As Range)
    Dim dblM As Double
    dblM = -9E+307

    Dim maxCellAddress As String
    Dim cell As Range

    For Each cell In rng1
       If IsNumeric(cell) Then
             If dblM < CDbl(cell.Value) And cell.Value <> "" Then
                 dblM = CDbl(cell.Value)
                 maxCellAddress = (cell.Address)
             End If
       End If
    Next cell

    find_max = maxCellAddress
End Function


来源:https://stackoverflow.com/questions/22696549/error-of-calling-a-sub-in-excel-2010-in-vba-on-win7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!