Last active
August 29, 2015 14:08
-
-
Save tsaiid/6b4b20242367425726fb to your computer and use it in GitHub Desktop.
Rank a selected range by rand in Excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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