VBA random numbers, no duplicates form a set of numbers and show the number in a message box

lex_m

Board Regular
Joined
Aug 5, 2003
Messages
155
I want it so when I click a button it will show a random number from this set of numbers 1,3,4,5,7,8,10,14,17,18,29,38,40,45,50,53 in a message box, no duplicates, when I have gone though the numbers a message box would come up to let me know that are all the numbers have been picked and reset so I can do it again. Is this possible in excel 2003?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
An alternative solution:-
NB:- Numbers also appear in column "A", see code to remove, if required.

Code:
[COLOR="Navy"]Sub[/COLOR] MG01Oct26
Static nums
Static c
Static temp
[COLOR="Navy"]Dim[/COLOR] Txt [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] oRnd [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
[COLOR="Navy"]Dim[/COLOR] n
Randomize
[COLOR="Navy"]If[/COLOR] c = 0 [COLOR="Navy"]Then[/COLOR]
     Columns("A:A").ClearContents
    nums = Array(0, 1, 3, 4, 5, 7, 8, 10, 14, 17, 18, 29, 38, 40, 45, 50, 53)
[COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]If[/COLOR] UBound(nums) > 0 [COLOR="Navy"]Then[/COLOR]
        oRnd = Int(Rnd * UBound(nums)) + 1
         c = c + 1
          Cells(c, 1) = nums(oRnd) '[COLOR="Green"][B]Remove sheet Nums if required[/B][/COLOR]
             c = IIf(c = 16, 0, c)
              MsgBox nums(oRnd)
            [COLOR="Navy"]For[/COLOR] n = 0 To UBound(nums)
                [COLOR="Navy"]If[/COLOR] Not nums(n) = nums(oRnd) [COLOR="Navy"]Then[/COLOR] Txt = Txt & nums(n) & ","
            [COLOR="Navy"]Next[/COLOR] n
              Txt = Left(Txt, Len(Txt) - 1)
                nums = Split(Txt, ",")
    [COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,508
Members
452,918
Latest member
Davion615

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top