jeudi 19 mars 2020

Custom Function to Create Random Number Not in Existing Table Column Range (VBA)

Please help!

I am trying to write a script that populates a random number into a cell that is not present in an existing range. The intention is to randomly pick a question number from a problem set, but I do not wish to repeat the same problem again. Each problem already completed is listed on a separate table column on sheet 2.

Can someone help me figure out the code? I'm wondering if I can use IF/THEN to keep running through random numbers until a value not present in the table column is populated in cell B10 on worksheet 1.

Does this seem like I'm on the right track? I cannot figure out the best logic to complete this function.

Here is my code, which is probably way off-base.

Function random_number_PS()

    Dim wrksht As Worksheet
    Dim tbl As ListObject
    Dim Random_Value As Integer

    Set wrksht = ActiveWorkbook.Worksheets("Sheet2")
    Set tbl = wrksht.ListObjects("Table1").ListColumns(1).DataBodyRange.Select

    Random_Value = Int((100 * Rnd) + 1)

        IF Random_Value 'this is where I am stuck

            Random_Value = Int((100 * Rnd) + 1)

        Else: ActiveWorkbook.Worksheets("Sheet2").Range("B10") = Random_Value

        End If

End Function

Here is the table in worksheet 2 that has the values I do not wish to repeat in the problem number column. Table with exclusive Values

Aucun commentaire:

Enregistrer un commentaire