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

Follow my updates on Twitter here
- Just found out about this designer Pedro Vilas-Boas yesterday - his ten_pt work blew me away: http://www.vilaz.tv/. (4 hrs ago)
About
You’re currently reading “Excel VB | unique random numbers”.
- Published:
- Tuesday, August 19th, 2008
- Author:
- Simon Page
- Category:
- Excel
- Tags:
- downloads, excel 2003, excel 2007, free, vb, vba, visual basic
Related Posts
Categories
- Artwork (58)
- Design Inspiration (23)
- Design Resources (6)
- Excel (12)
- Filemaker (5)
- Gadgets (26)
- Music (3)
- Photography (3)
- Photoshop (1)
- Press (3)
- Random (5)
- Software (4)
- Video Gaming (15)
Featured Posts
- 20 of the Best Amazing Photoshop Tutorials
- Creative Movie Poster Inspiration
- December 09: Graphic Designs, Illustrations and looking back at 2009
- Design Inspiration | typography posters
- Fun with Circles poster designs
- Inspiring Gallery of Video Game Concept Art
- International Year of Astronomy 2009 Posters
- Resources of Creative Design Inspiration
- Rolet Design Concept
- Top 10 Gadgets for 2009
- Top Christmas Electronics
- Tron Legacy Movie Poster






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