Tuesday, 31 January 2012

Thematic maps... (in Excel!!!)

I recently figured out how to make thematic maps in Excel, like the one below which shows average attainment in English and maths within English primary schools, available from the Department for Education.  First I adjusted the cells in the workbook to make the square grid.  Then I matched the postcode level test score data to a database (from Ordnance Survey) to return the grid reference of each school.  Next I calculated the average attainment for each 5km square, mapped the value between 1 and 10 (to help with the next step) and populated the grid with the results.  I then wrote a couple of macros (see below), which first of all edit Excel's standard colour palette, allowing the use of a nice light green to dark red colour scale, and then sets the colours in the grid according to the cell's value.


Excel VBA macros:



Sub ChangePalette()
Workbooks("name of your workbook.xls").Colors(1) = RGB(229, 255, 204)
Workbooks("name of your workbook.xls").Colors(2) = RGB(204, 255, 153)
Workbooks("name of your workbook.xls").Colors(3) = RGB(178, 255, 102)
Workbooks("name of your workbook.xls").Colors(4) = RGB(153, 255, 51)
Workbooks("name of your workbook.xls").Colors(5) = RGB(102, 204, 0)
Workbooks("name of your workbook.xls").Colors(6) = RGB(255, 102, 102)
Workbooks("name of your workbook.xls").Colors(7) = RGB(255, 51, 51)
Workbooks("name of your workbook.xls").Colors(8) = RGB(255, 0, 0)
Workbooks("name of your workbook.xls").Colors(9) = RGB(204, 0, 0)
Workbooks("name of your workbook.xls").Colors(10) = RGB(153, 0, 0)
End Sub


Sub ColorChange()

Dim rRange As Range

Dim rCell As Range

     Set rRange = Range("A1", Range("Z26").End(xlDown))

     For Each rCell In rRange 
     rCell.Interior.ColorIndex = rCell.Value

    Next rCell

End Sub




No comments:

Post a Comment