我有2个文本框,其中包含随机数。如何使用vba防止重复

时间:2015-08-19 20:33:06

标签: excel-vba vba excel

我有2个文本框,每个文本框都有一个随机数。如何防止重复被挑选。我认为if语句可以工作,但不知道如何返回选择随机数的行。严重的困惑。

Private Sub UserForm_Initialize()
Me.txtDate.Value = Format(Date, "mm/dd/yyyy")
 Columns("H:K").Select
Selection.NumberFormat = "# ??/??"
Range("A1").Select
Me.txtId.SetFocus

txtRand1 = Evaluate("randbetween(1,52)")
txtRand2 = Evaluate("randbetween(1,52)")

If txtRand1 = txtRand2 Then


 End If

End Sub

1 个答案:

答案 0 :(得分:2)

简单地重申一下。但是在一个循环中,所以如果你真的不走运并且它再次相同,它会一直持续到它们不再相等为止:

Private Sub UserForm_Initialize()

    Me.txtDate.Value = Format(Date, "mm/dd/yyyy")
     Columns("H:K").Select
    Selection.NumberFormat = "# ??/??"
    Range("A1").Select
    Me.txtId.SetFocus

    txtRand1 = Evaluate("randbetween(1,52)")
    txtRand2 = Evaluate("randbetween(1,52)")

    Do until txtRand1 <> txtRand2
        txtRand2 = Evaluate("randbetween(1,52)")
    Loop

end sub