+ Reply to Thread
Results 1 to 6 of 6

fill any 6 of 18 cells randomly with an "X"

Hybrid View

  1. #1
    Registered User
    Join Date
    11-07-2011
    Location
    Wisconsin USA
    MS-Off Ver
    Excel 2016
    Posts
    18

    fill any 6 of 18 cells randomly with an "X"

    I need a macro that will do exactly as the title says. I have 18 cells that, when a command button is pressed, it will randomly pick 6 of the 18 cells and fill them with an x. Is this even possible?

  2. #2
    Registered User
    Join Date
    08-26-2012
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    58

    Re: fill any 6 of 18 cells randomly with an "X"

    can use the below


    
    
     Sub Randmnumber()
    
    Range("A1:Z200").ClearContents
    
    For TM = 1 To 6
    
    RN = Int((6 - 18 + 1) * Rnd + 18)
    CN = Int((6 - 18 + 1) * Rnd + 18)
    
    If RN <> 0 And CN <> 0 Then
    Cells(RN, CN).Value = "x"
    Else
    TM = TM - 1
    End If
    
    
    Next
    
    
    End Sub

  3. #3
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: fill any 6 of 18 cells randomly with an "X"

    Another way:
    Sub fill_random_x()
    Dim x As Integer, y As Integer
    Dim rng As Range
    Set rng = Sheets("Sheet1").Range("A1:C6")
        
    rng.ClearContents
       
    Do While Application.WorksheetFunction.CountIf(rng, "x") < 6
        x = Application.WorksheetFunction.RandBetween(1, rng.Rows.Count)
        y = Application.WorksheetFunction.RandBetween(1, rng.Columns.Count)
        Cells(x, y) = "x"
    Loop
        
       
    End Sub

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 2019 on Win10 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: fill any 6 of 18 cells randomly with an "X"

    Hi, MyComputerGuy,

    Your post does not comply with Rule 8 of our Forum RULES. Do not crosspost your question on multiple forums without including links here to the other threads on other forums.

    Cross-posting is when you post the same question in other forums on the web. The last thing you want to do is waste people's time working on an issue you have already resolved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser) to the cross-post.

    Expect cross-posted questions without a link to be closed and a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).

    http://www.ozgrid.com/forum/showthread.php?t=181208

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: fill any 6 of 18 cells randomly with an "X"

    Sub fill_random_x_v2()
    Dim x As Integer, y As Integer
    Dim rng As Range
    Set rng = Sheets("Sheet1").Range("A1:C6")
       
    rng.ClearContents
       
    Do While Application.WorksheetFunction.CountIf(rng, "x") < 6
        x = Int(1 + Rnd() * rng.Rows.Count)
        y = Int(1 + Rnd() * rng.Columns.Count)
        Cells(x, y) = "x"
    Loop
       
    End Sub

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,627

    Re: fill any 6 of 18 cells randomly with an "X"

    Above solution is not good.
    It return wrong result on large number of results.

    Here is distribution of previous solution:
    4,60%	5,63%	6,42%
    7,03%	5,70%	4,05%
    6,64%	4,45%	5,55%
    4,02%	6,96%	5,65%
    5,53%	6,71%	4,50%
    5,65%	4,03%	6,88%
    And this is new solution:
    5,53%	5,58%	5,59%
    5,53%	5,59%	5,53%
    5,56%	5,57%	5,54%
    5,51%	5,55%	5,53%
    5,57%	5,57%	5,58%
    5,55%	5,58%	5,54%
    Looks like RANDEBETWEEN function doesn0t work fine

    Code is below

+ 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. [SOLVED] Auto Fill Blank Cells - Using Differing "Start" Numbers
    By HowD in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-23-2013, 01:05 PM
  2. [SOLVED] Manually fill up 1 or (some) cells with just "BFW"or"BF" in data validation
    By robke1960 in forum Non English Excel
    Replies: 2
    Last Post: 12-11-2012, 11:31 AM
  3. VBA. Fill empty cells with text "OPEN", I2 downwards, row count Col A
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2011, 06:50 PM
  4. Replies: 5
    Last Post: 06-12-2008, 12:39 PM
  5. [SOLVED] How to fill in "BLANK" Cells Automactically...Large Spread Sheet
    By msbutton27 in forum Excel General
    Replies: 3
    Last Post: 01-15-2006, 12:15 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