Excel 函数创建不在现有表中的随机数

Excel 函数创建不在现有表中的随机数,excel,vba,if-statement,random,Excel,Vba,If Statement,Random,我试图将一个随机数填充到一个单元格中,该随机数在现有范围内不存在 我的目的是从一个习题集中挑选一个问题编号,但我不想重复同样的问题 已完成的每个问题都列在第2页的单独表格列中 我想知道是否可以使用if/THEN继续运行随机数,直到在工作表1的单元格B10中填充表列中不存在的值 Function random_number_PS() Dim wrksht As Worksheet Dim tbl As ListObject Dim Random_Value As Inte

我试图将一个随机数填充到一个单元格中,该随机数在现有范围内不存在

我的目的是从一个习题集中挑选一个问题编号,但我不想重复同样的问题

已完成的每个问题都列在第2页的单独表格列中

我想知道是否可以使用if/THEN继续运行随机数,直到在工作表1的单元格B10中填充表列中不存在的值

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
这是工作表2中的表,其中的值我不想在问题编号列中重复

具有独占值的表
这是一个应该满足您需要的函数,您可以将其作为函数或在工作表中调用,例如
=RandNotInList(1100,B1:B100)


这个函数将返回一个不在列表中的随机数——几乎是

Function RandomNumber_PS() As Integer
    ' let your function return the random number found

    Dim Fun As Integer          ' this will be the function's return value
    Dim Ws As Worksheet
    Dim Fnd As Range
    Dim Tbl As Range
    Dim i As Integer

    Set Ws = ActiveWorkbook.Worksheets("Sheet2")
    Set Tbl = Ws.ListObjects("Table1").DataBodyRange.Columns(1)
    Randomize                   ' selects a random seed number

    Do
        Fun = Int((100 * Rnd) + 1)
        Set Fnd = Tbl.Find(Fun, LookAt:=xlWhole)
        ' leave the loop if Fun wasn't found in the table.
        If Fnd Is Nothing Then Exit Do
        i = i + 1                   ' emergency exit
    Loop While i < 300

    If i = 300 Then
        MsgBox "I haven't been able to find a random number.", _
               vbInformation, "Failure notice"
    End If

    RandomNumber_PS = Fun
End Function
顺便说一句,您可以将解决方案部署为UDF,并从工作表中调用函数:


第二张![B10]
=RandomNumber\u PS2()

你能给我看一下Sheet2的图像吗?当然可以。初始post已更新为包含第2页上表格的图像,其中包含我不希望包含在随机函数中的值。您是否也可以显示第二个表格的图像?
Function RandomNumber_PS() As Integer
    ' let your function return the random number found

    Dim Fun As Integer          ' this will be the function's return value
    Dim Ws As Worksheet
    Dim Fnd As Range
    Dim Tbl As Range
    Dim i As Integer

    Set Ws = ActiveWorkbook.Worksheets("Sheet2")
    Set Tbl = Ws.ListObjects("Table1").DataBodyRange.Columns(1)
    Randomize                   ' selects a random seed number

    Do
        Fun = Int((100 * Rnd) + 1)
        Set Fnd = Tbl.Find(Fun, LookAt:=xlWhole)
        ' leave the loop if Fun wasn't found in the table.
        If Fnd Is Nothing Then Exit Do
        i = i + 1                   ' emergency exit
    Loop While i < 300

    If i = 300 Then
        MsgBox "I haven't been able to find a random number.", _
               vbInformation, "Failure notice"
    End If

    RandomNumber_PS = Fun
End Function
Function RandomNumber_PS2() As Integer
    ' let your function return the random number found

    Const RandomLimit As Integer = 100      ' reset to suit

    Dim Ws As Worksheet
    Dim TblRng As Range
    Dim Fnd As Range
    Dim Arr As Variant, Lst() As Integer
    Dim Idx As Integer
    Dim R As Long

    Set Ws = ActiveWorkbook.Worksheets("Sheet2")
    Set TblRng = Ws.ListObjects("Table1").DataBodyRange.Columns(1)

    ReDim Lst(1 To RandomLimit)
    Arr = TblRng.Value
    For R = 1 To RandomLimit
        Set Fnd = TblRng.Find(R, LookAt:=xlWhole)
        If Fnd Is Nothing Then
            Idx = Idx + 1
            Lst(Idx) = R
        End If
    Next R
    ReDim Preserve Lst(1 To Idx)

    Select Case Idx
        Case 0
            MsgBox "There are no more tasks to alot.", _
                   vbInformation, "Failure notice"
        Case 1
            RandomNumber_PS2 = Lst(1)
        Case Else
            Randomize                   ' selects a random seed number
            RandomNumber_PS2 = Lst(Int((Idx * Rnd) + 1))
    End Select
End Function