如何在选择后从列表中删除数据

时间:2017-06-10 23:27:28

标签: excel vba excel-vba

我有一个带有2个TextBox的表单。我正在使用randomize方法根据列D获取某个国家/地区。我希望能够在选择后删除所选的国家/地区。在我的截图中,我们会注意到尼日利亚已经被分配给Jessie。

截图

Screenshot

    Private Sub CommandButton1_Click()

Dim randomCtry As Long

Randomize

Application.Wait Now + TimeValue("00:00:01")
DoEvents

Dim LastRow As Long
LastRow = Sheets("Sheet1").Range("D" & Rows.Count).End(xlUp).Row

'With txtDestination
'    .Text = Sheets("Sheet1").Range("D" & Int((LastRow - 1) * Rnd + 2))
'
'End With


Set randomCtry = Sheets("Sheet1").Range("D" & Int((LastRow - 1) * Rnd + 2))
txtDestination.Text = randomCtry.Text
randomCtry.Delete Shift:=xlUp ' <-- remove the selected country from the list


Application.Wait Now + TimeValue("00:00:01")
DoEvents


With Sheet1

        With .Range("A" & .Rows.Count).End(xlUp)

        .Offset(1).Resize(1, 2).Value = Array(txtMilitary.Text, txtDestination.Text)

        End With


        On Error Resume Next

End With

End Sub

1 个答案:

答案 0 :(得分:1)

如果要从D列中永久删除已分配的国家/地区,则可以这样执行:

   dim randomCtry as Range
   set randomCtry = Sheets("Sheet1").Range("D" & Int((LastRow - 1) * Rnd + 2))
   txtDestination.Text = randomCtry.Text
   randomCtry.Delete Shift:=xlUp ' <-- remove the selected country from the list

上面的代码替换了代码With txtDestination ...End With

的集团