Colour it like crazy

Excel Random Cell Background (Fill) Colours

Here is a macro that fills a chosen block of cells with different background, or fill, colours. It make use of Excel's RANDBETWEEN function to randomise the colours.

When running the macro, the position of the active cell in your worksheet marks the top left-hand corner of the block to which the colours are applied.

You can adapt the macro to determine the size of the block and to limit the range of hues or saturations, or of both, it uses for the backgrounds.


Check for Availability of RANDBETWEEN Function

The macro relies on the use of the Excel RANDBETWEEN function, which in Excel 2007, 2010, 201s and 2016 is an inlcuded Math & Trig function.

If your version of Excel does not list the RANDBETWEEN function amongst the functions available for use, you need to install the Analysis Toolpak before running the macro.

To do this in Excel 2003, for example, choose from the Tools menu the Add-Ins command.


The Background Colour Macro

This is the code of the macro:

Sub dmwFillColors(rows&, cols&)

'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On Error GoTo errHandler

Dim r%, g%, b%

Dim iRow&, iCol&

Dim rng As Range, rngFill As Range

Dim msg$, icon&, title$

Set rng = ActiveCell

For iCol& = 0 To cols&

For iRow& = 0 To rows&

Set rngFill = rng.Offset(iRow&, iCol&)

With rngFill.Interior

r% = _

WorksheetFunction.RandBetween(0, 255)

g% = _

WorksheetFunction.RandBetween(0, 255)

b% = _

WorksheetFunction.RandBetween(0, 255)

.Pattern = xlSolid

.PatternColorIndex = xlAutomatic

.Color = RGB(r%, g%, b%)

End With

Next iRow&

Next iCol&

procDone:

Exit Sub

errHandler:

title$ = "Fill Colors Macro Error"

icon& = vbOKOnly + vbExclamation

msg$ = Err.Number & " " & Err.Description

MsgBox msg$, icon&, title$

Resume procDone

End Sub

For details of the error trapping and error handling included in the macro code follow this link to our page on the subject.


Setting the Range of Cells Coloured

Using the code above, the macro colours a block of cells determined by the values you assign to its arguments rows& and cols&.


Controlling the Range of Colours

The macro tells Excel which colour to use by defining it as a combination of red, green and blue.

The arguments for the RandBetween in each must be integers between 0 and 255.

To make the macro use blue hues alone, and black, change those rows like this:

r% = WorksheetFunction.RandBetween(0, 0)

g% = WorksheetFunction.RandBetween(0, 0)

b% = WorksheetFunction.RandBetween(0, 255)

If you want bright blue hues alone:

r% = WorksheetFunction.RandBetween(0, 0)

g% = WorksheetFunction.RandBetween(0, 0)

b% = WorksheetFunction.RandBetween(212, 255)

This will give fills leaning towards pastel colours:

r% = WorksheetFunction.RandBetween(128, 255)

g% = WorksheetFunction.RandBetween(128, 255)

b% = WorksheetFunction.RandBetween(128, 255)

Leaving the macro as shown, it will use colours chosen from a palette of between 16 and 17 million available to it.