<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>yoursumbuddy</title>
	<atom:link href="http://yoursumbuddy.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://yoursumbuddy.com</link>
	<description>Doug Glancy&#039;s Excel Site</description>
	<lastBuildDate>Thu, 06 Jun 2013 14:01:45 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Prompt to Add New Items to ComboBox or Data Validation</title>
		<link>http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/</link>
		<comments>http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/#comments</comments>
		<pubDate>Sun, 02 Jun 2013 18:10:01 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Data Management]]></category>
		<category><![CDATA[Data Validation]]></category>
		<category><![CDATA[Tables]]></category>
		<category><![CDATA[UserForm]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2826</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/">Prompt to Add New Items to ComboBox or Data Validation</a></p><p>Prompt to Add New Items to ComboBox or Data Validation Microsoft Access ComboBoxes have a handy NotinList event which allow you to check whether a value entered in a combobox is already in its list. If it&#8217;s not you can &#8230; <a href="http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/">Prompt to Add New Items to ComboBox or Data Validation</a></p><h1><a title="Prompt to Add New Items to ComboBox or Data Validation" href="http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/" rel="bookmark">Prompt to Add New Items to ComboBox or Data Validation</a></h1>
<p>Microsoft Access ComboBoxes have a handy <a href="http://msdn.microsoft.com/en-us/library/office/ff845736.aspx" title="MSDN - Access combobox NotInList property" target="_blank">NotinList </a>event which allow you to check whether a value entered in a combobox is already in its list. If it&#8217;s not you can ask the user whether to add it. This post shows how to mimic that functionality in a combobox on a VBA userform. I also show how to do the same thing with a data validation list.</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_45_hat_combo.gif"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_45_hat_combo.gif" alt="hat ComboBox" width="376" height="216" class="alignnone size-full wp-image-2831" /></a></p>
<p><strong>Creating a ComboBox NotInList Event</strong></p>
<p>The key to doing this is checking the value of the ComboBox&#8217;s &#8220;MatchFound&#8221; property in its Exit event. If no match is found, we ask the user whether to add the item to the list of valid items (hats in this case). If the answer is &#8220;Yes&#8221; then a row with the hat is added to the table. If not, we clear the combobox and keep the focus on it. You can see this in action in the video above. </p>
<p>Here&#8217;s the code for the combobox&#8217;s Exit event:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Private Sub cboHats_Exit(ByVal Cancel As MSForms.ReturnBoolean)<br />
Dim loValidationSource As Excel.ListObject<br />
Dim loRow As Excel.ListRow<br />
<br />
'the Table with the list of valid hats<br />
Set loValidationSource = wsTables.ListObjects(&quot;tblValidationSource&quot;)<br />
With Me.cboHats<br />
&nbsp; &nbsp; 'We're only interested if these aren't true<br />
&nbsp; &nbsp; If .MatchFound Or .Value = &quot;&quot; Or .Value = STARTING_VALUE Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; Exit Sub<br />
&nbsp; &nbsp; End If<br />
&nbsp; &nbsp; 'If the hat entered isn't in list, prompt to add it<br />
&nbsp; &nbsp; If MsgBox(.Value &amp; &quot; is not in the list. Add it?&quot;, vbYesNo + vbDefaultButton2 + vbQuestion) = vbYes Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; Set loRow = loValidationSource.ListRows.Add<br />
&nbsp; &nbsp; &nbsp; &nbsp; loRow.Range.Cells(1).Value = .Value<br />
&nbsp; &nbsp; &nbsp; &nbsp; SortSourceTable<br />
&nbsp; &nbsp; &nbsp; &nbsp; RefreshComboList<br />
&nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; 'if &quot;no&quot;, keep focus on the ComboBox and set it's value to &quot;Choose a hat&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Cancel = True<br />
&nbsp; &nbsp; &nbsp; &nbsp; Me.cboHats.Value = STARTING_VALUE<br />
&nbsp; &nbsp; End If<br />
End With<br />
End Sub</div></div>
<p>One important thing is that the combobox&#8217;s &#8220;MatchRequired&#8221; property must be set to False (which is the default). Otherwise the Exit will be preempted by an &#8220;Invalid Property Value&#8221; message from Excel.</p>
<p><strong>Creating a Data Validation NotInList Event</strong></p>
<p>As with the combobox version, we use an event to prompt the user whether to add an item that&#8217;s not in the list. This time we use our own &#8220;MatchFound&#8221; function to check against the data validation&#8217;s source list. Similar to setting the &#8220;Match Required&#8221; combobox property to False, the data validation version requires that the &#8220;Show error alert after invalid data is entered&#8221; is unchecked in the data validation setup dialog. This is obviously not the default:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_0045_data_val_setup.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_0045_data_val_setup.jpg" alt="data validation setup" width="429" height="496" class="alignnone size-full wp-image-2843" /></a></p>
<p>Since I&#8217;m working in Excel 2010, I&#8217;ve created a single-column table (listobject) to hold the valid items. I then simply pointed the data validation&#8217;s Source property at the column, excluding the header. Because the source is in a table, it&#8217;s dynamic &#8211; it adjusts when you add or remove items from the column. No dynamic ranges are required, just select the cells:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_0045_data_val_source_list.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_0045_data_val_source_list.jpg" alt="data validation source list" width="601" height="403" class="alignnone size-full wp-image-2849" /></a></p>
<p>Here&#8217;s the code from the ThisWorkbook module, which contains the Workbook_SheetChange event and the MatchFound function:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Private</span> <span style="color: #E56717; font-weight: bold;">Sub</span> Workbook_SheetChange(<span style="color: #151B8D; font-weight: bold;">ByVal</span> Sh <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Object</span>, <span style="color: #151B8D; font-weight: bold;">ByVal</span> Target <span style="color: #151B8D; font-weight: bold;">As</span> Range)<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> cell <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> loValidationSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.ListObject<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> loHatCollection <span style="color: #151B8D; font-weight: bold;">As</span> Excel.ListObject<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> loRow <span style="color: #151B8D; font-weight: bold;">As</span> Excel.ListRow<br />
<br />
<span style="color: #008000;">'wsTables is the sheet's CodeName<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> Sh <span style="color: #8D38C9; font-weight: bold;">Is</span> wsTables <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; <span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #E56717; font-weight: bold;">Sub</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<span style="color: #151B8D; font-weight: bold;">Set</span> loValidationSource = wsTables.ListObjects(<span style="color: #800000;">&quot;tblValidationSource&quot;</span>)<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> loHatCollection = wsTables.ListObjects(<span style="color: #800000;">&quot;tblHatCollection&quot;</span>)<br />
<span style="color: #008000;">'only continue if change is in column with data validation<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> Intersect(Target, loHatCollection.ListColumns(<span style="color: #800000;">&quot;Hat Type&quot;</span>).DataBodyRange) <span style="color: #8D38C9; font-weight: bold;">Is</span> <span style="color: #00C2FF; font-weight: bold;">Nothing</span> <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; <span style="color: #E56717; font-weight: bold;">Exit</span> <span style="color: #E56717; font-weight: bold;">Sub</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<span style="color: #8D38C9; font-weight: bold;">With</span> Intersect(Target, loHatCollection.ListColumns(<span style="color: #800000;">&quot;Hat Type&quot;</span>).DataBodyRange)<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> <span style="color: #8D38C9; font-weight: bold;">Each</span> cell <span style="color: #8D38C9; font-weight: bold;">In</span> .Cells<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> MatchFound(cell.Value) = <span style="color: #00C2FF; font-weight: bold;">False</span> <span style="color: #8D38C9; font-weight: bold;">And</span> cell.Value &lt;&gt; <span style="color: #800000;">&quot;&quot;</span> <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> MsgBox(cell.Value &amp; <span style="color: #800000;">&quot; is not in the list. Add it?&quot;</span>, vbYesNo + vbDefaultButton2 + vbQuestion) = vbYes <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Set</span> loRow = loValidationSource.ListRows.Add<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loRow.Range.Cells(1).Value = cell.Value2<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cell.ClearContents<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> cell<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
SortSourceTable<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span><br />
<br />
<span style="color: #E56717; font-weight: bold;">Function</span> MatchFound(ValueToCheck <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> loValidationSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.ListObject<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> ValidationList <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range<br />
<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> loValidationSource = wsTables.ListObjects(<span style="color: #800000;">&quot;tblValidationSource&quot;</span>)<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> ValidationList = loValidationSource.ListColumns(<span style="color: #800000;">&quot;Hats Validation List&quot;</span>).DataBodyRange<br />
MatchFound = Application.WorksheetFunction.CountIf(ValidationList, ValueToCheck) &gt; 0<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>And here&#8217;s what it looks like in action:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_0045_data_val_prompt.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/06/Post_0045_data_val_prompt.jpg" alt="data validation prompt" width="310" height="327" class="alignnone size-full wp-image-2844" /></a></p>
<p><strong>The Sort object &#8211; Excel 2007 Onwards</strong></p>
<p>My code uses <a href="http://msdn.microsoft.com/en-us/library/office/bb178505%28v=office.12%29.aspx" title="MS Sort Object reference page" target="_blank">VBA&#8217;s Sort object</a>, which appeared in Excel 2007. I like the way it works. You add Sort Fields, just as you do in the user interface, and then apply the sort when needed. If you are using Excel 2003 or earlier you&#8217;d need to re-write the two sorting procedures to work with your version.</p>
<p>Also, if you are using Excel 2003 or earlier, see this <a href="http://blog.contextures.com/archives/2010/04/12/automatically-add-new-items-to-excel-data-validation-drop-down/" title="Contextures Add New Data Validation Items post" target="_blank">Contextures post</a> for a non-table way of automatically adding items to a data validation list. You could easily add the code to prompt the user whether to do so.</p>
<p><strong>Download</strong></p>
<p>Here&#8217;s a <a href="http://yoursumbuddy.com/downloads/post_0045_prompt_to_add_validation_items.zip" target="_blank">workbook</a> with all the code for both versions.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/prompt-to-add-new-items-to-combobox-or-data-validation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Filter Pivot Tables Using Source Data Helper Columns</title>
		<link>http://yoursumbuddy.com/filter-pivot-tables-using-source-data-helper-columns/</link>
		<comments>http://yoursumbuddy.com/filter-pivot-tables-using-source-data-helper-columns/#comments</comments>
		<pubDate>Sun, 19 May 2013 17:08:11 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Data Management]]></category>
		<category><![CDATA[Formulas]]></category>
		<category><![CDATA[Pivot Tables]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2678</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/filter-pivot-tables-using-source-data-helper-columns/">Filter Pivot Tables Using Source Data Helper Columns</a></p><p>Filter Pivot Tables Using Source Data Helper Columns When working with pivot tables you often need to filter out certain items. You can of course do this directly in the fields&#8217; filter dropdowns, and often that&#8217;s good enough. But if &#8230; <a href="http://yoursumbuddy.com/filter-pivot-tables-using-source-data-helper-columns/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/filter-pivot-tables-using-source-data-helper-columns/">Filter Pivot Tables Using Source Data Helper Columns</a></p><h1><a title="Filter Pivot Tables Using Source Data Helper Columns" href="http://yoursumbuddy.com/filter-pivot-tables-using-source-data-helper-columns/" rel="bookmark">Filter Pivot Tables Using Source Data Helper Columns</a></h1>
<p>When working with pivot tables you often need to filter out certain items. You can of course do this directly in the fields&#8217; filter dropdowns, and often that&#8217;s good enough. But if you change the filters repeatedly this can be tedious and error-prone. For this reason, and to make my workbooks more flexible and maintainable, I often add helper columns to the source data. This post explains three types of source data helper columns that I use to filter pivot tables.</p>
<p>The live workbook below contains a simple data set of All-Star baseball games, where they were held, and which league won. It also contains three helper columns, each using a different method to determine the rows included in the pivot table. All three techniques refer to tables that spell out the criteria for inclusion.</p>
<p><iframe width="700" height="650" frameborder="0" scrolling="no" src="https://skydrive.live.com/embed?cid=D71DDEC264BBBD69&#038;resid=D71DDEC264BBBD69%211065&#038;authkey=AJ8puZEt-qouO4o&#038;em=2&#038;AllowTyping=True&#038;ActiveCell='data%20-%20All%20Star%20games'!A1&#038;wdHideGridlines=True&#038;wdHideHeaders=True&#038;wdDownloadButton=True"></iframe><br />
<em>(You can switch tabs, edit cells and refresh pivot tables. Reloading this page reverts it to its starting state. Note the buttons for downloading or opening in a full web page.)</em></p>
<p>You can click into the formulas in the helper columns and see that they are referring to tables &#8211; a different one for each column. Each formula uses a COUNTIF or COUNTIFS function to determine if the venue for that row meets certain criteria.</p>
<p>When you click on the &#8220;pivot and helper tables&#8221; tab you&#8217;ll see the pivot table on the left and the three helper tables on the right. The pivot table has a report filter for each of the three helper columns. Note that the report filters, table headers and helper columns are color-coded to show which ones go together.</p>
<p>Okay, let&#8217;s look at the three methods in order.</p>
<p><strong>Table Containing Only Values to Include</strong></p>
<p>Back on the data tab, the helper column &#8220;Venue In Table&#8221; looks at the first table, the one titled &#8220;Venues to Include&#8221;. The formula is a simple one:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=COUNTIF(tblVenuesToInclude[Venues to Include],[@Venue])&gt;0</div></div>
<p>It returns TRUE if the ballpark for that row is found in the table. Its report filter is already set to TRUE in the pivot table, so you can test it by adding or deleting a stadium name in the table, right-clicking the pivot table and clicking &#8220;refresh&#8221;. </p>
<p><strong>Table With All Values and an &#8220;Include&#8221; column</strong></p>
<p>The second helper column/table pair are very similar, but instead of a table listing only the baseball fields you want included, you list all of them and add another column that contains TRUE if the ballpark should be included. I might use this method if I had a table with all the venues that I was already using for another purpose:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=COUNTIFS(tblVenuesIncludeCol[Venue],[@Venue],tblVenuesIncludeCol[Include],TRUE)&gt;0</div></div>
<p>The COUNTIFS formula checks both the Venue and the Include columns. (You can use a SUMPRODUCT formula if you&#8217;re using Excel 2003 or earlier). You can test this formula by changing its report filter to TRUE and then changing the values in the Include column of the 2nd table.</p>
<p><strong>Table Containing Words to Look For</strong></p>
<p>The final helper column is &#8220;Venue Word in Table&#8221;. It looks into the &#8220;Word to Find&#8221; table and uses a COUNTIF <a href="http://www.cpearson.com/excel/ArrayFormulas.aspx" title="Chip Pearson's array formula page" target="_blank">array formula</a> that searches the venue&#8217;s name for words from the table. Changing the report filter to TRUE will find the Kingdome, Polo Grounds and any park whose name contains &#8220;field&#8221;.</p>
<p>Here&#8217;s the formula, entered with Ctrl-Shift-Enter:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">{=MAX(COUNTIF([@Venue],&quot;*&quot; &amp; tblWordToFind[Word to Find] &amp; &quot;*&quot;))&gt;0}</div></div>
<p>This formula takes advantage of the ability to use wildcards in COUNTIF functions, by putting asterisks before and after the table column reference. Being an array formula, it tests the venue name against each word in the &#8220;Word to Find&#8221; table. The 1934 data row returns TRUE because &#8220;Polo Grounds&#8221; contains the word &#8220;polo&#8221;, which is in the table. </p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/05/Post_0044_helper_3_and_table.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/05/Post_0044_helper_3_and_table.jpg" alt="helper 3 and table" width="483" height="197" class="alignnone size-full wp-image-2767" /></a></p>
<p>If we use the <a href="http://chandoo.org/wp/2008/12/15/formula-debugging-tips-excel/" title="Chandoo's F9 explanation" target="_blank">F9</a> key to successively evaluate parts of the function it looks like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=MAX(COUNTIF([@Venue],&quot;*&quot; &amp; tblWordToFind[Word to Find] &amp; &quot;*&quot;))&gt;0</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=MAX(COUNTIF([@Venue],&quot;*&quot; &amp; {&quot;king&quot;;&quot;polo&quot;;&quot;field&quot;} &amp; &quot;*&quot;))&gt;0</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=MAX(COUNTIF(&quot;Polo Grounds&quot;,&quot;*&quot; &amp; {&quot;king&quot;;&quot;polo&quot;;&quot;field&quot;} &amp; &quot;*&quot;))&gt;0</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=MAX({0;1;0})&gt;0</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=1&gt;0</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">TRUE</div></div>
<p>One final note: In actual practice, I always put the helper tables on a separate sheet, not next to the pivot table as done here. Expanding pivot tables, among other things, makes this layout impractical in real use.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/filter-pivot-tables-using-source-data-helper-columns/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Single Quotes in Worksheet Names</title>
		<link>http://yoursumbuddy.com/single-quotes-in-worksheet-names/</link>
		<comments>http://yoursumbuddy.com/single-quotes-in-worksheet-names/#comments</comments>
		<pubDate>Fri, 19 Apr 2013 04:49:31 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Excel interface]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2684</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/single-quotes-in-worksheet-names/">Single Quotes in Worksheet Names</a></p><p>Single Quotes in Worksheet Names I was working on a function that uses regular expressions to determine whether a potential name for a workbook, worksheet or range contains illegal characters. I started by writing a little routine to determine which &#8230; <a href="http://yoursumbuddy.com/single-quotes-in-worksheet-names/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/single-quotes-in-worksheet-names/">Single Quotes in Worksheet Names</a></p><h1><a title="Single Quotes in Worksheet Names" href="http://yoursumbuddy.com/single-quotes-in-worksheet-names/" rel="bookmark">Single Quotes in Worksheet Names</a></h1>
<p>I was working on a function that uses regular expressions to determine whether a potential name for a workbook, worksheet or range contains illegal characters. I started by writing a little routine to determine which characters are illegal for sheet names. Of course, I could have just used one that I knew was prohibited and got the message below. But then I might never have thought about the use of single quotes in worksheet names.</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_illegal_char_message.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_illegal_char_message.jpg" alt="Illegal worksheet character message" width="467" height="191" class="alignnone size-full wp-image-2687" /></a></p>
<p>Here&#8217;s the code I used:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> IllegalWsNameCharacters()<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<br />
<span style="color: #8D38C9; font-weight: bold;">With</span> ActiveSheet<br />
&nbsp; &nbsp; .Range(<span style="color: #800000;">&quot;A1&quot;</span>).Cells.ClearContents<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> 127<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .Name = Chr(i)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> Err.Number &lt;&gt; 0 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Range(<span style="color: #800000;">&quot;A1&quot;</span>).Value = .Range(<span style="color: #800000;">&quot;A1&quot;</span>).Value &amp; Chr(i)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p>It cycles through each of the 128 ASCII characters and tries to use it as the ActiveSheet&#8217;s name. An error means that character is illegal, at least by itself, so it&#8217;s added to the contents of cell A1. I don&#8217;t know why I printed to a cell instead of the Immediate window, but it kept me from immediately (ha!) noticing that the first illegal character was a single quote or, as normal people say, an apostrophe.</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_single_quote_in_cell.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_single_quote_in_cell.jpg" alt="Single quote in cell" width="218" height="99" class="alignnone size-full wp-image-2692" /></a></p>
<p>You can see it doesn&#8217;t show up in the cell contents, only in the formula box. That&#8217;s because the single quote is the escape character that tells Excel that <a href="http://books.google.com/books?id=Pm8pEIHFIdQC&#038;pg=PA206&#038;lpg=PA206&#038;dq=walkenbach+apostrophe+number+text&#038;source=bl&#038;ots=_f5ozuNHrW&#038;sig=uRNQN2Zp9woufSRD6-kqev2_gI0&#038;hl=en&#038;sa=X&#038;ei=NshwUdTHNsG7igKEtoC4DQ&#038;ved=0CDIQ6AEwAA#v=onepage&#038;q=walkenbach%20apostrophe%20number%20text&#038;f=false" title="John Walkenback 2007 Bible - page 206"target=_blank>whatever follows it is text</a>, and part of its duty is to stay quietly out of sight.</p>
<p>At any rate, I saw it and was surprised at it&#8217;s illegality. I&#8217;m pretty sure I&#8217;ve used an apostrophe in a sheet name before. And, sure enough you can:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_name_with_single_quote.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_name_with_single_quote.jpg" alt="Name with single quote" width="450" height="75" class="alignnone size-full wp-image-2696" /></a></p>
<p>What you can&#8217;t do is use it as the first or last character in a sheet name. And this makes sense, since single quotes are how Excel surrounds worksheet and workbook names that have spaces in them, as in:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">='A spaced out name'!A1 * serious_wks_name!A1</div></div>
<p>All in all, it seems you are best off leaving single quotes out of your sheet names. A quick web search reveals issues with hyperlinks, OLEDB references and Excel Services REST, whatever that is.</p>
<p>Finally, and interestingly, you <em>can </em>name your sheets with some of the &#8220;nonprintable&#8221; ASCII character codes, i.e., characters 0 to 31, if only through VBA:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ActiveSheet.Name = Chr(8) &amp; &quot; &quot; &amp; Chr(12) &amp; &quot; &quot; &amp; Chr(17) &amp; &quot; &quot; &amp; Chr(15) &amp; &quot; &quot; &amp; Chr(14)</div></div>
<p>
<p>
Mind you, I&#8217;m not recommending it.</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_name_with_nonprintable_characters.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0043_name_with_nonprintable_characters.jpg" alt="Worksheet name with nonprintable characters" width="298" height="226" class="alignnone size-full wp-image-2699" /></a></p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/single-quotes-in-worksheet-names/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Why Pi is Irrational &#8211; In Pictures</title>
		<link>http://yoursumbuddy.com/why-pi-is-irrational-in-pictures/</link>
		<comments>http://yoursumbuddy.com/why-pi-is-irrational-in-pictures/#comments</comments>
		<pubDate>Wed, 10 Apr 2013 03:25:56 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2605</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/why-pi-is-irrational-in-pictures/">Why Pi is Irrational &#8211; In Pictures</a></p><p>Why Pi is Irrational &#8211; In Pictures I was a good math student in junior high, and I&#8217;m sure I did well on all my pi-related homework and test questions. I understood that pi was an irrational number, and knew, &#8230; <a href="http://yoursumbuddy.com/why-pi-is-irrational-in-pictures/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/why-pi-is-irrational-in-pictures/">Why Pi is Irrational &#8211; In Pictures</a></p><h1><a title="Why Pi is Irrational - In Pictures" href="http://yoursumbuddy.com/why-pi-is-irrational-in-pictures/" rel="bookmark">Why Pi is Irrational &#8211; In Pictures</a></h1>
<p>I was a good math student in junior high, and I&#8217;m sure I did well on all my pi-related homework and test questions. I understood that pi was an <a href="http://en.wikipedia.org/wiki/Irrational_number" title="Wikipedia pi article" target="_blank">irrational number</a>, and knew, theoretically, that it had an endless number of non-repeating decimals. However, I had no real grasp of <strong><em>why</em></strong> it&#8217;s irrational. It was just another abstract, memorized math fact. But a few years back a realization came to me. So, in belated celebration of <a href="http://www.piday.org/" title="Pi Day official website" target="_blank">Pi Day</a> on March 14, I offer my pictorial explanation of why pi is irrational.</p>
<p>The realization came when thinking about the formula for an area of a circle, &Pi; r<sup>2</sup> (pi * r<sup>2</sup>) where r is the circle&#8217;s radius. It dawned on me that the r<sup>2</sup> referred to a real geometric square with sides and corners and all, not just a letter with a number above it, and that:</p>
<blockquote><p>Pi represents the number of squares with a side of length r that would fill a circle with a radius of length r.</p></blockquote>
<p>Therefore, my explanation of why pi&#8217;s digits are infinite is that no matter how many squares you put inside a circle, there will always be a smaller &#8220;corner&#8221; into which you can cram a smaller square. This leaves an even smaller corner for an even smaller square, and on and on without end.</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0042_Pi_Squares_1.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0042_Pi_Squares_1.jpg" alt="Pi Squares 1" width="329" height="179" class="alignnone size-full wp-image-2612" /></a></p>
<p>I don&#8217;t know whether this is a mathematically valid explanation. But transforming this abstract concept into something I could draw with a pencil pleased me no end.</p>
<p><strong>Pi and VBA</strong></p>
<p>A pencil is one thing, but on Pi Day I decided to illustrate my explanation with Excel and VBA. It took me a really long time! First I tried using shapes, a reasonable, but really bad, idea. I ended up using just one shape, a circle, and a bunch of tiny cells, which are, after all, squares:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_42_Pi_Squares_2.gif"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_42_Pi_Squares_2.gif" alt="Pi Squares 2" width="700" height="618" class="alignnone size-full wp-image-2613" /></a></p>
<p>The circle above has a radius of 50. When all the squares that fit are filled in, their total area is about 3.08 times that of a square with a radius of 50. In other words, it&#8217;s fairly close to pi. If I change the settings to a radius of 100, the number climbs above 3.1, but still short of pi.</p>
<p>Programming this was challenging and fun. Because of the nature of the project I coded some things more loosely than normal. There&#8217;s a bunch of global variables, and even a &#8220;Select Case True&#8221; statement.</p>
<p>I did a lot of Unioning and Intersecting in the code. One thing I rediscovered is a major glitch with the Union statement, which gives you an <a href="http://www.cpearson.com/excel/BetterUnion.aspx" title="Chip Pearson's Better Union page" target="_blank">incorrect cell count</a> for the Union of two overlapping ranges:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0042_Union_wrong_count.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/04/Post_0042_Union_wrong_count.jpg" alt="Union wrong count" width="438" height="75" class="alignnone size-full wp-image-2654" /></a></p>
<p><strong>Two Miscellaneous Things</strong></p>
<p>My search for a screen-capture-to-animated-GIF program continues. I had been using Cropper, but it was unable to keep up with the circle&#8217;s &#8220;exit stage left&#8221; in the animation above. As you can see, I&#8217;m now trying ScreenCast-o-Matic, which has a nice interface, and both desktop and web versions. For $15 I can upgrade and remove their logo from the video, but when I tried this morning their website refused to take my money.</p>
<p>Finally, on Pi Day&#8217;s website it claims to be celebrated all around the world. It must be a fairly abstract reference in countries that use the, more logical, <a href="http://en.wikipedia.org/wiki/Date_format_by_country" title="Wikipedia - date format by country" target="_blank">DMY date system</a>, e.g., 14/3/2013.</p>
<p><strong>Download!</strong></p>
<p>Here&#8217;s a <a href="http://yoursumbuddy.com/downloads/Post_0042_pi_squares.zip" target="_blank">workbook</a> with the code.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/why-pi-is-irrational-in-pictures/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>No IFs in Conditional Formatting Formulas</title>
		<link>http://yoursumbuddy.com/no-ifs-in-conditional-formatting-formulas/</link>
		<comments>http://yoursumbuddy.com/no-ifs-in-conditional-formatting-formulas/#comments</comments>
		<pubDate>Fri, 15 Mar 2013 05:50:56 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Conditional Formatting]]></category>
		<category><![CDATA[Formulas]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2574</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/no-ifs-in-conditional-formatting-formulas/">No IFs in Conditional Formatting Formulas</a></p><p>No IFs in Conditional Formatting Formulas From time to time I&#8217;ll answer a question on Stack Overflow about a Conditional Formatting formula that includes an IF statement. Something like this: =IF(A2=&#8221;Something&#8221;,TRUE,FALSE) I&#8217;ll politely let them know you don&#8217;t need IFs &#8230; <a href="http://yoursumbuddy.com/no-ifs-in-conditional-formatting-formulas/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/no-ifs-in-conditional-formatting-formulas/">No IFs in Conditional Formatting Formulas</a></p><h1><a title="No IFs in Conditional Formatting Formulas" href="http://yoursumbuddy.com/no-ifs-in-conditional-formatting-formulas/" rel="bookmark">No IFs in Conditional Formatting Formulas</a></h1>
<p>From time to time I&#8217;ll answer a question on <a href="http://stackoverflow.com/users/293078/doug-glancy" title="shameless self-promotion" target="_blank">Stack Overflow</a> about a Conditional Formatting formula that includes an IF statement. Something like this:</p>
<p>=IF(A2=&#8221;Something&#8221;,TRUE,FALSE)</p>
<p>I&#8217;ll politely let them know you don&#8217;t need IFs in conditional formatting formulas, and that their formula can be reduced to something like:</p>
<p>=A2=&#8221;Something&#8221;</p>
<p>I&#8217;m going to go out on a limb and say there&#8217;s never a need for an IF in a conditional formatting formula.</p>
<p><strong>Why &#8220;No IFs?&#8221;</strong></p>
<p>What I usually say quickly in my answers is that the IF is implied by the fact that it&#8217;s a <em>conditional</em> formula. In other words, when you write the formula you&#8217;re telling the conditional formatting:</p>
<p>&#8220;If this condition is true, then apply the format, otherwise don&#8217;t apply it&#8221;. You only need the condition, the rest is a given.</p>
<p>Further, there will never be a reason for a nested IF. Nested IFs only apply when there are more than two possible outcomes. And again, in CF there can only be two: formatting applied or formatting not applied. You might very well, of course, use ORs and ANDs, as those allow you to narrow down the condition: </p>
<p>=AND(A2=&#8221;Something&#8221;,A3=&#8221;Else&#8221;)</p>
<p>But it&#8217;s still only one condition with two possible outcomes.</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/03/Post_0041_something_else.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/03/Post_0041_something_else.jpg" alt="something else" width="542" height="513" class="alignnone size-full wp-image-2589" /></a></p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/no-ifs-in-conditional-formatting-formulas/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>VBA FormatConditions &#8211; Per-Row ColorScales, DataBars and IconSets</title>
		<link>http://yoursumbuddy.com/vba-formatconditions-per-row-colorscales-databars-and-iconsets/</link>
		<comments>http://yoursumbuddy.com/vba-formatconditions-per-row-colorscales-databars-and-iconsets/#comments</comments>
		<pubDate>Sun, 17 Feb 2013 21:43:33 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Conditional Formatting]]></category>
		<category><![CDATA[Excel 2007/10/13]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2485</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/vba-formatconditions-per-row-colorscales-databars-and-iconsets/">VBA FormatConditions &#8211; Per-Row ColorScales, DataBars and IconSets</a></p><p>VBA FormatConditions &#8211; Per-Row ColorScales, DataBars and IconSets At the end of Conditional Formatting Per-Row Color Scales I said I&#8217;d be back with code to selectively copy IconSets and DataBars on a per-row basis as well. It took some doing &#8230; <a href="http://yoursumbuddy.com/vba-formatconditions-per-row-colorscales-databars-and-iconsets/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/vba-formatconditions-per-row-colorscales-databars-and-iconsets/">VBA FormatConditions &#8211; Per-Row ColorScales, DataBars and IconSets</a></p><h1><a title="VBA FormatConditions - Per-Row ColorScales, DataBars and IconSets" href="http://yoursumbuddy.com/vba-formatconditions-per-row-colorscales-databars-and-iconsets/" rel="bookmark">VBA FormatConditions &#8211; Per-Row ColorScales, DataBars and IconSets</a></h1>
<p>At the end of <a href="http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/" title="Conditional Formatting Per-Row Color Scales" target="_blank">Conditional Formatting Per-Row Color Scales</a> I said I&#8217;d be back with code to selectively copy IconSets and DataBars on a per-row basis as well. It took some doing but it&#8217;s now presentable. I learned a lot about programming these types of FormatConditions and I&#8217;m going to use this utility to generalize what I learned.</p>
<p>First, here&#8217;s a sample of the code in action. Some silly guy has added Icons, Data Bars and Color Scales to the values in Row 1. Now he&#8217;s copying only the ColorScales from that first row to the rows below:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_CF_copied.gif"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_CF_copied.gif" alt="Conditional formatting copied" width="715" height="365" class="alignnone size-full wp-image-2514" /></a></p>
<p>I (okay, it&#8217;s me) adjust the setting and push the button. The button routine passes the values in the worksheet to the main routine. It&#8217;s an expanded version of the code in the previous post, with an addition to delete selected formats. The main procedure is available in the download workbook linked at the end of this post. (The delete code is interesting, and shown later in this post.)</p>
<p><strong>ColorScale, IconSet and DataBar Subroutines</strong></p>
<p>Below are the three subroutines, called from the main procedure, to copy each type of FormatCondition: ColorScales, IconSets and DataBars. </p>
<p>The structure of each of these objects is different. With the exception of the Formula property, ColorScale has no relevant properties in the base object. The properties that need to be copied are all within its ColorScaleCriterion object. IconSets have relevant properties both within the IconSet object and within its IconCriterion object. Finally, DataBars contain no criterion object. All of its relevant properties are set within the base DataBar object. </p>
<p><em><strong>ColorScale</strong></em></p>
<p>Here&#8217;s the ColorScale subroutine. You can see that the only property in the ColorScale object is the Formula property, which is an interesting one. As far as I can tell this property is only applicable through code. I&#8217;ll discuss it more below. It&#8217;s only sometimes applicable, and if it&#8217;s not it will generate an error when you refer to it. So I surround it with and On Error statement to avoid that.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> SetRangeColorScale(rngTargetSection <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, csSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.ColorScale)<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> csTarget <span style="color: #151B8D; font-weight: bold;">As</span> ColorScale<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> csCriterion <span style="color: #151B8D; font-weight: bold;">As</span> ColorScaleCriterion<br />
<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> csTarget = rngTargetSection.FormatConditions.AddColorScale(csSource.<span style="color: #151B8D; font-weight: bold;">Type</span>)<br />
<span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
csTarget.Formula = csSource.Formula<br />
<span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
<span style="color: #8D38C9; font-weight: bold;">For</span> <span style="color: #8D38C9; font-weight: bold;">Each</span> csCriterion <span style="color: #8D38C9; font-weight: bold;">In</span> csSource.ColorScaleCriteria<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">With</span> csTarget.ColorScaleCriteria(csCriterion.Index)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #151B8D; font-weight: bold;">Type</span> = csCriterion.<span style="color: #151B8D; font-weight: bold;">Type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .Value = isCriterion.Value<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
&nbsp; &nbsp; &nbsp; &nbsp; .FormatColor.Color = csCriterion.FormatColor.Color<br />
&nbsp; &nbsp; &nbsp; &nbsp; .FormatColor.TintAndShade = csCriterion.FormatColor.TintAndShade<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
<span style="color: #8D38C9; font-weight: bold;">Next</span> csCriterion<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p>Aside from Formula, the properties that need to be copied are part of ColorScale&#8217;s ColorScaleCriterion object. They are Type, and the Color and TintAndShade shade properties of ColorScaleCriterion.FormatColor.</p>
<p>The Type property specifies whether a criterion is based on a number, percent, percentile or formula. Value sets the value to be used for the Type, for example, the 90th percentile or the appropriate formula. These properties all have their counterpart in the conditional formatting dialog, shown here with a formula for the Min and Max values:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_color_scale_with_formula.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_color_scale_with_formula.jpg" alt="Color scale with formula" width="567" height="491" class="alignnone size-full wp-image-2522" /></a></p>
<p>I surrounded the Type and Value assignments in an On Error statement because only some of the criteria have them  and you&#8217;ll get an error on the ones that don&#8217;t. Sheesh, this stuff is confusing!</p>
<p><em><strong>IconSet</strong></em></p>
<p>Aside from Formula, the IconSet object has three properties that need to be copied: IconSet, ReverseOrder and ShowIconOnly. IconSet determines the color and type of icons and the others should be obvious:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> SetRangeIconset(rngTargetSection <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, isSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.IconSetCondition)<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> isTarget <span style="color: #151B8D; font-weight: bold;">As</span> IconSetCondition<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> isCriterion <span style="color: #151B8D; font-weight: bold;">As</span> IconCriterion<br />
<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> isTarget = rngTargetSection.FormatConditions.AddIconSetCondition<br />
<span style="color: #8D38C9; font-weight: bold;">With</span> isTarget<br />
&nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp; .Formula = isSource.Formula<br />
&nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
&nbsp; &nbsp; .IconSet = isSource.IconSet<br />
&nbsp; &nbsp; .ReverseOrder = isSource.ReverseOrder<br />
&nbsp; &nbsp; .ShowIconOnly = isSource.ShowIconOnly<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> <span style="color: #8D38C9; font-weight: bold;">Each</span> isCriterion <span style="color: #8D38C9; font-weight: bold;">In</span> isSource.IconCriteria<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">With</span> .IconCriteria(isCriterion.Index)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Icon = isCriterion.Icon<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #151B8D; font-weight: bold;">Type</span> = isCriterion.<span style="color: #151B8D; font-weight: bold;">Type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Value = isCriterion.Value<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Operator = isCriterion.Operator<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> isCriterion<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p>At the IconCriterion level, there are four properties: Icon, Type, Value and Operator. Icon allows you to change individual icons, just like you can in the user interface. For example, here the gray arrow icon set has been modified to use a green ball in place of the gray up arrow:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_changed_icon.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_changed_icon.jpg" alt="Changed icon" width="476" height="86" class="alignnone size-full wp-image-2520" /></a></p>
<p>Operator sets the relationship of the Icon to the Value. For example, the &#8220;>&#8221; Operator says to use an up arrow if the cell&#8217;s value is greater than the Value property.</p>
<p><em><strong>DataBar</strong></em></p>
<p>The DataBar object has no criterion property. All the relevant properties are at the DataBar level. So instead of criteria for the Min and Max, you get the MinPoint and MaxPoint properties. These two made me nervous when I first saw them, as you have to set them via their Modify methods, but that seems to work fine.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> SetRangeDataBar(rngTargetSection <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, dbSource <span style="color: #151B8D; font-weight: bold;">As</span> Databar)<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> dbTarget <span style="color: #151B8D; font-weight: bold;">As</span> Databar<br />
<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> dbTarget = rngTargetSection.FormatConditions.AddDatabar<br />
<span style="color: #8D38C9; font-weight: bold;">With</span> dbTarget<br />
&nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span><br />
&nbsp; &nbsp; .Formula = dbSource.Formula<br />
&nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">With</span> .AxisColor<br />
&nbsp; &nbsp; &nbsp; &nbsp; .Color = dbSource.AxisColor.Color<br />
&nbsp; &nbsp; &nbsp; &nbsp; .TintAndShade = dbSource.AxisColor.TintAndShade<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
&nbsp; &nbsp; .AxisPosition = dbSource.AxisPosition<br />
&nbsp; &nbsp; .BarBorder.<span style="color: #151B8D; font-weight: bold;">Type</span> = dbSource.BarBorder.<span style="color: #151B8D; font-weight: bold;">Type</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">With</span> .BarColor<br />
&nbsp; &nbsp; &nbsp; &nbsp; .Color = dbSource.BarColor.Color<br />
&nbsp; &nbsp; &nbsp; &nbsp; .TintAndShade = dbSource.BarColor.TintAndShade<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
&nbsp; &nbsp; .BarFillType = dbSource.BarFillType<br />
&nbsp; &nbsp; .Direction = dbSource.Direction<br />
&nbsp; &nbsp; .MinPoint.Modify newtype:=dbSource.MinPoint.<span style="color: #151B8D; font-weight: bold;">Type</span>, newvalue:=dbSource.MinPoint.Value<br />
&nbsp; &nbsp; .MaxPoint.Modify newtype:=dbSource.MaxPoint.<span style="color: #151B8D; font-weight: bold;">Type</span>, newvalue:=dbSource.MaxPoint.Value<br />
&nbsp; &nbsp; .NegativeBarFormat.ColorType = dbSource.NegativeBarFormat.ColorType<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">With</span> .NegativeBarFormat.Color<br />
&nbsp; &nbsp; &nbsp; &nbsp; .Color = dbSource.NegativeBarFormat.Color.Color<br />
&nbsp; &nbsp; &nbsp; &nbsp; .TintAndShade = dbSource.NegativeBarFormat.Color.TintAndShade<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
&nbsp; &nbsp; .PercentMax = dbSource.PercentMax<br />
&nbsp; &nbsp; .PercentMin = dbSource.PercentMin<br />
&nbsp; &nbsp; .ShowValue = dbSource.ShowValue = <span style="color: #00C2FF; font-weight: bold;">True</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p>There&#8217;s a whole bunch of properties besides MinPoint and MaxPoint. About half deal with formatting, including formatting of negative values. All of these are analogous to their similarly-named user interface properties:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_data_bar_dialog.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_data_bar_dialog.jpg" alt="Data bar dialog" width="799" height="578" class="alignnone size-full wp-image-2530" /></a></p>
<p><strong>Deleting Selected FormatConditions</strong></p>
<p>I wrote a routine to delete only certain types of format conditions from a target range. It gets called before the routine to copy the formats. For example, if the routine is copying DataBar formats, we&#8217;ll first delete all the existing ones.</p>
<p>It took longer than I expected to get a workable routine, chiefly because my initial attempts were inpossibly slow. Whereas copying all three types of FormatCondition to 1000 rows takes about a second, deleting formats by looping through the whole FormatConditions collection was taking minutes.</p>
<p>The solution was to break the target range into little ranges of 10 rows each and delete the matching conditions from those ranges. I assume this must simplify Excel&#8217;s internal indexing of the FormatConditions collection, whatever that means <img src='http://yoursumbuddy.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . This only takes a couple of seconds for a thousand rows.</p>
<p>The other thing to note is that you have to loop backwards through the FormatConditions &#8211; just like deleting any Excel object in a loop &#8211; or you&#8217;ll get a &#8220;Subscript out of Range&#8221; error:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> DeleteFormatConditions(rngTarget <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, _<br />
&nbsp; &nbsp; DeleteColorScales <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span>, DeleteDataBars <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span>, DeleteIconSets <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span>)<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> FormatConditionsCount <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> DeleteIncrement <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> DeleteRangeStart <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> DeleteRangeEnd <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> DeleteDone <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> rngDelete <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> objFormatCondition <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Object</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<br />
<span style="color: #008000;">'Break target range into smaller ranges, which makes deletion go many times faster!<br />
</span>DeleteIncrement = 10<br />
DeleteRangeStart = 1<br />
<span style="color: #008000;">'Min to keep from going past end of target range<br />
</span>DeleteRangeEnd = Application.WorksheetFunction.Min _<br />
&nbsp; &nbsp; (DeleteRangeStart + DeleteIncrement, rngTarget.Rows.Count)<br />
<span style="color: #8D38C9; font-weight: bold;">Do</span> <span style="color: #8D38C9; font-weight: bold;">While</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> DeleteDone<br />
&nbsp; &nbsp; <span style="color: #008000;">'rngTarget.Parent is the worksheet<br />
</span> &nbsp; &nbsp;<span style="color: #151B8D; font-weight: bold;">Set</span> rngDelete = rngTarget.Parent.Range(rngTarget.Cells(DeleteRangeStart, 1), _<br />
&nbsp; &nbsp; &nbsp; &nbsp; rngTarget.Cells(DeleteRangeEnd, rngTarget.Columns.Count))<br />
&nbsp; &nbsp; FormatConditionsCount = rngDelete.FormatConditions.Count<br />
&nbsp; &nbsp; <span style="color: #008000;">'Check each format condition's type and call matching routine<br />
</span> &nbsp; &nbsp;<span style="color: #008000;">'Step backwards or risk &quot;Subscript out of Range&quot;<br />
</span> &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">For</span> i = FormatConditionsCount <span style="color: #8D38C9; font-weight: bold;">To</span> 1 <span style="color: #8D38C9; font-weight: bold;">Step</span> -1<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Set</span> objFormatCondition = rngDelete.FormatConditions(i)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> DeleteColorScales <span style="color: #8D38C9; font-weight: bold;">And</span> objFormatCondition.<span style="color: #151B8D; font-weight: bold;">Type</span> = 3 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objFormatCondition.Delete<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> DeleteDataBars <span style="color: #8D38C9; font-weight: bold;">And</span> objFormatCondition.<span style="color: #151B8D; font-weight: bold;">Type</span> = 4 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objFormatCondition.Delete<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> DeleteIconSets <span style="color: #8D38C9; font-weight: bold;">And</span> objFormatCondition.<span style="color: #151B8D; font-weight: bold;">Type</span> = 6 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objFormatCondition.Delete<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">If</span> DeleteRangeEnd &gt;= rngTarget.Rows.Count <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; DeleteDone = <span style="color: #00C2FF; font-weight: bold;">True</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; DeleteRangeStart = Application.WorksheetFunction.Min _<br />
&nbsp; &nbsp; &nbsp; &nbsp; (DeleteRangeStart + DeleteIncrement, rngTarget.Rows.Count)<br />
&nbsp; &nbsp; DeleteRangeEnd = Application.WorksheetFunction.Min _<br />
&nbsp; &nbsp; &nbsp; &nbsp; (DeleteRangeEnd + DeleteIncrement, rngTarget.Rows.Count)<br />
<span style="color: #8D38C9; font-weight: bold;">Loop</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p><strong>The Formula Property</strong></p>
<p>As I mentioned above, each of these FormatConditions has a Formula property. I don&#8217;t see a match for this property anywhere in the Excel user interface. According to <a href="http://msdn.microsoft.com/en-us/library/office/ff197162.aspx" title="Data.Formula property" target="_blank">MSDN</a>, it:</p>
<blockquote><p>Returns or sets a String representing a formula, which determines the values to which the data bar will be applied.</p>
<p>This property is useful to limit the range of values that will display the conditional format. A typical scenario is when you have a range of numbers containing both positive and negative values. You may want to create more than one conditional format for this range of numbers—one for positive values and another for negative values.
</p></blockquote>
<p>Sure enough, if I apply it to a DataBar definition, like so&#8230;</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">dbDataBar.Formula = &quot;=A1 &gt; -5&quot;</div></div>
<p>&#8230; I get this formatting, where the DataBars are only applied to cells with values greater than negative 5:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_data_bar_formula_property.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0040_data_bar_formula_property.jpg" alt="Data bar formula property" width="456" height="65" class="alignnone size-full wp-image-2533" /></a></p>
<p>If there is a way to do this in the user interface, please let me know!</p>
<p><strong>Miscellaneous</strong>:</p>
<ul>
<li>In my previous post on this, I noted that a recorded macro sets the added conditional format to the first priority and that I was doing the same in my code. However, that&#8217;s a bad idea. When copying more than one format it will rearrange their order.
</li>
<li>If you are fooling around with this stuff, you&#8217;ll notice that recording a macro that modifies existing conditional formatting produces all the code for that formatting, not just for the modification. This can be annoying or useful, depending.
</li>
<li>The StopIfTrue FormatConition doesn&#8217;t apply to these three objects. You can see that they are grayed out in the Excel dialog.
</li>
<li>The ScopeType and PTCondition properties have to do with conditional formatting in pivot tables.</ul>
</li>
<p><strong>Download!</strong></p>
<p><a href="http://yoursumbuddy.com/downloads/Post_0040_more_per_row_CF.zip" target="_blank">Here&#8217;s a workbook</a> with a working model and all the code.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/vba-formatconditions-per-row-colorscales-databars-and-iconsets/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>many borders fast</title>
		<link>http://yoursumbuddy.com/many-borders-fast/</link>
		<comments>http://yoursumbuddy.com/many-borders-fast/#comments</comments>
		<pubDate>Thu, 07 Feb 2013 05:46:49 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[handy little things]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2453</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/many-borders-fast/">many borders fast</a></p><p>many borders fast If you want to draw borders fast in Excel, you can hold down the Ctrl key, select all the areas that need borders and apply them in one swell foop! I just realized this recently. It&#8217;s pretty &#8230; <a href="http://yoursumbuddy.com/many-borders-fast/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/many-borders-fast/">many borders fast</a></p><h1><a title="many borders fast" href="http://yoursumbuddy.com/many-borders-fast/" rel="bookmark">many borders fast</a></h1>
<p>If you want to draw borders fast in Excel, you can hold down the Ctrl key, select all the areas that need borders and apply them in one swell foop!</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_39_many_borders.gif"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_39_many_borders.gif" alt="many borders" width="329" height="416" class="alignnone size-full wp-image-2464" /></a></p>
<p>I just realized this recently. It&#8217;s pretty fast, unless you&#8217;re trying to do a screen capture with buggy software.</p>
<p>Speaking of which, I just found a new straight-to-animated-gif program, called Cropper, that replaced the free-for-one-month-and-then-pay-$300 software (had it for a month.) Cropper works fine so far, except for one major bug. It&#8217;s not the most intuitive program, but on the flip side it works with no intermediate steps and produces a pretty small file.</p>
<p>Small enough, that is, that it loads pretty fast on my laptop. I&#8217;d be interested to hear from anybody about whether it started fast, or not, for them.</p>
<p>If you do use it, you&#8217;ll want to know about the bug. You can start a screenshot in at least two ways, double-clicking or pressing Alt-PrtSc. Double-clicking leaves the transparent blue selection layer on the screen, so I like the hotkey approach. But the bug is that the first time after opening Cropper, you need to start your capture by double-clicking or you&#8217;ll see the disheartening .Net runtime error screen. </p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0039_cropper_error.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0039_cropper_error.jpg" alt="Cropper error" width="533" height="214" class="alignnone size-full wp-image-2471" /></a></p>
<p>After seeing it a few times the phrase &#8220;<a href="http://idioms.thefreedictionary.com/come+a+cropper" title=""come a cropper" definition" target="_blank">come a cropper</a>&#8221; leapt to mind. But no worries, after double-clicking once you can use the hotkey for subsequent captures.</p>
<p>If anybody has any suggestions for animated gif software, I&#8217;m all ears.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/many-borders-fast/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Conditional Formatting Per-Row Color Scales</title>
		<link>http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/</link>
		<comments>http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/#comments</comments>
		<pubDate>Sun, 03 Feb 2013 18:35:54 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Conditional Formatting]]></category>
		<category><![CDATA[Excel 2007/10]]></category>
		<category><![CDATA[Utilities]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2372</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/">Conditional Formatting Per-Row Color Scales</a></p><p>Conditional Formatting Per-Row Color Scales If you want to compare all the numbers in a range, you can apply a conditional formatting color scale to the entire area and it works just fine. However, sometimes I want to compare the &#8230; <a href="http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/">Conditional Formatting Per-Row Color Scales</a></p><h1><a title="Conditional Formatting Per-Row Color Scales" href="http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/" rel="bookmark">Conditional Formatting Per-Row Color Scales</a></h1>
<p>If you want to compare all the numbers in a range, you can apply a conditional formatting color scale to the entire area and it works just fine. However, sometimes I want to compare the data on a per-row basis. Here&#8217;s two examples of data that could use per-row color scales:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_same_color_scale_throughout_1.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_same_color_scale_throughout_1.jpg" alt="same color scale throughout 11" width="445" height="197" class="alignnone size-full wp-image-2374" /></a></p>
<p>The first, above, mixes values and percents. All the percents are red &#8211; the low end of the scale &#8211; because they equate to numbers between 0 and 1, and they&#8217;re getting compared to sales values between 1 and 100.</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_same_color_scale_throughout_2.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_same_color_scale_throughout_2.jpg" alt="same color scale throughout 2" width="603" height="167" class="alignnone size-full wp-image-2398" /></a></p>
<p>The second mixes sales from a large and small business. The sales for the small business are all shown as low &#8211; red &#8211; because they&#8217;re generally less than 1/100th of the sales of the large corporation.</p>
<p>In both cases I just want to compare the cells in each row to each other. Doing that, the second example looks like this, showing the relative sales within the company and year:</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_per_row_color_scale_1.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_per_row_color_scale_1.jpg" alt="per row color scale 1" width="599" height="169" class="alignnone size-full wp-image-2399" /></a></p>
<p><strong>VBA to Apply Per-Row Color Scales</strong></p>
<p>As far as I can tell, there&#8217;s no quick built-in way to apply color-scales (or icon sets or data bars) on a per-row basis. Instead you need to apply them one row at a time. So, of course, I wrote some VBA.</p>
<p>I&#8217;ve long been intimidated by Excel 2010&#8242;s conditional formatting object model, at least when it comes to non-formula conditions. But one day I answered this <a href="http://stackoverflow.com/questions/14381200/excel-2010-conditional-formatting-individual-rows/14381852#14381852" title="StackOverlow question about per-row color scales" target="_blank">StackOverflow post</a> about per-row color scales and decided to dig deeper.</p>
<p>For that answer I turned on the Macro Recorder, applied a color scale, and then called the generated code in a loop for each row. A better approach is to copy an existing row with the color scale you want and paste it over each row.</p>
<p>The simplest version of this is to copy all formatting in a source row and paste it to a target row. However, I&#8217;d prefer to grab only the color scale and paste it, ignoring other formats such as borders, text styles and other conditional formats.</p>
<p>If you turn on the Macro Recorder and apply a 3-gradient color scale to a range, you get something that looks like this, with the last five lines repeated two more times:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Selection.FormatConditions.AddColorScale ColorScaleType:=3<br />
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority<br />
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _<br />
&nbsp; &nbsp; &nbsp; &nbsp; xlConditionValueLowestValue<br />
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor<br />
&nbsp; &nbsp; .Color = 7039480<br />
&nbsp; &nbsp; .TintAndShade = 0<br />
End With</div></div>
<p>Note that it adds a ColorScale and then moves it to the top of the conditional formatting stack with SetFirstPriority.</p>
<p>I discovered a quirk when I tried to modify the above code to do some simple looping through the conditional formatting settings in a range. I tried something like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Dim fc As FormatCondition<br />
For Each fc In Selection.FormatConditions<br />
&nbsp; &nbsp; Debug.Print fc.Application.Name<br />
Next fc</div></div>
<p>That makes sense right? Set an object to a FormatCondition in order to loop through all the FormatConditions. However, if Selection only has color scale conditional formatting, you&#8217;ll get a &#8220;Type Mismatch&#8221; error on the &#8220;For Each fc&#8221; line. Turns out you need to declare fc as a ColorScale. Then the above code will run and will only cycle through the ColorScale objects. </p>
<p>So here&#8217;s the workhorse of my code. It&#8217;s called by the main module and loops through a target range, applying the color scale from the source range:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> SetRangeColorScale(rngTargetSection <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, csSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.ColorScale)<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> csTarget <span style="color: #151B8D; font-weight: bold;">As</span> ColorScale<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> csCriterion <span style="color: #151B8D; font-weight: bold;">As</span> ColorScaleCriterion<br />
<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> csTarget = rngTargetSection.FormatConditions.AddColorScale(csSource.<span style="color: #151B8D; font-weight: bold;">Type</span>)<br />
rngTargetSection.FormatConditions(rngTargetSection.FormatConditions.Count).SetFirstPriority<br />
<span style="color: #8D38C9; font-weight: bold;">For</span> <span style="color: #8D38C9; font-weight: bold;">Each</span> csCriterion <span style="color: #8D38C9; font-weight: bold;">In</span> csSource.ColorScaleCriteria<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">With</span> csTarget.ColorScaleCriteria(csCriterion.Index)<br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #151B8D; font-weight: bold;">Type</span> = csCriterion.<span style="color: #151B8D; font-weight: bold;">Type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .FormatColor.Color = csCriterion.FormatColor.Color<br />
&nbsp; &nbsp; &nbsp; &nbsp; .FormatColor.TintAndShade = csCriterion.FormatColor.TintAndShade<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
<span style="color: #8D38C9; font-weight: bold;">Next</span> csCriterion<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p>Below is my main routine. It allows you to choose Row or Column orientation, so you can paste the color scales by-column if you wish. The SectionIncrement variable specifies how many rows at a time to paste the color scale, so you could apply the comparison over two or more rows (or columns) at a time.</p>
<p>Note that in this module, objSourceCondition is declared as an object and tested to see if it&#8217;s a ColorScale <a href="http://msdn.microsoft.com/en-us/library/office/ff837832%28v=office.14%29.aspx" title="MS Office 2010 FormatCondition enumerations" target="_blank">type of FormatCondition</a>:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> CopyColorScaleInSections()<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> rngSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> rngTarget <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> ws <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Worksheet<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> objSourceCondition <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Object</span> <span style="color: #008000;">'we'll test for ColorScale<br />
</span><span style="color: #151B8D; font-weight: bold;">Dim</span> rngTargetSection <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> FillDirection <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> IncompatibleRangeError <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> SectionIncrement <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> SectionsCount <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<br />
<span style="color: #008000;">'change the settings below to suit<br />
</span><span style="color: #151B8D; font-weight: bold;">Set</span> ws = ActiveSheet<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> rngSource = ws.Range(<span style="color: #800000;">&quot;B2:E2&quot;</span>)<br />
<span style="color: #151B8D; font-weight: bold;">Set</span> rngTarget = ws.Range(<span style="color: #800000;">&quot;B3:E7&quot;</span>)<br />
FillDirection = <span style="color: #800000;">&quot;Rows&quot;</span><br />
SectionIncrement = 1<br />
<br />
<span style="color: #008000;">'deletes all existing formats<br />
</span><span style="color: #008000;">'you might want to change to just delete<br />
</span><span style="color: #008000;">'ColorScales, but for demo purposes<br />
</span><span style="color: #008000;">'this works well<br />
</span>rngTarget.FormatConditions.Delete<br />
<span style="color: #008000;">'checks whether the settings above work together<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> CompatibleRanges(rngSource, rngTarget, SectionIncrement, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; FillDirection, IncompatibleRangeError) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; MsgBox IncompatibleRangeError, vbOKOnly + vbExclamation<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">GoTo</span> exit_point<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<br />
<span style="color: #008000;">'determine how many sections of rows or columns<br />
</span><span style="color: #008000;">'we'll be pasting over<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> FillDirection = <span style="color: #800000;">&quot;Rows&quot;</span> <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; SectionsCount = rngTarget.Rows.Count / SectionIncrement<br />
<span style="color: #8D38C9; font-weight: bold;">ElseIf</span> FillDirection = <span style="color: #800000;">&quot;Columns&quot;</span> <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; SectionsCount = rngTarget.Columns.Count / SectionIncrement<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<br />
<span style="color: #8D38C9; font-weight: bold;">For</span> i = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> SectionsCount - 1<br />
&nbsp; &nbsp; <span style="color: #008000;">'set an individual section to be pasted over<br />
</span> &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> FillDirection = <span style="color: #800000;">&quot;Rows&quot;</span> <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Set</span> rngTargetSection = rngTarget((i * SectionIncrement) + 1, 1) _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Resize(SectionIncrement, rngTarget.Columns.Count)<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">ElseIf</span> FillDirection = <span style="color: #800000;">&quot;Columns&quot;</span> <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">Set</span> rngTargetSection = rngTarget(1, (i * SectionIncrement) + 1) _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Resize(rngTarget.Rows.Count, SectionIncrement)<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> <span style="color: #8D38C9; font-weight: bold;">Each</span> objSourceCondition <span style="color: #8D38C9; font-weight: bold;">In</span> rngSource.FormatConditions<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">'test if it's a ColorScale - 3<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #8D38C9; font-weight: bold;">If</span> objSourceCondition.<span style="color: #151B8D; font-weight: bold;">Type</span> = 3 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SetRangeColorScale rngTargetSection, objSourceCondition<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> objSourceCondition<br />
<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<br />
exit_point:<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p>Obviously, when you do this you end up with a passel of conditional formatting rules, so don&#8217;t be surprised!</p>
<p><a href="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_rule_for_each_row.jpg"><img src="http://yoursumbuddy.com/wp-content/uploads/2013/02/Post_0038_rule_for_each_row.jpg" alt="rule for each row" width="827" height="372" class="alignnone size-full wp-image-2409" /></a></p>
<p>Here&#8217;s the function, called from the main routine above, that checks whether the source and target ranges are compatible:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Function</span> CompatibleRanges(rngSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, rngTarget <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, _<br />
&nbsp; &nbsp; SectionIncrement <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, FillDirection <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>, _<br />
&nbsp; &nbsp; <span style="color: #151B8D; font-weight: bold;">ByRef</span> IncompatibleRangeError <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">String</span>) <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Boolean</span><br />
<br />
<span style="color: #008000;">'no #DIV/0<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> SectionIncrement = 0 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; IncompatibleRangeError = _<br />
&nbsp; &nbsp; <span style="color: #800000;">&quot;You can't use an increment of 0&quot;</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">GoTo</span> exit_point<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<span style="color: #008000;">'can't specify a SectionIncrement bigger than the target range<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> (FillDirection = <span style="color: #800000;">&quot;Rows&quot;</span> <span style="color: #8D38C9; font-weight: bold;">And</span> rngTarget.Rows.Count &lt; SectionIncrement) <span style="color: #8D38C9; font-weight: bold;">Or</span> _<br />
&nbsp; &nbsp;(FillDirection = <span style="color: #800000;">&quot;Columns&quot;</span> <span style="color: #8D38C9; font-weight: bold;">And</span> rngTarget.Columns.Count &lt; SectionIncrement) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; IncompatibleRangeError = _<br />
&nbsp; &nbsp; <span style="color: #800000;">&quot;Target range must have at least&quot;</span> &amp; vbCrLf &amp; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; SectionIncrement &amp; <span style="color: #800000;">&quot; rows.&quot;</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">GoTo</span> exit_point<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<span style="color: #008000;">'target range rows or columns must be<br />
</span><span style="color: #008000;">'evenly divisible by the SectionIncrement<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> (FillDirection = <span style="color: #800000;">&quot;Rows&quot;</span> <span style="color: #8D38C9; font-weight: bold;">And</span> rngTarget.Rows.Count <span style="color: #151B8D; font-weight: bold;">Mod</span> SectionIncrement &lt;&gt; 0) <span style="color: #8D38C9; font-weight: bold;">Or</span> _<br />
&nbsp; &nbsp;(FillDirection = <span style="color: #800000;">&quot;Columns&quot;</span> <span style="color: #8D38C9; font-weight: bold;">And</span> rngTarget.Columns.Count <span style="color: #151B8D; font-weight: bold;">Mod</span> SectionIncrement &lt;&gt; 0) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; IncompatibleRangeError = _<br />
&nbsp; &nbsp; <span style="color: #800000;">&quot;Target range &quot;</span> &amp; FillDirection &amp; <span style="color: #800000;">&quot; must be&quot;</span> &amp; vbCrLf &amp; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #800000;">&quot;evenly divisible by &quot;</span> &amp; SectionIncrement &amp; <span style="color: #800000;">&quot;.&quot;</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">GoTo</span> exit_point<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<span style="color: #008000;">'target range width or height has to match<br />
</span><span style="color: #008000;">'source range width or height<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> (rngSource.Rows.Count = rngTarget.Rows.Count <span style="color: #8D38C9; font-weight: bold;">Or</span> _<br />
&nbsp; &nbsp; &nbsp; &nbsp; rngSource.Columns.Count = rngTarget.Columns.Count) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; IncompatibleRangeError = _<br />
&nbsp; &nbsp; <span style="color: #800000;">&quot;Source and Target ranges must have&quot;</span> &amp; vbCrLf &amp; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #800000;">&quot;either the same number&quot;</span> &amp; vbCrLf &amp; <span style="color: #800000;">&quot;of rows or columns.&quot;</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">GoTo</span> exit_point<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<br />
exit_point:<br />
CompatibleRanges = IncompatibleRangeError = <span style="color: #800000;">&quot;&quot;</span><br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Function</span></div></div>
<p>I&#8217;ve run this code successfully on up to 10,000 rows. It took about 7 seconds. I did notice that deleting 9,900 of those rows afterwards takes a while, and that the workbook can then act sluggish until it&#8217;s saved. I&#8217;m not sure what the issue is.</p>
<p>No download right now, but I&#8217;m planning to whip up a workbook that expands this to IconSets and DataBars and whatever else I can cram in there. So look for that in a week or so.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/conditional-formatting-per-row-color-scales/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Conditional Formatting Color Scales Based on Other Cells</title>
		<link>http://yoursumbuddy.com/conditional-formatting-color-scales-based-on-other-cells/</link>
		<comments>http://yoursumbuddy.com/conditional-formatting-color-scales-based-on-other-cells/#comments</comments>
		<pubDate>Thu, 24 Jan 2013 05:02:45 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Camera Tool]]></category>
		<category><![CDATA[Conditional Formatting]]></category>
		<category><![CDATA[Excel 2007/10]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2311</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/conditional-formatting-color-scales-based-on-other-cells/">Conditional Formatting Color Scales Based on Other Cells</a></p><p>Conditional Formatting Color Scales Based on Other Cells With formula-based conditional formatting, it&#8217;s pretty easy to base the formats on other cells in the workbook, simply by referring to those cells in the formula. However, it&#8217;s more complicated if you &#8230; <a href="http://yoursumbuddy.com/conditional-formatting-color-scales-based-on-other-cells/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/conditional-formatting-color-scales-based-on-other-cells/">Conditional Formatting Color Scales Based on Other Cells</a></p><h1><a title="Conditional Formatting Color Scales Based on Other Cells" href="http://yoursumbuddy.com/conditional-formatting-color-scales-based-on-other-cells/" rel="bookmark">Conditional Formatting Color Scales Based on Other Cells</a></h1>
<p>With formula-based conditional formatting, it&#8217;s pretty easy to base the formats on other cells in the workbook, simply by referring to those cells in the formula. However, it&#8217;s more complicated if you want color scales derived from values in another range. In this post I discuss two ways to base color scales on another range. The first uses the camera tool while the second is a VBA subroutine that mimics conditional formatting.</p>
<p>Below is an example of what I&#8217;m talking about. The color formatting isn&#8217;t based on the values in the pivot table. Instead, it reflects the values in the second table, each cell of which contains the difference from the previous year in the pivot table. The colors range from red at the low end to green at the high end:</p>
<p><img src="http://yoursumbuddy.com/wp-content/uploads/2013/01/Post_0037_format_with_camera_tool_1.jpg" alt="ormat with camera tool 1" width="701" height="347" class="alignnone size-full wp-image-2358" /></p>
<p>So, here&#8217;s my two approaches to doing this:</p>
<p><strong>Using the Camera Tool</strong> </p>
<p>This method uses Excel&#8217;s under-publicized <a href="http://spreadsheetpage.com/index.php/oddity/the_camera_tool/" title="John Walkenbach's camera tool page" target="_blank">camera tool</a>, which creates a live picture linked to a group of cells. In this case the formatting is applied to a pivot table, but you can do it with any range. Here&#8217;s the steps:</p>
<ul>
<li>Create the range of formulas that you&#8217;ll base the conditional formatting on.</li>
<li>Format the numbers in that range to be invisible, by using a custom format of &#8220;;;;&#8221;. All you want to see is the conditional formatting.</li>
<li>Use the camera tool to take a picture of the entire pivot table and paste it over the range you just created, lining up the conditionally formatted cells. Set the picture to be completely transparent, using the &#8220;no fill&#8221; setting. This way you can see through the picture to the conditionally formatted cells underneath.</li>
</ul>
<p>The result will be like the illustration below. The source pivot table is in rows 11 to 18, and you can see that the picture starting in row 2 is linked to it. The cells underneath the picture contain the formulas referring to the pivot table. The conditional formatting is based on these cells, whose text is invisible because of the custom format.</p>
<p><img src="http://yoursumbuddy.com/wp-content/uploads/2013/01/Post_0037_format_with_camera_tool_2.jpg" alt="format with camera tool 2" width="706" height="416" class="alignnone size-full wp-image-2346" /></p>
<p>One thing to be aware of is that the picture doesn&#8217;t update until there&#8217;s a worksheet recalculation. You may have to force recalculation with F9 to have the picture update.</p>
<p>For one project I augmented this method by writing code that let me toggle back and forth between the values in the pivot table and the values the conditional formatting is based on.</p>
<p><strong>Using VBA to Create &#8220;FauxMatting&#8221;</strong></p>
<p>As the heading implies, this method attempts to replicate conditional formatting using VBA. The following subroutine takes two ranges &#8211; a source and a target range &#8211; as its arguments. It finds the highest and lowest values in the source range. It assigns each of those values a color in a scale from green to red, with white in the middle. This is done by dividing the range of values source values into 255 increments. The colors are then assigned to the target range:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #E56717; font-weight: bold;">Sub</span> ConditionalFauxmatting(rngSource <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range, rngtarget <span style="color: #151B8D; font-weight: bold;">As</span> Excel.Range)<br />
Const NUMBER_OF_INCREMENTS <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span> = 255<br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> MinValue <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> MaxValue <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> ScaleIncrement <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Double</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> ScalePosition <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> var <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Variant</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> CellColor() <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<span style="color: #151B8D; font-weight: bold;">Dim</span> i <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span>, j <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Long</span><br />
<br />
<span style="color: #8D38C9; font-weight: bold;">If</span> <span style="color: #8D38C9; font-weight: bold;">Not</span> (rngSource.Rows.Count = rngtarget.Rows.Count <span style="color: #8D38C9; font-weight: bold;">And</span> rngSource.Columns.Count = rngtarget.Columns.Count) <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; MsgBox <span style="color: #800000;">&quot;Source and Target ranges must be&quot;</span> &amp; vbCrLf &amp; <span style="color: #800000;">&quot;same shape and size&quot;</span><br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">GoTo</span> exit_point<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
MinValue = Application.WorksheetFunction.Min(rngSource.Value)<br />
MaxValue = Application.WorksheetFunction.Max(rngSource.Value)<br />
<span style="color: #008000;">'divide the range between Min and Max values into 255 increments<br />
</span>ScaleIncrement = (MaxValue - MinValue) / NUMBER_OF_INCREMENTS<br />
<span style="color: #008000;">'if all source cells have the same value or there's only one<br />
</span><span style="color: #8D38C9; font-weight: bold;">If</span> ScaleIncrement = 0 <span style="color: #8D38C9; font-weight: bold;">Or</span> rngSource.Cells.Count = 1 <span style="color: #8D38C9; font-weight: bold;">Then</span><br />
&nbsp; &nbsp; rngtarget.Cells.Interior.Color = RGB(255, 255, 255)<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">GoTo</span> exit_point<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span><br />
<span style="color: #008000;">'assign all the values to variant array<br />
</span>var = rngSource.Value<br />
<span style="color: #151B8D; font-weight: bold;">ReDim</span> CellColor(<span style="color: #151B8D; font-weight: bold;">UBound</span>(var, 1), <span style="color: #151B8D; font-weight: bold;">UBound</span>(var, 2))<br />
<span style="color: #8D38C9; font-weight: bold;">For</span> i = <span style="color: #151B8D; font-weight: bold;">LBound</span>(var, 1) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(var, 1)<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = <span style="color: #151B8D; font-weight: bold;">LBound</span>(var, 2) <span style="color: #8D38C9; font-weight: bold;">To</span> <span style="color: #151B8D; font-weight: bold;">UBound</span>(var, 2)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">'the scale position must be a value between 0 and 255<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;ScalePosition = (var(i, j) - MinValue) * (1 / ScaleIncrement)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008000;">'this formula goes from blue to red, hitting white - RGB(255,255,255) at the midpoint<br />
</span> &nbsp; &nbsp; &nbsp; &nbsp;CellColor(i, j) = RGB(Application.WorksheetFunction.Min(ScalePosition * 2, 255), _<br />
&nbsp; &nbsp; &nbsp; &nbsp; IIf(ScalePosition &lt; 127, 255, Abs(ScalePosition - 255) * 2), _<br />
&nbsp; &nbsp; &nbsp; &nbsp; IIf(ScalePosition &lt; 127, ScalePosition * 2, Abs(ScalePosition - 255) * 2))<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
<span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<span style="color: #008000;">'assign the colors stored in the array<br />
</span><span style="color: #008000;">'to the target range<br />
</span><span style="color: #8D38C9; font-weight: bold;">With</span> rngtarget<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> i = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> .Rows.Count<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">For</span> j = 1 <span style="color: #8D38C9; font-weight: bold;">To</span> .Columns.Count<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Cells(i, j).Interior.Color = CellColor(i, j)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> j<br />
&nbsp; &nbsp; <span style="color: #8D38C9; font-weight: bold;">Next</span> i<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">With</span><br />
<br />
exit_point:<br />
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></div></div>
<p>The result looks like this:</p>
<p><img src="http://yoursumbuddy.com/wp-content/uploads/2013/01/Post_0037_format_with_VBA.jpg" alt="format with VBA" width="708" height="411" class="alignnone size-full wp-image-2335" /></p>
<p>I&#8217;m not sure how practical this is, but it was fun to figure out! Obviously, you&#8217;d want to tie this to a worksheet or pivot table event to update the formatting when the values change.</p>
<p><a href="http://yoursumbuddy.com/downloads/post_0037_color_scales_other_cells.zip" target="_blank">Here&#8217;s a workbook</a> demonstrating these two methods.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/conditional-formatting-color-scales-based-on-other-cells/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unified Method of Pivot Table Formatting</title>
		<link>http://yoursumbuddy.com/unified-method-of-pivot-table-formatting/</link>
		<comments>http://yoursumbuddy.com/unified-method-of-pivot-table-formatting/#comments</comments>
		<pubDate>Sun, 13 Jan 2013 16:45:49 +0000</pubDate>
		<dc:creator>Doug Glancy</dc:creator>
				<category><![CDATA[Conditional Formatting]]></category>
		<category><![CDATA[Excel 2007/10]]></category>
		<category><![CDATA[Pivot Tables]]></category>

		<guid isPermaLink="false">http://yoursumbuddy.com/?p=2166</guid>
		<description><![CDATA[<p><p><a href="http://yoursumbuddy.com/unified-method-of-pivot-table-formatting/">Unified Method of Pivot Table Formatting</a></p><p>Unified Method of Pivot Table Formatting In preparation for my big annual reporting push I&#8217;ve developed a (partial) Unified Method of Pivot Table Formatting. My motivation was to define a system that allows me to copy pivot tables as values &#8230; <a href="http://yoursumbuddy.com/unified-method-of-pivot-table-formatting/">Continue reading <span class="meta-nav">&#8594;</span></a></p></p><p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></description>
				<content:encoded><![CDATA[<p><a href="http://yoursumbuddy.com/unified-method-of-pivot-table-formatting/">Unified Method of Pivot Table Formatting</a></p><h1><a title="Unified Method of Pivot Table Formatting" href="http://yoursumbuddy.com/unified-method-of-pivot-table-formatting/" rel="bookmark">Unified Method of Pivot Table Formatting</a></h1>
<p>In preparation for my big annual reporting push I&#8217;ve developed a (partial) Unified Method of Pivot Table Formatting. My motivation was to define a system that allows me to copy pivot tables as values while preserving the formatting, and that increases formatting flexibility. Simply stated the Unified Method is:</p>
<blockquote><p>&#8220;Use Conditional Formatting for Everything, and Apply Every Conditional Format to the Entire Pivot Table.&#8221;  <strong>*</strong></p></blockquote>
<p>That&#8217;s right. No built-in &#8220;PivotTable Styles,&#8221; no field-level conditional formats, and no more just selecting the whole table, drawing a border around it and hoping &#8220;Preserve cell formatting on update&#8221; works.</p>
<p><strong>*</strong> Excel 2007 and later. Applies only to formula-based conditions, e.g., not to color scales. Other restrictions apply.</p>
<p><img src="http://yoursumbuddy.com/wp-content/uploads/2013/01/Post_0036_flexibly_formatted_pivot_1.jpg" alt="Formatted Pivot Table" width="539" height="692" class="alignnone size-full wp-image-2236" /></p>
<p>Above is a section of a world population pivot table. It has the following conditional formats:</p>
<ul>
<li>The header row is colored orange with a black border around each cell.</li>
<li>There&#8217;s a black border to the left and right of the table.</li>
<li>Subtotal rows are bold with a black border above and below but no interior border, and are the same color as the header row.</li>
<li>Detail row cells are surrounded by a light gray border.</li>
<li>There are no borders between columns A and B, as Column A is only one pixel wide and flops over into Column B, as with &#8220;Oceania Total.&#8221;</li>
<li>Country rows have alternate banding by country.</li>
<li>State/Province rows&#8217; population number font is gray</li>
</ul>
<p>That&#8217;s quite a list, I think you&#8217;ll agree. In the past I might have used regular formatting,  pivot table styles and field-level conditional formatting. I&#8217;d like to avoid all of those approaches, for the following reasons:</p>
<p><strong>Regular Formatting:</strong> By &#8220;regular formatting&#8221; I mean something like selecting the whole pivot table and applying an outside border. This requires the pivot table setting &#8220;Preserve cell formatting on update&#8221; to be turned on. I&#8217;ll admit I&#8217;ve never mastered the quirks of this setting, so I&#8217;d like to just avoid it. </p>
<p>I&#8217;ve already found things, like header row word-wrapping, that may make me relent on this one.</p>
<p><strong>Built-in PivotTable Styles</strong>: I really don&#8217;t like the built-in pivot table styles, for a few reasons:</p>
<ul>
<li>The text and cell colors are ugly to the point of unusability. They almost always need modification.</li>
<li>Modifying them is a pain. The names for pivot table parts are weird. For example, what&#8217;s a &#8220;First Row Stripe,&#8221; and how does &#8220;Column Subheading 2&#8243; compare to &#8220;Subtotal Column 2?&#8221; So I do a lot of guessing and backing out of the dialog to see if I guessed right. Very clunky.</li>
<p><img src="http://yoursumbuddy.com/wp-content/uploads/2013/01/Post_0036_pivot_table_modify_style_dialog.jpg" alt="Pivot Table Modify Style Dialog" width="600" height="378" class="alignnone size-full wp-image-2242" /></p>
<li>Finally, if you copy the pivot as values these formats disappear, although John Walkenbach has a <a href="http://spreadsheetpage.com/index.php/tip/unlinking_a_pivot_table_from_its_source_data/" title="John Walkenbach's unlink a pivot table page" target="_blank">solution</a> for that.</li>
</ul>
<p><strong>Field-level Conditional Formatting</strong>: The field-level conditional formatting that became available in Excel 2007, and that I discuss in <a href="http://yoursumbuddy.com/re-apply-excel-pivot-table-conditional-formatting/" title="Re-Apply Pivot Table Conditional Formatting">Re-Apply Pivot Table Conditional Formatting</a>, is certainly better than the pivot table styles. But, again, the formats disappear when you copy the pivot table as values. And you can only use them to format the value fields of a pivot table, so for something like alternate row banding that includes row labels you need to apply the rule twice.</p>
<p><strong>THE &#8220;UNIFIED&#8221; APPROACH</strong><br />
So, instead of the approaches above, I apply every conditional format to the entire pivot table. I use the ModifyAppliesToRange method, as discussed in the post linked above, to re-apply conditional formatting to the entire pivot table when it&#8217;s refreshed. This keeps all the formatting intact when I copy the table as values. It also allows me to easily apply formatting to specific columns and rows. </p>
<p>Note that in the Extend Pivot Table Conditional Formatting Post I only dealt with the rows of the pivot table that had data. In the example file at the end of this post I&#8217;ve extended the code to include the entire pivot table.</p>
<p><strong>Two Examples</strong><br />
Since I&#8217;m applying the conditional formatting to the whole pivot table, the conditions sometimes need to specify row or column numbers. For example, I only want to gray the text for State/Province rows in columns D and E. That condition looks like this:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=<span style="color: #8D38C9; font-weight: bold;">AND</span>(COLUMN(A1)&gt;=4,$C1&lt;&gt;<span style="color: #800000;">&quot;&quot;</span>)</div></div>
<p>This simply says if column C is blank, gray the text from Column D to the right. I could also specify less than or equal to 5 (column E), but since  the conditional formatting is limited to the pivot table that&#8217;s not necessary.</p>
<p>Another part that was fiddly is not showing borders between columns A and B. To do this requires two formulas, one to negate column A&#8217;s right border, another to blank out column B&#8217;s left border. Here&#8217;s the formula and setup for column A:</p>
<p><img src="http://yoursumbuddy.com/wp-content/uploads/2013/01/Post_0036_no_border_format.jpg" alt="No Border Formatting" width="698" height="599" class="alignnone size-full wp-image-2239" /></p>
<p>The order of the rules is very important with this and other conditions. These &#8220;no-border&#8221; formats need to be before the formats with the borders.</p>
<p><strong>A Couple More Things</strong></p>
<p>I found this post harder to write than most. Although I think this is an interesting and helpful approach, I don&#8217;t know how clear I&#8217;ve been. If you have any questions, let me know.</p>
<p>It&#8217;s worth restating that the Unified Method of Pivot Table Formatting really only works for Excel 2007 onwards. Earlier versions limit you to three conditional formats in a given cell. Also, it only works for formula-based conditional formatting, i.e., not for color scales, icons, etc.</p>
<p>You can <a href="http://yoursumbuddy.com/downloads/post_0036_flexible_pivot_formatting.zip" target="_blank">download a workbook</a> with the pivot table shown above. It also includes the code to extend the conditional formatting to the whole table after it&#8217;s refreshed.</p>
<p><a href="http://yoursumbuddy.com">yoursumbuddy - Doug Glancy&#039;s Excel Site</a></p>]]></content:encoded>
			<wfw:commentRss>http://yoursumbuddy.com/unified-method-of-pivot-table-formatting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
