简体   繁体   中英

finding the lowest value in a cell Excel VBA

I am new to this. I am trying to find the lowest value in a cell with multiple values inside. For example,

48
44.50
41.00
37.50

I am trying to find 37.50. What should be the code for it?

Thanks

Based on your posted example:

Sub FindMin()
   Dim s As String, CH As String
   Dim wf As WorksheetFunction
   Dim bry() As Double
   Set wf = Application.WorksheetFunction
   s = ActiveCell.Text

   CH = Chr(10)
   ary = Split(s, CH)
   ReDim bry(LBound(ary) To UBound(ary))

   For i = LBound(ary) To UBound(ary)
      bry(i) = CDbl(ary(i))
   Next i

   MsgBox wf.Min(bry)
End Sub

在此处输入图片说明

This assumes that there is a hard return ( ASCII-10 ) between the fields in the cell.

EDIT#1:

To make it into a function, remove the sub and replace with:

Public Function FindMin(r As Range) As Variant
   Dim s As String, CH As String
   Dim wf As WorksheetFunction
   Dim bry() As Double
   Set wf = Application.WorksheetFunction
   s = r.Text

   CH = Chr(10)
   ary = Split(s, CH)
   ReDim bry(LBound(ary) To UBound(ary))

   For i = LBound(ary) To UBound(ary)
      bry(i) = CDbl(ary(i))
   Next i

   FindMin = wf.Min(bry)
End Function

EDIT#2:

based on your comment, here is an example of input vs output:

在此处输入图片说明

Note that all the values are in a single cell and the values are separated by hard returns rather than spaces.

By code with same cell and a " " as delimiter to break

temp = Range("A1").Value

temp = Split(temp, " ")

Low = CInt(temp(0))

For i = 0 To UBound(temp) - 1
    If CInt(temp(i)) < Low Then Low = CInt(temp(i))
Next

Range("a2").Value = Low

if they are in a range you can use a formula

=MIN(A1:A4)

This question is pretty close to one previously asked: VBA/EXCEL: extract numbers from one cell that contained multiple values with comma

If you take the code from that answer and replace the comma with whatever is separating your values, you will be able to get access to them in VBA. Then you can write code to find the minimum.

You can make a macro to split the values for each cell you selected and then check for the highest value. And a quick check to make sure you are not parsing all the empty rows (when you selected a column).

The macro below will set the highest value in the next column.

Sub lowest()

    Dim Values As Variant
    Dim LowestValue As Double

    Dim a As Range

    Set a = Selection

    For Each Row In a.Rows

      For Each Cell In Row.Cells

        LowestValue = -1
        Values = Split(Cell.Value, Chr(10))

        For Each Value In Values

            If LowestValue = -1 Then
                LowestValue = Value
            ElseIf Value < LowestValue Then
                LowestValue = Value
            End If
        Next

        Cells(Cell.Row, Cell.Column + 1).Value = LowestValue
        If IsEmpty(Cell.Value) Then GoTo EndLoop

      Next Cell

    Next Row

EndLoop:

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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