A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Is there any update on it? You may also give feedback on it.
Feel free to come back if you need any help about Excel.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi all, apologies if this has been asked previously.
I wish to randomly select a name from a large list (the same name appears more than once) and remove all occurrences of that name once it is selected.
Any help will be appreciated.
Thank you
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Answer accepted by question author
Thank you very much.
Will give this a try.
Appreciate your help.
Sub RemoveRandomNameAndLog()
Dim ws As Worksheet
Dim lastRow As Long
Dim rng As Range
Dim randomIndex As Long
Dim selectedName As String
Dim logRow As Long
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your sheet name
' Find the last row with data in column A
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' Check if there are any names in column A
If lastRow<1 Then
MsgBox "No names found in column A."
Exit Sub
End If
' Set the range of names in column A
Set rng = ws.Range("A1:A" & lastRow)
' Generate a random index
Randomize
randomIndex = Int((rng.Rows.Count) \* Rnd + 1)
' Get the randomly selected name
selectedName = rng.Cells(randomIndex, 1).Value
' Find the next empty row in column C
logRow = ws.Cells(ws.Rows.Count, "C").End(xlUp).Row + 1
' Log the removed name in column C
ws.Cells(logRow, 3).Value = selectedName
' Remove all occurrences of the selected name
For i = lastRow To 1 Step -1
If ws.Cells(i, 1).Value = selectedName Then
ws.Cells(i, 1).Delete
End If
Next i
End Sub