从一个范围中提取五个唯一数字,并使用Excel VBA

时间:2017-06-20 16:08:49

标签: excel excel-vba vba

我在excel表中有一个如下表所示的数字表。 Excel Table

No1 No2 No3 No4 No5
1   190 134 190 101
10  142 117 10  151
155 12  12  12  128
154 154 154 154 154

我有一个包含5个TextBoxes的UserForm,可以显示表中的5个唯一数字。

当我单击命令按钮时,它应该从上表中拉出5个UNIQUE数字并分别显示在TextBoxes中。

此外,如果我再次单击命令按钮(即多次点击以获得更多结果),它应该从表中填充不同的 UNIQUE 数字。

我使用简单的if else概念来实现这一点,但它无法正常工作。

请帮我实现结果。谢谢!

我使用以下代码:

Private Sub btnGenerate_Click()

Dim PresentRow As Byte, PresentColumn As Byte

PresentRow = ActiveCell.Row
PresentColumn = ActiveCell.Column

If PresentRow = 5 And PresentColumn = 1 Then
    Sheet1.Range("F2").Activate
ElseIf PresentRow = 5 And PresentColumn = 5 Then
    Sheet1.Range("A2").Activate
End If

Select Case ActiveCell.Column

 Case 1
     TextBox1.Text = ActiveCell.Value

     If ActiveCell.Offset(0, 1).Value <> TextBox1.Text Then
         TextBox2.Text = ActiveCell.Offset(0, 1).Value
     Else
         TextBox2.Text = ActiveCell.Offset(1, 1).Value
     End If

     If ActiveCell.Offset(0, 2).Value <> TextBox2.Text Then
         TextBox3.Text = ActiveCell.Offset(0, 2).Value
     Else
         TextBox3.Text = ActiveCell.Offset(1, 2).Value
     End If

     If ActiveCell.Offset(0, 3).Value <> TextBox3.Text Then
         TextBox4.Text = ActiveCell.Offset(0, 3).Value
     Else
         TextBox4.Text = ActiveCell.Offset(1, 4).Value
     End If

     If ActiveCell.Offset(0, 4).Value <> TextBox4.Text Then
         TextBox5.Text = ActiveCell.Offset(0, 4).Value
     Else
         TextBox5.Text = ActiveCell.Offset(1, 5).Value
     End If

 Case 5

     TextBox1.Text = ActiveCell.Value

     If ActiveCell.Offset(0, -1).Value <> TextBox1.Text Then
         TextBox2.Text = ActiveCell.Offset(0, -1).Value
     Else
         TextBox2.Text = ActiveCell.Offset(1, -1).Value
     End If

     If ActiveCell.Offset(0, -2).Value <> TextBox2.Text Then
         TextBox3.Text = ActiveCell.Offset(0, -2).Value
     Else
         TextBox3.Text = ActiveCell.Offset(1, -2).Value
     End If

     If ActiveCell.Offset(0, -3).Value <> TextBox3.Text Then
         TextBox4.Text = ActiveCell.Offset(0, -3).Value
     Else
         TextBox4.Text = ActiveCell.Offset(1, -4).Value
     End If

     If ActiveCell.Offset(0, -4).Value <> TextBox4.Text Then
         TextBox5.Text = ActiveCell.Offset(0, -4).Value
     Else
         TextBox5.Text = ActiveCell.Offset(1, -5).Value
     End If

End Select
Sheet1.Activate
ActiveCell.Offset(1, 0).Select

End Sub

1 个答案:

答案 0 :(得分:0)

  

以下要求通过工具,参考资料将 Microsoft Scripting Runtime 添加到项目中。

在工作表代码表中:

Option Explicit

Private Sub CommandButton1_Click()
    Dim txt As Long, vals As Variant, ky As Variant
    Dim uniq As New Scripting.Dictionary

    vals = Range("A2:E5").Value2
    uniq.RemoveAll

    Do While uniq.Count < 5
        uniq.Item(vals(Application.RandBetween(LBound(vals, 1), UBound(vals, 1)), _
                       Application.RandBetween(LBound(vals, 2), UBound(vals, 2)))) = vbNullString
    Loop

    TextBox1 = uniq.Keys(0)
    TextBox2 = uniq.Keys(1)
    TextBox3 = uniq.Keys(2)
    TextBox4 = uniq.Keys(3)
    TextBox5 = uniq.Keys(4)

End Sub

enter image description here