Consulting

Results 1 to 8 of 8

Thread: Solved: VBA Code . Max of 4 nos and min of 4 nos

  1. #1
    VBAX Regular
    Joined
    Aug 2008
    Posts
    48
    Location

    Solved: VBA Code . Max of 4 nos and min of 4 nos

    Hi ! I need VBA code to find the minimum of 4 nos ( ignoring zeros) and the max of 4 numbers... need this in vba as I will be using it in a user-defined function.

    Thanks...

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    MaxNum = Application.Max(Num1, Num2, Num3, Num4)
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Aug 2008
    Posts
    48
    Location
    thanks.... any tips for the min of 4 nos ? ignoring zeros and negative numbers?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Will it ba range values or variables?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    As xld said, it makes a difference for the input type. For ranges:
    [vba]Sub test()
    [a1] = 5
    [a2] = 0
    [a3] = 4
    [a4] = 1
    MsgBox "Max=" & MaxMin(Range("A1:A4")) & vbCrLf & _
    "Min=" & MaxMin(Range("A1:A4"), False)
    End Sub

    Function MaxMin(my4Nums As Range, Optional bMax As Boolean = True)
    Dim r As Range, i As Integer, n() As Variant
    i = 0
    If bMax = False Then
    For Each r In my4Nums
    If r.Value <> 0 Then
    i = i + 1
    ReDim Preserve n(1 To i) As Variant
    n(i) = r.Value
    End If
    Next r
    MaxMin = WorksheetFunction.Min(n)
    Exit Function
    End If
    MaxMin = WorksheetFunction.Max(my4Nums)
    End Function

    [/vba]

  6. #6
    VBAX Regular
    Joined
    Aug 2008
    Posts
    48
    Location
    variables... not a range...

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    sValues = "{" & Num1 & "," & Num2 & "," & Num3 & "," & Num4 & "}"

    MinNum = ActiveSheet.Evaluate("MIN(IF(" & sValues & ">0," & sValues & "))")
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    VBAX Regular
    Joined
    Aug 2008
    Posts
    48
    Location
    Thanks a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •