View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default how do i fill cells with random color?

I can't see the entire thread (actually, I see only the message I am
responding to), but I think this macro will do what you ask (just put your
color indexes in the Array function call and set the range of cells to color
in the Set statement)...

Sub ColorTheRange()
Dim RangeToColor As Range, Cell As Range, Indexes() As Variant
Randomize
Indexes = Array(3, 4, 5, 6, 29)
Set RangeToColor = Range("A1:l32")
For Each Cell In RangeToColor
Cell.Interior.ColorIndex = Indexes(Int(((UBound(Indexes) - _
LBound(Indexes) + 1) * Rnd) + LBound(Indexes)))
Next
End Sub

As written, the code will handle any number of color index assignments (to a
maximum of 56) in the Array statement list and the rest of the code will
work correctly with them.

--
Rick (MVP - Excel)



"honeybee129" wrote in message
...
This works if you just want a random color of the first 5 colors in the
color
index list, but if I wanted a radom color out of a specific 5 colors how
could I adapt this? The 5 color indexes I want a 3,4,5,6, and 29.

"N10" wrote:


"Casey" wrote in message
...
I am trying to fill a grid of equal sized cells with random colors, or
colors
attached to a random number. I can fill the grid with random numbers
easily
enough, it's the colors i want.
Thanx....Casey



Hi Casey

Try this then adpat to your needs


Sub colorit()


Dim task As Range
Dim myvalue
Set task = Range("A1:l32")


For y = 1 To 5

For Each Cell In task
Randomize

myvalue = Int((56 * Rnd) + 1)

Cell.Interior.ColorIndex = myvalue

Next

Next

'
End Sub