+ Reply to Thread
Results 1 to 6 of 6

Command Button to Choose Random Number from LIST NOT RANGE

Hybrid View

  1. #1
    Registered User
    Join Date
    10-14-2014
    Location
    Staunton, VA
    MS-Off Ver
    Excel 2010 Professional
    Posts
    9

    Command Button to Choose Random Number from LIST NOT RANGE

    I would like to create a command button that each time it's clicked will populate a cell with one number from the following list: (1,3,5,7,9). I'd also like to create a separate command button that will populate a different cell with one number from this list: (2,4,6,8) It's ok for the same numbers to appear again on subsequent clicks of their respective buttons.

    Private Sub CommandButton1_Click()
    Range("BH3") = WorksheetFunction.RandBetween(1, 9)
    End Sub

    works fine for selecting from the entire range of numbers between 1 & 9 but can't figure out how to select from just the odd and even numbers between 1 & 9.

    If someone could provide some sample code to accomplish this it would be greatly appreciated.

  2. #2
    Forum Contributor
    Join Date
    07-19-2012
    Location
    Redmond, WA
    MS-Off Ver
    O365
    Posts
    133

    Re: Command Button to Choose Random Number from LIST NOT RANGE

    It doesn't look like it will do odd or even, but:
    https://www.techonthenet.com/excel/formulas/rnd.php

    Int ((9 - 1 + 1) * Rnd + 1)
    Result: random number between 1 and 9
    Rep is appreciated. Click my * if I helped.

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Command Button to Choose Random Number from LIST NOT RANGE

    Hi Ghostrider55,

    Try these macros:

    Option Explicit
    Sub Even()
    
        Dim blnIsNumberEven As Boolean
        Dim lngMyNumber As Long
        
        Application.ScreenUpdating = False
        
        blnIsNumberEven = False
        Do Until blnIsNumberEven = True
            lngMyNumber = Evaluate("RANDBETWEEN(1,9)")
            If lngMyNumber Mod 2 <> 1 Then 'http://www.vbaexpress.com/forum/showthread.php?17837-Solved-if-odd-even-numbers
                blnIsNumberEven = True
            End If
        Loop
        
        Range("BH3").Value = lngMyNumber
        
        Application.ScreenUpdating = True
    
    End Sub
    Sub Odd()
    
        Dim blnIsNumberEven As Boolean
        Dim lngMyNumber As Long
        
        Application.ScreenUpdating = False
        
        blnIsNumberEven = True
        Do Until blnIsNumberEven = False
            lngMyNumber = Evaluate("RANDBETWEEN(1,9)")
            If lngMyNumber Mod 2 = 1 Then 'http://www.vbaexpress.com/forum/showthread.php?17837-Solved-if-odd-even-numbers
                blnIsNumberEven = False
            End If
        Loop
        
        Range("BH3").Value = lngMyNumber
        
        Application.ScreenUpdating = True
    
    End Sub
    Please wrap any code you post in the applicable tags i.e. [CODE] your code here [/CODE] as I have done. Thanks.

    Regards,

    Robert
    Last edited by Trebor76; 11-04-2019 at 06:45 PM.
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Registered User
    Join Date
    10-14-2014
    Location
    Staunton, VA
    MS-Off Ver
    Excel 2010 Professional
    Posts
    9

    Re: Command Button to Choose Random Number from LIST NOT RANGE

    Robert - apologize for not tagging the code in my post and thanks for the reminder. Your macros work great so thanks for the response. The other one suggested below using IsOdd and IsEven works as well so I now have 2 options to choose from whereas before I had none. Thanks Again!

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,447

    Re: Command Button to Choose Random Number from LIST NOT RANGE

    Hey GR55,

    How about using an IsOdd or IsEven function in your quest. Something like:

    Private Sub CommandButton1_Click()
    DoOver:
        Range("BH3") = WorksheetFunction.RandBetween(1, 9)
        If WorksheetFunction.IsOdd(Cells(3, "BH")) Then GoTo Good
        GoTo DoOver
    Good:
    End Sub
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  6. #6
    Registered User
    Join Date
    10-14-2014
    Location
    Staunton, VA
    MS-Off Ver
    Excel 2010 Professional
    Posts
    9

    Re: Command Button to Choose Random Number from LIST NOT RANGE

    MarvinP - Works like a charm, Thank You!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 10-31-2019, 06:49 PM
  2. Create unique random number after hitting command button
    By vbogaert in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2018, 10:17 AM
  3. Choose random number from list
    By ricklou in forum Excel Formulas & Functions
    Replies: 16
    Last Post: 12-05-2016, 06:52 AM
  4. [SOLVED] Choose Random Number within range
    By ScabbyDog in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-12-2016, 11:49 AM
  5. [SOLVED] Choose random number between given values
    By Hitch75 in forum Excel General
    Replies: 3
    Last Post: 07-02-2015, 06:27 AM
  6. Command button linked to docket number range.
    By bumble1290 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-12-2013, 02:24 AM
  7. Choose Random Value Based On List
    By kblaine in forum Excel General
    Replies: 5
    Last Post: 04-20-2011, 02:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1