Question

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 !

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top