select后如何从列表中删除数据

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

在这里ClickMe ClickMe

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 

如果您想从列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 Withreplace你的代码With txtDestination ...End With