tel 01732 833085 · e-mail david wallis
Here is a macro that fills a chosen block of cells with randomly chosen background, or fill, colours. The position of the active cell in your worksheet marks the top left-hand corner of the block.
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.
The macro relies on the use of the Excel RANDBETWEEN 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, from the Tools menu, choose the Add-Ins command.
This is the code of the macro:
For details of the error trapping and error handling included in the macro code follow this link to our page on the subject.
Using the code above, the macro colours a block of cells composing of 10 rows and 10 columns.
If you need to change the size and shape of the block, change the value assigned to the iRows and iCols variables in rows 12 and 13 of the macro.
The macro tells Excel which colour to use by defining it as a combination of red, green and blue.
Row 18 of the macro assigns a value to the red component of the colour by assigning a random number to the variable r. Row 19 assigns to variable g the green component; and row 20 to variable b the blue component.
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:
If you want bright blue hues alone:
This will give fills leaning towards pastel colours:
Leaving the macro as shown, it will use colours chosen from a palette of between 16 and 17 million available to it.