VBA RandCellTest: Random Cell Generator

Morey

New Member
Joined
Sep 24, 2018
Messages
37
The VBA Code below works for me but, I would prefer to be able to specify a 10% random selection. Any help would be appreciated.

VBA Code:
Function RandCell(Rg As Range) As Range
    Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function

Sub RandCellTest()
Dim Counter As Long
Dim TargetRg As Range, Cell As Range

Set TargetRg = Range("M3:M2000")

TargetRg.ClearFormats

For Counter = 1 To 150
    Set Cell = RandCell(TargetRg)
    Cell.Interior.Color = RGB(0, 255, 0)   'use this if you need one color
'    Cell.Interior.Color = RGB(Int((255 * Rnd)), Int((255 * Rnd)), Int((255 * Rnd)))     'use this if you need random colors
Next

End Sub


Regards,

Morey
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Do you mean you want to highlight 10% of the selected range (ie ~200 cells)?
Also are you worried about certain cells being "selected" more than once?
 
Upvote 0
Do you mean you want to highlight 10% of the selected range (ie ~200 cells)?
Also are you worried about certain cells being "selected" more than once?

Fluff,

Yes & yes... Also, currently the code re-formats the cells. I would like the cells not to be reformatted.

Regards,

Morey
 
Last edited:
Upvote 0
Ok, how about
VBA Code:
Sub RandCellTest()
   Dim Counter As Long
   Dim TargetRg As Range, Cell As Range
   
   Set TargetRg = Range("M3:M2000")
   
   TargetRg.Interior.Color = xlNone
   
   With CreateObject("scripting.dictionary")
      For Counter = 1 To TargetRg.Count / 10
         Set Cell = RandCell(TargetRg)
         Do Until .Exists(Cell.Address) = False
            Set Cell = RandCell(TargetRg)
         Loop
         .Add Cell.Address, Nothing
         Cell.Interior.Color = RGB(0, 255, 0)   'use this if you need one color
      Next Counter
   End With
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub RandCellTest()
   Dim Counter As Long
   Dim TargetRg As Range, Cell As Range
  
   Set TargetRg = Range("M3:M2000")
  
   TargetRg.Interior.Color = xlNone
  
   With CreateObject("scripting.dictionary")
      For Counter = 1 To TargetRg.Count / 10
         Set Cell = RandCell(TargetRg)
         Do Until .Exists(Cell.Address) = False
            Set Cell = RandCell(TargetRg)
         Loop
         .Add Cell.Address, Nothing
         Cell.Interior.Color = RGB(0, 255, 0)   'use this if you need one color
      Next Counter
   End With
End Sub

Fluff,

I'm getting a Compile Error (Sub or Function not defined). Image is attached for reference.


Regards,

Morey
 

Attachments

  • Error.PNG
    Error.PNG
    25.4 KB · Views: 6
Upvote 0
Did you delete this function?
VBA Code:
Function RandCell(Rg As Range) As Range
    Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function
 
Upvote 0
Did you delete this function?
VBA Code:
Function RandCell(Rg As Range) As Range
    Set RandCell = Rg.Cells(Int(Rnd * Rg.Cells.Count) + 1)
End Function

Fluff,

Added that section back in... What do you, know? It works. I could not be more appreciative.


Thank you,

Morey
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,391
Messages
6,124,678
Members
449,179
Latest member
fcarfagna

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