Randomly placing 5 same alphabet in a range of 20 cells. (Random Generating)

Excript

New Member
Joined
Mar 12, 2009
Messages
2
Hi, I desperately need help with something.

I would like excel to randomly pick cells and insert an alphabet to a range of cells.

For example.... 1 row of 20 and i would like excel to randomly pick 5 out of the 20 cells and insert an alphabet into it.

someone posted this before

Cell 1 =IF(RAND() <= 0.2, "a", "")
Cell 2 Drag to cell 20 =IF(RAND() < (4 - COUNTIF(B$1:B1, "a") )/ (21 - ROW()), "a", "")

Problem with this formula is that the excel sheet will auto generate when i click on other cells or when i start filtering.

Appreciate it if someone is able to help
<!-- / message --> <!-- BEGIN TEMPLATE: ad_showthread_firstpost_sig --> <!-- END TEMPLATE: ad_showthread_firstpost_sig --> <!-- controls -->
progress.gif
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this...

Code:
Sub test()
    Dim rng As Range
    Dim selectionrange As Range
    Dim i As Integer
    
    Set selectionrange = Application.InputBox("Please Select Your Range", Type:=8)
    
    For Each rng In selectionrange.Cells
        
        i = Rnd() * 10
        
        If i <= 1 Then
            rng.Value = "a"
        ElseIf i > 2 And i <= 3 Then
            rng.Value = "b"
        ElseIf i > 5 And i <= 6 Then
            rng.Value = "c"
        ElseIf i > 7 And i <= 8 Then
            rng.Value = "d"
        ElseIf i > 9 And i <= 10 Then
            rng.Value = "e"
        End If
    
    Next rng
End Sub
 
Upvote 0
err...it generates different alphabets...can i have only one alphabet only and have 5 being placed randomly in 20 selected cells?
 
Last edited:
Upvote 0
On your excel window, press Alt+F11... in the new window that opens up, go to Insert->Module.

Once you have inserted the module, on the white area just paste the code. Save and close the new window.

In your excel sheet, click Alt+F8, select test & click on run. It will 1st ask you to select a range. Just select the required range and click on OK.
 
Upvote 0

Forum statistics

Threads
1,222,103
Messages
6,163,953
Members
451,867
Latest member
csktwyr

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