Camera Icon

Got an event that needs shooting?  I've covered multiple events for marathon-photos.com and can capture the scenes & emotions that make your event unique.

Some samples of my own work are at Digital Formula on Flickr.

Wand Icon

Do you or your business need a website but you're unsure where to start?  Get in touch now and we'll get it sorted.

I focus on usable functionality & can setup smaller sites using Wordpress within hours of the order being placed.  Magic!

Globe Icon

So, you've already got a website but need help managing it?  I can help you.

Don't worry about knowing every single detail.  I'll find out the most important stuff and work with you to reach your goal, not someone else's.

said on twitter: LOL spammers really do bank on the ignorance of their targets huh? A file with a .jpg.exe extension ... really? http://t.co/0TgobjQc

 
Short URL
Warning: This article is older than 180 days and may contain inaccurate information.  Please use the information below at your own risk.

Microsoft Excel - Count coloured cells

What backup?

Most companies have some sort of backup strategy in place and that strategy often involves documenting the success & failure rate of the backups on a daily basis. For obvious reasons all the companies I've worked with in the past have documented this using a variation of the well-known "backup matrix", created using Microsoft Excel.

I choose to create my backup matrix documents so that backup results are colour-coded. That way they're easily visible and it's easy to see what happened when and to get an overall idea of how good your backups have been. Green coloured cells are successful backups, red cells are failed backups etc.

The first thing I need to do when I create these documents is to make some 'reference cells'. These cells are coloured with the various colours that will be used in the matrix, e.g. green for successful etc, as listed above. So they don't look like reference cells I also use them as the key for the matrix so people know what the various colours mean.

How, then, do you work out how good the backups have been if there's no text in the cells to look for? You need a VBA function that looks for the cell's colour instead of its contents - easy. Below is the function I wrote for that purpose - you'll need to make sure you are using .XLSM documents if you are using Excel 2007 for Windows as .XLSX files are, by default, secured so that macros won't run.

Please note that the code below won't work in Office for Mac 2008 as VBA is not supported. Office for Mac 2011 will have full support for VBA - I'm looking forward to see if this code works there.
Function countColours(colourReferenceCell As Range, cellRange As Range)
    ' countColours
    ' Chris Rasmussen, April 2010
    '
    ' Count occurences of cells that are a certain background colour
    ' E.g. for use in a backup matrix
    
    Dim currentCell As Range
    Dim colourReference As Long
    Dim vResult
    
    colourReference = colourReferenceCell.Interior.ColorIndex
    
    ' reset the total count back to zero to prevent errors
    vResult = 0
    
    ' go through all the cells in the specified range and look at the background colour
    For Each currentCell In cellRange
        If currentCell.Interior.ColorIndex = colourReference Then
            vResult = vResult + 1
    End If
    Next currentCell
    
    countColours = vResult
    
End Function

Once you've got this function in place, all you need to do is choose the cell that will show the total of successful backups (for example) and enter the following formula. We'll assume that cell A1 is the cell that is coloured green for reference. We'll also assume that the Excel cell range C1:C5 contains the results.

=countColours(A1,C1:C5)

If you have 5 successful backups in your range and they're all coloured green, the cell containing that formula with contain the number 5. Easy. :)

» Tags: excel, vba, report, reports, reporting
DigitalFormula is an experiment in HTML5 and CSS3 design by Chris Rasmussen, an amateur/casual designer based in Melbourne, Australia. -37.813611 144.963056