lundi 20 avril 2020

Create Custom Function to return random cell value from a range

I am a VBA amateur and am trying to create a custom function that returns a random value from a known range. I must be doing something wrong with the following code because it causes the program to crash.

Do you have any idea how I can make the corrections that will allow the function to loop through each cell of the range until the cell value equals the random number, which has been arbitrarily bound by 1 and 1000?

Thanks so much for any thoughts!

Function RandinList(InRange As Range) As Long

Dim random As Long
Dim cell As Range

Do

    random = Int((1000 - 1 + 1) * Rnd + 1)

    For Each cell In InRange
        If Not random = cell Then Exit For
    Next cell

Loop Until cell.Value = random

RandinList = random

End Function



Aucun commentaire:

Enregistrer un commentaire