{"id":3390,"date":"2017-06-08T08:57:13","date_gmt":"2017-06-08T14:57:13","guid":{"rendered":"https:\/\/www.vertex42.com\/blog\/?p=3390"},"modified":"2024-11-06T18:15:27","modified_gmt":"2024-11-07T00:15:27","slug":"dynamic-geographic-heat-map-in-excel","status":"publish","type":"post","link":"https:\/\/www.vertex42.com\/blog\/help\/excel-help\/dynamic-geographic-heat-map-in-excel.html","title":{"rendered":"How to Make a Dynamic Geographic Heat Map in Excel"},"content":{"rendered":"<p><em>By Onur Yilmaz of Someka.net, edited by Jon Wittwer<\/em><\/p>\n<p>Data visualization is a trending topic in recent years as we collect and analyze more and more data. Geographic Heat Maps provide a very useful technique for visualizing data associated with countries, states, and cities.<\/p>\n<p>If you have tried the <b>Power Map<\/b> feature in Excel, known now as <a href=\"https:\/\/support.office.com\/en-us\/article\/Get-started-with-3D-Maps-6b56a50d-3c3e-4a9e-a527-eea62a387030\"><b>3D Maps<\/b><\/a> in newer versions of Excel, you may have been amazed at how easy it is to create a heat map for standard geographic regions. If you delve even deeper, you can learn how to create heat maps for custom boundaries.<\/p>\n<p>There may be times when you'd like to create a geographic heat map without being limited to the functionality of the built-in 3D Maps feature. In this article, we will share how you can do this with custom shapes, formulas, and a little bit of VBA code. The result is a great-looking heat map that looks like this:<\/p>\n<div class=\"center\"><img decoding=\"async\" src=\"https:\/\/cdn.vertex42.com\/blog\/images\/screenshots\/geographic-heat-map-example-usa.png\" alt=\"Geographic Heat Map Example - USA\" width=\"574\" height=\"347\" class=\"aligncenter imgshadow\"><\/div>\n<p><!--\n\n\n<p>You could import shapes and modify the colors one by one according to your data, but that would be a big hassle when you want to make some changes. With the help of some formulas and macros you can create a <b>dynamic<\/b> geographic heat map in Excel - one that will update automatically when the data is updated.<\/p>\n\n\n--><\/p>\n<p>In this article we will go over every step to create your own Geographic Heat Map Generator in Excel for USA states. After you get the idea, you can implement the same for other cities and countries or even situations where the shapes might not even be geographical.<\/p>\n<p>If you don't have enough time to make one for yourself, you can take a look at the <a href=\"https:\/\/www.someka.net\/excel-templates\/heat-maps\/\" target=\"_blank\" rel=\"noopener\">Ready-To-Use Heat Map Generators in Excel<\/a> on Someka.net<\/p>\n<p>Ok, let's get our hands dirty!<\/p>\n<p>The development of this tool consists of 4 main parts:<\/p>\n<ol>\n<li><a href=\"#visual-assets\">Create\/Gather Visual Assets<\/a><\/li>\n<li><a href=\"#data-table\">Set up the Data Table and Name Shapes<\/a><\/li>\n<li><a href=\"#legend-and-scale\">Create the Legend and Color Scale<\/a><\/li>\n<li><a href=\"#automate\">Automate with VBA<\/a><\/li>\n<\/ol>\n<h2 id=\"visual-assets\">1. Create\/Gather Visual Assets<\/h2>\n<p>First, we will need to find, create, or import separate shapes for each of the states in the USA. A full USA map doesn't work for this task. The shapes should be free-form and editable.<\/p>\n<p>You can Google \"editable powerpoint USA map\" or something similar. These shapes are generally provided in powerpoint templates. You can find both free and premium maps. (For example: http:\/\/www.presentationgo.com\/presentation\/usa-editable-powerpoint-map\/) After you get the shapes, copy or insert them into a blank Excel workbook. The shapes may be grouped together, so don't forget to \"Ungroup\" the shapes after you get them into Excel.<\/p>\n<p>An alternative is to draw the shapes yourself in Excel using Insert &gt; Shapes &gt; Freeform: Shape. If you insert an image of a map into Excel, you can use the image as an aid in tracing boundaries as you create the shapes. You could also use Photoshop, Illustrator or other image editing software. Drawing your own shapes will just take longer.<\/p>\n<p>After you have the USA states inserted into Excel, editable and ready to be painted, try changing the fill color of a couple of the shapes one at a time to make sure that works.<\/p>\n<p>You will have something similar to this:<\/p>\n<div class=\"center\"><img decoding=\"async\" src=\"https:\/\/cdn.vertex42.com\/blog\/images\/screenshots\/separate-shapes-for-usa-states.png\" width=\"591\" height=\"405\" class=\"aligncenter imgshadow\" alt=\"Separate Shapes for USA States\"><\/div>\n<h2 id=\"data-table\">2. Set up the Data Table and Name Shapes<\/h2>\n<p>Create a data table similar to the one in the image below. It is important to include the \"Order\" column because we will use the number to link the data table to the shapes in the map.<\/p>\n<p>The Shape and Text Box columns show the names that will be used for naming each of the shapes and textboxes in the next step. <!--Hint: when creating a column of data like usmap1, usmap2, you can enter just the first value (usmap1) and then copy the cell down and Excel will usually be smart enough to increment the number at the end of the text value.--><\/p>\n<div class=\"center\"><img decoding=\"async\" src=\"https:\/\/cdn.vertex42.com\/blog\/images\/screenshots\/data-table-for-geographic-heat-map.png\" width=\"502\" height=\"248\" class=\"aligncenter imgshadow\" alt=\"Data Table for Geographic Heat Map\"><\/div>\n<h3>Name each shape<\/h3>\n<p>Now we will need to \"Name\" each shape one by one. This is the most tedious part. We need to name these shapes because we will loop through them automatically with a VBA macro to change the fill color. It will take effort, but the results are rewarding.<\/p>\n<p>&bull; Select each shape (state) and name it using the name box to the left side of the formula bar. In the image of the shapes above, you can see that the selected shape is named \"Shape 1394.\" Change that to the correct name found in the Shape column of the data table.<\/p>\n<p>&bull; Use a naming convention that is easy to loop through with VBA. For example: <i>usmap1<\/i>, <i>usmap2<\/i>, <i>usmap3<\/i>.<\/p>\n<h3>Create text boxes for each shape<\/h3>\n<p>&bull; Create a text box (Insert &gt; Shapes &gt; Textbox) for each state and place the text box over each state (or for small states, place the text box nearby and use a leader line to point to the state). In the end you will have as many text boxes as your state shapes. Tip: Format the first text box the way you want (using whatever placeholder text you want), and then copy and paste the text box.<\/p>\n<p>&bull; Name the text boxes <i>ustext1<\/i>, <i>ustext2<\/i>, <i>ustext3<\/i>, etc. the same way you did for the states (select the textbox, use the Name Box to name it, etc.)<\/p>\n<p>&bull; The text box names should correspond with the state names as shown in the data table.<\/p>\n<p>Creating and naming the shapes and text boxes is the most tedious part of this process. But, now you have 50 state shapes and 50 corresponding text boxes placed on each state. We are ready to proceed to the next part.<\/p>\n<h2 id=\"legend-and-scale\">3. Create the Legend and Color Scale<\/h2>\n<p>Now we will define our legend and color scale and write formulas that our macro will use to update the colors and text values in the map.<\/p>\n<h3>Define your Legend<\/h3>\n<p>The legend is used to define the data ranges used for the color scale in the heat map. There isn't a limit to the number of divisions in your legend. However, using too many different colors may be confusing.<\/p>\n<p>In this example, we will use the following 5 divisions for population in our legend:<\/p>\n<div class=\"center\"><img decoding=\"async\" src=\"https:\/\/cdn.vertex42.com\/blog\/images\/screenshots\/legend-and-color-scale-for-heat-map.png\" alt=\"Legend and Color Scale for Heat Map\" width=\"291\" height=\"185\" class=\"aligncenter imgshadow\"><\/div>\n<h3>Define the Color Scale<\/h3>\n<p>You can choose any color scheme you want, but it is common to use different shades of the same color (for a monochromatic color scale).<\/p>\n<p><b>Important:<\/b> After editing the fill color of the cells in your legend, name each of the cells <i>color1<\/i>, <i>color2<\/i>, etc. by selecting the cells in the Color column and using the Name Box to enter the names. If named properly, when you select cell K3, the name <i>color1<\/i> should appear in the Name Box.<\/p>\n<h2 id=\"automate\">4. Automatically Update the Map Using a Macro<\/h2>\n<p>Before we create the VBA macro that updates our map, we will create a small helper table that the macro will use. We need to create formulas that will return the data value, text value, and color based on a chosen Order number.<\/p>\n<p>The following image shows the helper table and the formulas that are used to return each of the values.<\/p>\n<div class=\"center\"><img decoding=\"async\" src=\"https:\/\/cdn.vertex42.com\/blog\/images\/screenshots\/macro-helper-table-for-map.png\" alt=\"Macro Helper Table for Map\" width=\"455\" height=\"210\" class=\"aligncenter imgshadow\"><\/div>\n<p>When you (or the macro) updates the Order # to 5, the formulas create the names of the shape and text boxes using text concatenation (&amp;). The population and state abbreviations are grabbed from the data table using VLOOKUP. The color is determined using the MATCH function, which compares the data value to the Min values in the Legend.<\/p>\n<p>Named cells make VBA macros more robust, so we will name the cells in our helper table as follows: N3:<i>actorder<\/i>, N4:<i>actstate<\/i>, N5:<i>actstatevalue<\/i>, N6:<i>actcolorcode<\/i>, N7:<i>acttext<\/i>, N8:<i>acttextvalue<\/i>.<\/p>\n<p>After creating your helper table, manually change the number in the Order cell to make sure the formulas are working properly.<\/p>\n<h3>Create the Macro<\/h3>\n<p>Now we are finally ready to create the macro that will automatically loop through each shape and text box to update our heat map.<\/p>\n<p>To create the macro, open the VBA window (Alt+F11) and insert a module and copy\/paste the following code into the module. If you aren't familiar with how to create a macro using VBA, then you may want to review the article <a href=\"https:\/\/support.office.com\/en-us\/article\/Quick-start-Create-a-macro-741130ca-080d-49f5-9471-1e5fb3d581a8\" target=\"_blank\" rel=\"noopener\">Create a Macro<\/a> at support.office.com. You can use the macro recorder to record a macro named \"Paint\" which might just involve selecting a cell in the spreadsheet. You can then open the VBA editor to replace the code in your recorded macro with the following.<\/p>\n<div class=\"codebox\" style=\"margin-left:0;font-size:0.9em;\">\n<pre>\r\n<span style=\"color:blue\">Sub<\/span> Paint()\r\n  <span style=\"color:blue\">Dim<\/span> i <span style=\"color:blue\">As Integer<\/span>\r\n  <span style=\"color:blue\">For<\/span> i = 1 <span style=\"color:blue\">To<\/span> 7\r\n    <span style=\"color:green\">'First, paint the state shapes<\/span>\r\n    Range(\"<i>actorder<\/i>\").Value = i\r\n    ActiveSheet.Shapes(Range(\"<i>actstate<\/i>\").Value).Fill.ForeColor.RGB = _\r\n    Range(Range(\"<i>actcolorcode<\/i>\").Value).Interior.Color\r\n\r\n    <span style=\"color:green\">'Second, update the text boxes<\/span>\r\n    ActiveSheet.Shapes(Range(\"<i>acttext<\/i>\").Value).Select\r\n    Selection.Text = Range(\"<i>acttextvalue<\/i>\").Value\r\n    \r\n    <span style=\"color:green\">'(Optional) Format the text boxes<\/span>\r\n    Selection.ShapeRange.Fill.ForeColor.RGB = RGB(255, 255, 255)\r\n    Selection.ShapeRange.Fill.Transparency = 0.3\r\n    Selection.ShapeRange.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 0)\r\n    Selection.ShapeRange.TextFrame2.TextRange.Font.Shadow.Visible = False\r\n    Selection.ShapeRange.TextFrame2.MarginLeft = 2.5\r\n    Selection.ShapeRange.TextFrame2.MarginRight = 2.5\r\n  <span style=\"color:blue\">Next<\/span> i\r\n  ActiveSheet.Range(\"pntOrder\").Select\r\n<span style=\"color:blue\">End Sub<\/span>\r\n<\/pre>\n<\/div>\n<h3>Add a Button to run the \"Paint\" macro<\/h3>\n<p>In the Developer tab, go to Insert &gt; Button. When prompted to assign a macro to the button, choose the \"Paint\" macro you just created.<\/p>\n<p>If you are lucky, the macro will work perfectly the first time. If it doesn't work the first time and the debug tool doesn't help, check that all of the cells referenced in the macro are named correctly (including the color1, color2 ... cells).<\/p>\n<p>Remember, if you run into problems or you don't want to spend time creating your own heat map generator, you can get a ready-to-use <a href=\"https:\/\/www.someka.net\/excel-template\/usa-heat-map-generator\/\" target=\"_blank\" rel=\"noopener\">USA Geographic Heat Map Generator Template<\/a> via Someka.net.<\/p>\n<p>Geographic heat maps are great tools to visualize country\/state data. You can use them in your presentations, reports and they look really cool! You are not limited to just USA maps. With the same logic explained above you can build different geo heat maps for other countries as well.<\/p>\n<div class=\"contributor\">\n<h2>Author Bio: Onur Yilmaz<\/h2>\n<p class=\"bio\"><img decoding=\"async\" class=\"alignleft\" src=\"https:\/\/cdn.vertex42.com\/blog\/images\/profiles\/Onur-Yilmaz_96x96.jpg\" alt=\"Onur Yilmaz\" width=\"96\" height=\"96\" \/>Onur Yilmaz hosts the website <a href=\"https:\/\/www.someka.net\/\" target=\"_blank\" rel=\"noopener\">Someka.net<\/a> and has a background in engineering and finance. He specializes in template design and was the Turkey Excel Champion in 2016.<\/p>\n<\/div>\n<div class=\"clear\"><\/div>\n<h3>Editor Comments (by Jon Wittwer)<\/h3>\n<p>In the process of editing this guest post by Onur, I created a very simple generic example using custom shapes. The point was to show that you don't need to be limited to standard geographic boundaries. Although this post requires some intermediate Excel knowledge, I was very pleased with how simple it was to create my own heat map using Onur's process.<\/p>\n<div class=\"center\"><img decoding=\"async\" src=\"https:\/\/cdn.vertex42.com\/blog\/images\/screenshots\/custom-shape-heat-map-example.png\" alt=\"Custom Shape Heat Map Example\" width=\"577\" height=\"535\" class=\"aligncenter imgshadow\"><\/div>\n<p>If you'd like to analyze the example file, you can download the file I created below.<\/p>\n<p><a href=\"https:\/\/www.vertex42.com\/Files\/heat-map-example.xlsm\"><b>Download the Example File<\/b> (.xlsm)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As an alternative to Power Map or 3D Maps, learn how to create a geometric heat map in Excel using shapes and a simple VBA macro. Guest post by Onur Yilmaz of Someka.net.<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[37,78],"tags":[],"class_list":{"0":"post-3390","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-excel-help","7":"category-excel-tips"},"_links":{"self":[{"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/posts\/3390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/comments?post=3390"}],"version-history":[{"count":0,"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/posts\/3390\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/media?parent=3390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/categories?post=3390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vertex42.com\/blog\/wp-json\/wp\/v2\/tags?post=3390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}