{"id":127,"date":"2008-08-19T14:10:18","date_gmt":"2008-08-19T13:10:18","guid":{"rendered":"http:\/\/simoncpage.co.uk\/blog\/?p=127"},"modified":"2008-10-19T14:53:26","modified_gmt":"2008-10-19T13:53:26","slug":"excel-vb-unique-random-numbers","status":"publish","type":"post","link":"https:\/\/simoncpage.co.uk\/blog\/2008\/08\/excel-vb-unique-random-numbers\/","title":{"rendered":"Excel VB | unique random numbers"},"content":{"rendered":"<p>I often like to fill my excel model and workbooks with data to test out\u00a0functionality and what better than a random\u00a0range of numbers. (hint look in my other post about changing <a href=\"https:\/\/simoncpage.co.uk\/blog\/2008\/08\/19\/excel-convert-positive-to-negative-and-negative-to-positive\/\">negative numbers\u00a0to positives and vice versa<\/a> if you want to make the them different sizes for instance if you wanted the numbers in the thousands).<\/p>\n<p>This code below basically starts with the first cell of the selection and creates a random\u00a0number and then check that number to any others in the\u00a0range that are\u00a0duplicates &#8211; looping until there are\u00a0no matches\u00a0and then\u00a0carrying on to the next cell in the range to create another unique random number.<\/p>\n<blockquote><p>Sub RandomNoDuplicates()<br \/>\nDim LowerLimit As Byte<br \/>\nDim Limit2 As Long<\/p>\n<p>Call CheckProtectedSheet<br \/>\nCall CheckForMultipleAreas<\/p>\n<p>If MsgBox(&#8220;This will place a unique random number in each cell in your selection?&#8221; &amp; vbNewLine &amp; &#8220;(n.b. existing values will be overwritten)&#8221;, vbQuestion + vbOKCancel, AT &amp; &#8221; &#8211; Insert random numbers without duplicates&#8221;) = vbCancel Then Exit Sub<br \/>\nLowerLimit = 1<br \/>\nLimit2 = Selection.Cells.Count<br \/>\nCellen = Selection.Cells.Count<br \/>\ni = 1<br \/>\nSelection.ClearContents<\/p>\n<p>For Each rngCel In Selection<br \/>\nApplication.StatusBar = &#8220;Processing random numbers: &#8221; &amp; Int((i * 100) \/ Cellen) &amp; &#8221; %&#8221;<\/p>\n<p>Section1:<br \/>\nrngCel.Value = Int((Limit2 &#8211; LowerLimit + 1) * Rnd + LowerLimit)<\/p>\n<p>If Application.WorksheetFunction.CountIf(Selection, rngCel.Value) = 1 Then<br \/>\nGoTo Section2<br \/>\nElse<br \/>\nGoTo Section1<br \/>\nEnd If<\/p>\n<p>Section2:<br \/>\ni = i + 1<\/p>\n<p>Next<br \/>\nApplication.StatusBar = False<\/p>\n<p>End Sub<\/p><\/blockquote>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-98\" title=\"excel\" src=\"https:\/\/simoncpage.co.uk\/blog\/wp-content\/uploads\/2008\/08\/excel.gif\" alt=\"\" width=\"91\" height=\"92\" \/><\/p>\n<p><a href=\"https:\/\/simoncpage.co.uk\/blog\/wp-content\/uploads\/2008\/08\/duplicates-random-lists.zip\">duplicates-random-lists.zip<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often like to fill my excel model and workbooks with data to test out\u00a0functionality and what better than a random\u00a0range of numbers. (hint look in my other post about changing negative numbers\u00a0to positives and vice versa if you want to make the them different sizes for instance if you wanted the numbers in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[26],"tags":[123,79,78,117,27,119,121],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/127"}],"collection":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":0,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"wp:attachment":[{"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simoncpage.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}