Excel VB | unique random numbers
I often like to fill my excel model and workbooks with data to test out functionality and what better than a random range of numbers. (hint look in my other post about changing negative numbers to positives and vice versa if you want to make the them different sizes for instance if you wanted the numbers in the thousands).
This code below basically starts with the first cell of the selection and creates a random number and then check that number to any others in the range that are duplicates - looping until there are no matches and then carrying on to the next cell in the range to create another unique random number.
Sub RandomNoDuplicates()
Dim LowerLimit As Byte
Dim Limit2 As LongCall CheckProtectedSheet
Call CheckForMultipleAreasIf MsgBox(”This will place a unique random number in each cell in your selection?” & vbNewLine & “(n.b. existing values will be overwritten)”, vbQuestion + vbOKCancel, AT & ” - Insert random numbers without duplicates”) = vbCancel Then Exit Sub
LowerLimit = 1
Limit2 = Selection.Cells.Count
Cellen = Selection.Cells.Count
i = 1
Selection.ClearContentsFor Each rngCel In Selection
Application.StatusBar = “Processing random numbers: ” & Int((i * 100) / Cellen) & ” %”Section1:
rngCel.Value = Int((Limit2 - LowerLimit + 1) * Rnd + LowerLimit)If Application.WorksheetFunction.CountIf(Selection, rngCel.Value) = 1 Then
GoTo Section2
Else
GoTo Section1
End IfSection2:
i = i + 1Next
Application.StatusBar = FalseEnd Sub

- Battling on iPad using awesome Teamviewer RDP app with poor bandwidth. Got project files organized & sent along with some new prints to boot. (2 hrs ago) follow me...
About
You’re currently reading “Excel VB | unique random numbers”.
- Published:
- Tuesday, August 19th, 2008
- Author:
- Simon Page
- Category:
- Excel
Related
Categories
- Artwork (64)
- Design Inspiration (24)
- Design Resources (5)
- Excel (12)
- Filemaker (5)
- Gadgets (26)
- Music (3)
- Photography (3)
- Photoshop (1)
- Press (3)
- Random (6)
- Software (4)
- Video Gaming (15)
Featured
- Album Cover Artwork Inspiration
- Creative Movie Poster Inspiration
- Design Inspiration | typography posters
- Exclusive iPad Wallpaper
- Futurism - Limited Edition Prints
- Hertsi Kohina album cover design
- Inspiring Gallery of Video Game Concept Art
- International Year of Astronomy 2009 Posters
- Rolet Design Concept
- Tron Legacy Movie Poster
- WIRED Magazine iPad App Design






No comments
Jump to comment form | comments rss | trackback uri