Skip to content

Instantly share code, notes, and snippets.

@tsaiid
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsaiid/6b4b20242367425726fb to your computer and use it in GitHub Desktop.
Save tsaiid/6b4b20242367425726fb to your computer and use it in GitHub Desktop.
Rank a selected range by rand in Excel
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Function ExistInSelection(criteria) As Boolean
For Each a In Selection.Areas
If WorksheetFunction.CountIf(a, criteria) Then
ExistInSelection = True
Exit Function
End If
Next
ExistInSelection = False
End Function
Sub RandRanges()
Dim CellsCount As Integer
CellsCount = Selection.Cells.Count
If CellsCount > 1 Then
Dim i As Integer
For i = 1 To 50
Selection.Clear
For Each c In Selection.Cells
Dim UniqueNo As Integer
Do
UniqueNo = WorksheetFunction.RandBetween(1, CellsCount)
Loop While ExistInSelection(UniqueNo)
c.Value = UniqueNo
Next
Sleep (20)
Next i
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment