2

I would like to find a given value in a range, and if I find the value I would like to put it in an array. After the loop I would like to give the smallest value in the array to a variable.

How is is possible?

    For k = 2 To LastRow_Roles
        If roles_sheet.Cells(k, 2).Value = "Admin" Then
            selected_number = roles_sheet.Cells(k, 1).Value
            ReDim aArray(0 To UBound(aArray) + 1) As Variant
            aArray(UBound(aArray)) = selected_number        
         end if
    next k

minimum_value = Application.min(aArray)
low_sheet.Cells(1, 1) = minimum_value
Erase aArray
ReDim aArray(0 To 1) As Variant

1 Answer 1

1

You will need to use ReDim Preserve so that you're preserving the existing data that has been appended to the array.

You can then use the Application.WorksheetFunction.Min function to extract the Min number found within the array.

For k = 2 To LastRow_Roles
    If roles_sheet.Cells(k, 2).Value = "Admin" Then
        ReDim Preserve aArray(0 To UBound(aArray) + 1)
        aArray(UBound(aArray)) = roles_sheet.Cells(k, 1).Value
    End If
Next k

minimum_value = Application.WorksheetFunction.Min(aArray)
low_sheet.Cells(1, 1) = minimum_value
Erase aArray
ReDim aArray(0 To 1) As Variant
Sign up to request clarification or add additional context in comments.

3 Comments

thank you. Just one question. When I am using Application.WorksheetFunction.max(aArray) it gives back rounded value. I see only 66 intead of 66.4684. Can you please help me with this? I tried Long and double instead of Variant but it is not working.
what have you declared the variable you're assigning the maximum value to? If it's got decimal places it should be a double. Dim max_value as Double. Otherwise, debug and see if the array is populating with decimal values.
you are right. The maximum_value was Long. With Double is working like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.