<?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>PTS Blog &#187; VBA</title>
	<atom:link href="http://peltiertech.com/WordPress/category/vba/feed/" rel="self" type="application/rss+xml" />
	<link>http://peltiertech.com/WordPress</link>
	<description>PTS Excel Charts and Tutorials Blog</description>
	<lastBuildDate>Fri, 20 Nov 2009 15:03:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pivot Table Conditional Formatting with VBA</title>
		<link>http://peltiertech.com/WordPress/pivot-table-conditional-formatting-with-vba/</link>
		<comments>http://peltiertech.com/WordPress/pivot-table-conditional-formatting-with-vba/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 05:00:38 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[conditional formatting]]></category>
		<category><![CDATA[Pivot Tables]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=2534</guid>
		<description><![CDATA[A reader encountered problems applying conditional formatting to a pivot table. I tried it myself, using the same kind of formulas I would have applied in a regular worksheet range, and had no problem. The reader responded that he was having problems in Excel 2007, and I was using 2003. Apparently in 2003 the conditional [...]]]></description>
			<content:encoded><![CDATA[<p>A reader encountered problems applying conditional formatting to a pivot table. I tried it myself, using the same kind of formulas I would have applied in a regular worksheet range, and had no problem. The reader responded that he was having problems in Excel 2007, and I was using 2003. Apparently in 2003 the conditional formatting is preserved when the table is refreshed, but in 2007, the CF in the data range is wiped out.</p>
<p>Well, this is just the type of thing Bill gates invented Visual Basic for. I&#8217;ll apply an approach related to that in my VBA Conditional Chart Formatting series: <a href="http://peltiertech.com/WordPress/vba-conditional-formatting-of-charts-by-series-name/"title="VBA Conditional Formatting of Charts by Series Name | PTS Blog" >VBA Conditional Formatting of Charts by Series Name</a>, <a href="http://peltiertech.com/WordPress/vba-conditional-formatting-of-charts-by-category-label/"title="VBA Conditional Formatting of Charts by Category Label | PTS Blog" >VBA Conditional Formatting of Charts by Category Label</a>, and <a href="http://peltiertech.com/WordPress/vba-conditional-formatting-of-charts-by-value/"title="VBA Conditional Formatting of Charts by Value | PTS Blog" >VBA Conditional Formatting of Charts by Value</a>, with a little help from <a href="http://peltiertech.com/WordPress/referencing-pivot-table-ranges-in-vba/"title="Referencing Pivot Table Ranges in VBA | PTS Blog" >Referencing Pivot Table Ranges in VBA</a>. I&#8217;ll use arbitrary data and an arbitrary condition for the example.</p>
<p><span id="more-2534"></span>Here is a data source for a simple pivot table. The entire range of values in column D consists of random integers between 1 and 9, and there are three categories by which they can be sorted.</p>
<p align="center"><img src="/images/2009-10/VBA_PT_CF_data.png" alt="Pivot Table Data Source" /></p>
<p>The first pivot table, PivotTable1, has the category fields all placed in the rows area. I manually typed the name &#8220;Pivot Table 1&#8243; in the cell above the pivot table.</p>
<p align="center"><img src="/images/2009-10/VBA_PT_CF_pivot1a.png" alt="Pivot Table 1 Unformatted" /></p>
<p>Tip: To name a pivot table, right click on the table, choose Table Options, and edit the name in the first line of the dialog.</p>
<p>The second pivot table, PivotTable2, has one category in the columns area, so the values are separated into two columns.</p>
<p align="center"><img src="/images/2009-10/VBA_PT_CF_pivot2a.png" alt="Pivot Table 2 Unformatted" /></p>
<p>I used the following VBA procedure to apply bold text and yellow fill formatting to rows in PivotTable1 if the value in the Total column is 7 or greater. We are testing the entire DataBodyRange, which is one column wide.</p>
<pre class="vbasmall">Sub FormatPT1()
  Dim c As Range
  With ActiveSheet.PivotTables("PivotTable1")

    <span style="color: #339966;">' reset default formatting
</span>    With .TableRange1
      .Font.Bold = False
      .Interior.ColorIndex = 0
    End With

    <span style="color: #339966;">' apply formatting to each row if condition is met</span>
    For Each c In .DataBodyRange.Cells
      If c.Value &gt;= 7 Then
        With .TableRange1.Rows(c.Row - .TableRange1.Row + 1)
          .Font.Bold = True
          .Interior.ColorIndex = 6
        End With
      End If
    Next

  End With
End Sub
 </pre>
<p>I used the next procedure to apply the same formatting to PivotTable2 rows if the value in column a under Category 3 is 7 or greater. The DataBodyRange is two columns wide, for pivot items &#8220;a&#8221; and &#8220;b&#8221;. To test against pivot item &#8220;a&#8221;, we test the pivot item&#8217;s DataRange.</p>
<pre class="vbasmall">Sub FormatPT2()
  Dim c As Range
  With ActiveSheet.PivotTables("Pivottable2")

    <span style="color: #339966;">' reset default formatting
</span>    With .TableRange1
      .Font.Bold = False
      .Interior.ColorIndex = 0
    End With

    <span style="color: #339966;">' apply formatting to each row if condition is met</span>
    For Each c In .PivotFields("Category 3").PivotItems("a").DataRange.Cells
      If c.Value &gt;= 7 Then
        With .TableRange1.Rows(c.Row - .TableRange1.Row + 1)
          .Font.Bold = True
          .Interior.ColorIndex = 6
        End With
      End If
    Next

  End With
End Sub
 </pre>
<p>The two procedures above belong in a regular code module.</p>
<p>Here is PivotTable1 with the conditional formatting applied.</p>
<p align="center"><img src="/images/2009-10/VBA_PT_CF_pivot1b.png" alt="Pivot Table 1 Formatted" /></p>
<p>Here is PivotTable2 with the same formatting applied.</p>
<p align="center"><img src="/images/2009-10/VBA_PT_CF_pivot2b.png" alt="Pivot Table 2 Formatted" /></p>
<p>Note that refreshing the pivot tables changes values but does not automatically reformat the tables. You have to manually rerun the VBA routines, or capture the PivotTableUpdate event:</p>
<pre class="vbasmall">Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
  Select Case Target.Name
    Case "PivotTable1"
      FormatPT1
    Case "PivotTable2"
      FormatPT2
  End Select
End Sub
 </pre>
<p>This procedure goes in the code module for the worksheet that contains the pivot tables. To access this module easily, right click on the sheet tab, and choose View Code. If you use this approach, you can put the first two procedures into the worksheet&#8217;s code module, instead of a regular module.</p>
<p>Peltier Technical Services, Inc., Copyright © 2009.<br />&nbsp;<br /><span style="font: 80% Verdana,Tahoma,Arial,sans-serif;">Licensed under a <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/" rel="nofollow" rel="license" >Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License</a>.<br />&nbsp;<br />
<a href="http://peltiertech.com/Utility/" rel="nofollow"  title="PTS Chart Utilities: Waterfall, Cluster-Stack Column, Box and Whisker, Marimekko"><img src="http://peltiertech.com/Utility/pix/ptschtbanner1.png" alt="PTS Chart Utilities: Waterfall, Cluster-Stack Column, Box and Whisker, Marimekko" border="0" /></a></p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;title=Pivot%20Table%20Conditional%20Formatting%20with%20VBA&amp;bodytext=A%20reader%20encountered%20problems%20applying%20conditional%20formatting%20to%20a%20pivot%20table.%20I%20tried%20it%20myself%2C%20using%20the%20same%20kind%20of%20formulas%20I%20would%20have%20applied%20in%20a%20regular%20worksheet%20range%2C%20and%20had%20no%20problem.%20The%20reader%20responded%20that%20he%20was%20having%20problems" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;title=Pivot%20Table%20Conditional%20Formatting%20with%20VBA&amp;notes=A%20reader%20encountered%20problems%20applying%20conditional%20formatting%20to%20a%20pivot%20table.%20I%20tried%20it%20myself%2C%20using%20the%20same%20kind%20of%20formulas%20I%20would%20have%20applied%20in%20a%20regular%20worksheet%20range%2C%20and%20had%20no%20problem.%20The%20reader%20responded%20that%20he%20was%20having%20problems" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;t=Pivot%20Table%20Conditional%20Formatting%20with%20VBA" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Pivot%20Table%20Conditional%20Formatting%20with%20VBA%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;title=Pivot%20Table%20Conditional%20Formatting%20with%20VBA" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;title=Pivot%20Table%20Conditional%20Formatting%20with%20VBA&amp;annotation=A%20reader%20encountered%20problems%20applying%20conditional%20formatting%20to%20a%20pivot%20table.%20I%20tried%20it%20myself%2C%20using%20the%20same%20kind%20of%20formulas%20I%20would%20have%20applied%20in%20a%20regular%20worksheet%20range%2C%20and%20had%20no%20problem.%20The%20reader%20responded%20that%20he%20was%20having%20problems" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;title=Pivot%20Table%20Conditional%20Formatting%20with%20VBA" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;t=Pivot%20Table%20Conditional%20Formatting%20with%20VBA" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Pivot%20Table%20Conditional%20Formatting%20with%20VBA&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;title=Pivot%20Table%20Conditional%20Formatting%20with%20VBA&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=A%20reader%20encountered%20problems%20applying%20conditional%20formatting%20to%20a%20pivot%20table.%20I%20tried%20it%20myself%2C%20using%20the%20same%20kind%20of%20formulas%20I%20would%20have%20applied%20in%20a%20regular%20worksheet%20range%2C%20and%20had%20no%20problem.%20The%20reader%20responded%20that%20he%20was%20having%20problems" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fpivot-table-conditional-formatting-with-vba%2F&amp;submitHeadline=Pivot%20Table%20Conditional%20Formatting%20with%20VBA&amp;submitSummary=A%20reader%20encountered%20problems%20applying%20conditional%20formatting%20to%20a%20pivot%20table.%20I%20tried%20it%20myself%2C%20using%20the%20same%20kind%20of%20formulas%20I%20would%20have%20applied%20in%20a%20regular%20worksheet%20range%2C%20and%20had%20no%20problem.%20The%20reader%20responded%20that%20he%20was%20having%20problems&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/pivot-table-conditional-formatting-with-vba/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Apply Chart Formatting to Other Charts</title>
		<link>http://peltiertech.com/WordPress/apply-chart-formatting-to-other-charts/</link>
		<comments>http://peltiertech.com/WordPress/apply-chart-formatting-to-other-charts/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 05:00:16 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[Formatting]]></category>
		<category><![CDATA[Paste Special]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=2369</guid>
		<description><![CDATA[Sometimes you have a whole workbook full of charts. You made some last week, and others yesterday, but the one you made today has just the right look to it. You&#8217;d like to apply the formatting to the rest of the charts, but the thought of reformatting all of those charts makes your brain ache.
The [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you have a whole workbook full of charts. You made some last week, and others yesterday, but the one you made today has just the right look to it. You&#8217;d like to apply the formatting to the rest of the charts, but the thought of reformatting all of those charts makes your brain ache.</p>
<p>The <strong>good news </strong>is that you don&#8217;t need to reformat every little element of every last chart. When you copy a chart, you can select another chart and go to Paste Special. The options in the Paste Special dialog are to paste the formulas (the data in the copied chart), the formats (from the copied chart), or both data and formatting.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-08/formatpastespecial.png" alt="paste special dialog for charts" /></p>
<p>Not only can you paste formats from one chart to another, you can write a little VBA to automate the process.</p>
<p><span id="more-2369"></span><strong>John Mansfield </strong>did just that in <a href="http://www.cellmatrix.net/index.php/excel/comments/vba_to_copy_embedded_chart_formatting/" rel="nofollow" title="VBA to Copy Embedded Chart Formatting" >VBA to Copy Embedded Chart Formatting</a> in his <strong>cellMatrix.net </strong>blog. I punched up John&#8217;s original code to get this procedure:</p>
<pre class="vbasmall">Sub Copy_Chart_Formats()

    Dim Sht As Worksheet
    Dim Cht As ChartObject

    Application.ScreenUpdating = False

    ActiveChart.ChartArea.Copy

    For Each Sht In ActiveWorkbook.Worksheets
        For Each Cht In Sht.ChartObjects
            Cht.Chart.Paste Type:=xlFormats
        Next Cht
    Next Sht

    Application.ScreenUpdating = True

End Sub
 </pre>
<p>Let&#8217;s see how it works. Here are four charts, with different data and with different original chart types and formatting. <br class="spacer_" /></p>
<p align="center"><img src="http://peltiertech.com/images/2009-08/formatoriginal.png" alt="original four charts" /></p>
<p>With the top left chart selected, running the program changes none of the data but all of the formatting of the charts (including the top left chart).</p>
<p align="center"><img src="http://peltiertech.com/images/2009-08/formatcopy.png" alt="formats copied onto the charts" /></p>
<p>Closer inspection reveals the <strong>bad news</strong>, which is that not only are the formats changed to match the master chart, but so are the chart and axis titles. This would be <em>horrible</em>, to get the formats you wanted in all 200 charts in your workbook, but to also have 200 sets of titles to change back.</p>
<p>Fortunately we&#8217;re all expert programmers, with a thorough knowledge of Excel&#8217;s object model, so we recognize before even trashing all of our charts what steps we need to take.</p>
<p>First we&#8217;ll do a little check before reformatting a chart, to save us the fraction of a second it takes to redundantly reformat the original chart. Then we&#8217;ll extract the chart and axis titles, apply the master format to the chart (which hoses the titles), and finally reapply the titles.</p>
<pre class="vbasmall">Sub Copy_Chart_Formats_Not_Titles()

  Dim Sht As Worksheet
  Dim Cht As ChartObject
  Dim chtMaster As Chart
  Dim bTitle As Boolean
  Dim bXTitle As Boolean
  Dim bYTitle As Boolean
  Dim sTitle As String
  Dim sXTitle As String
  Dim sYTitle As String

  Application.ScreenUpdating = False

  Set chtMaster = ActiveChart

  For Each Sht In ActiveWorkbook.Worksheets
    For Each Cht In Sht.ChartObjects
      If Sht.Name = chtMaster.Parent.Parent.Name And _
          Cht.Name = chtMaster.Parent.Name Then
        <span style="color: #339966;">' don't waste time on chtMaster</span>
      Else
        With Cht.Chart
          <span style="color: #339966;">' get titles</span>
          bTitle = .HasTitle
          If bTitle Then
            <span style="color: #339966;">' chart title exists</span>
            sTitle = .ChartTitle.Characters.Text
          End If
          If .HasAxis(xlCategory) Then
            bXTitle = .Axes(xlCategory).HasTitle
            If bXTitle Then
              <span style="color: #339966;">' axis title exists</span>
              sXTitle = .Axes(xlCategory).AxisTitle.Characters.Text
            End If
          End If
          If .HasAxis(xlValue) Then
            bYTitle = .Axes(xlValue).HasTitle
            If bYTitle Then
              <span style="color: #339966;">' axis title exists</span>
              sYTitle = .Axes(xlValue).AxisTitle.Characters.Text
            End If
          End If

          <span style="color: #339966;">' apply formats
</span>          chtMaster.ChartArea.Copy
          .Paste Type:=xlFormats

          <span style="color: #339966;">' restore titles</span>
          If bTitle Then
            .HasTitle = True
            .ChartTitle.Characters.Text = sTitle
          End If
          If bXTitle Then
            .Axes(xlCategory).HasTitle = True
            .Axes(xlCategory).AxisTitle.Characters.Text = sXTitle
          End If
          If bYTitle Then
            .Axes(xlValue).HasTitle = True
            .Axes(xlValue).AxisTitle.Characters.Text = sYTitle
          End If
        End With
      End If
    Next Cht
  Next Sht

  Application.ScreenUpdating = True

End Sub
</pre>
<p>As before, the master chart is selected, then its formatting is applied to all of the other charts in the workbook.<br class="spacer_" /></p>
<p align="center"><img src="http://peltiertech.com/images/2009-08/formatcopywithouttitles.png" alt="formats copied onto the charts and titles restored" /></p>
<p>Nice, we&#8217;ve changed all of the formats while retaining the axis and chart titles. Instead of three minutes to reformat each chart (longer in 2007 because the F4 shortcut key for Repeat Last Action is no longer an effective timesaver), the whole process took seconds. Maybe a minute to copy and paste this code.<br class="spacer_" /></p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;title=Apply%20Chart%20Formatting%20to%20Other%20Charts&amp;bodytext=Sometimes%20you%20have%20a%20whole%20workbook%20full%20of%20charts.%20You%20made%20some%20last%20week%2C%20and%20others%20yesterday%2C%20but%20the%20one%20you%20made%20today%20has%20just%20the%20right%20look%20to%20it.%20You%27d%20like%20to%20apply%20the%20formatting%20to%20the%20rest%20of%20the%20charts%2C%20but%20the%20thought%20of%20reformatting" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;title=Apply%20Chart%20Formatting%20to%20Other%20Charts&amp;notes=Sometimes%20you%20have%20a%20whole%20workbook%20full%20of%20charts.%20You%20made%20some%20last%20week%2C%20and%20others%20yesterday%2C%20but%20the%20one%20you%20made%20today%20has%20just%20the%20right%20look%20to%20it.%20You%27d%20like%20to%20apply%20the%20formatting%20to%20the%20rest%20of%20the%20charts%2C%20but%20the%20thought%20of%20reformatting" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;t=Apply%20Chart%20Formatting%20to%20Other%20Charts" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Apply%20Chart%20Formatting%20to%20Other%20Charts%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;title=Apply%20Chart%20Formatting%20to%20Other%20Charts" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;title=Apply%20Chart%20Formatting%20to%20Other%20Charts&amp;annotation=Sometimes%20you%20have%20a%20whole%20workbook%20full%20of%20charts.%20You%20made%20some%20last%20week%2C%20and%20others%20yesterday%2C%20but%20the%20one%20you%20made%20today%20has%20just%20the%20right%20look%20to%20it.%20You%27d%20like%20to%20apply%20the%20formatting%20to%20the%20rest%20of%20the%20charts%2C%20but%20the%20thought%20of%20reformatting" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;title=Apply%20Chart%20Formatting%20to%20Other%20Charts" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;t=Apply%20Chart%20Formatting%20to%20Other%20Charts" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Apply%20Chart%20Formatting%20to%20Other%20Charts&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;title=Apply%20Chart%20Formatting%20to%20Other%20Charts&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=Sometimes%20you%20have%20a%20whole%20workbook%20full%20of%20charts.%20You%20made%20some%20last%20week%2C%20and%20others%20yesterday%2C%20but%20the%20one%20you%20made%20today%20has%20just%20the%20right%20look%20to%20it.%20You%27d%20like%20to%20apply%20the%20formatting%20to%20the%20rest%20of%20the%20charts%2C%20but%20the%20thought%20of%20reformatting" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fapply-chart-formatting-to-other-charts%2F&amp;submitHeadline=Apply%20Chart%20Formatting%20to%20Other%20Charts&amp;submitSummary=Sometimes%20you%20have%20a%20whole%20workbook%20full%20of%20charts.%20You%20made%20some%20last%20week%2C%20and%20others%20yesterday%2C%20but%20the%20one%20you%20made%20today%20has%20just%20the%20right%20look%20to%20it.%20You%27d%20like%20to%20apply%20the%20formatting%20to%20the%20rest%20of%20the%20charts%2C%20but%20the%20thought%20of%20reformatting&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/apply-chart-formatting-to-other-charts/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Unspecified but Painfully Frustrating Error</title>
		<link>http://peltiertech.com/WordPress/unspecified-painfully-frustrating-error/</link>
		<comments>http://peltiertech.com/WordPress/unspecified-painfully-frustrating-error/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 07:00:04 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=2137</guid>
		<description><![CDATA[In recent months I have encountered a particularly troublesome error. It occurs in Excel 2003 and 2007 (and perhaps in earlier versions, but I don&#8217;t recall any such cases). The error is heralded by an unusually unhelpful error message, even my Microsoft&#8217;s standards: Microsoft Visual Basic &#8211; System Error &#38;H80004005 (-2147467259). Unspecified error.

Pardon my French, [...]]]></description>
			<content:encoded><![CDATA[<p>In recent months I have encountered a particularly troublesome error. It occurs in Excel 2003 and 2007 (and perhaps in earlier versions, but I don&#8217;t recall any such cases). The error is heralded by an unusually unhelpful error message, even my Microsoft&#8217;s standards: <em><strong>Microsoft Visual Basic &#8211; System Error &amp;H80004005 (-2147467259). Unspecified error.</strong></em></p>
<p align="center"><img title="Unspecified Error" src="http://peltiertech.com/images/2009-06/unspecifiederror.png" alt="System Error &amp;H80004005 (-2147467259). Unspecified error" /></p>
<p><span id="more-2137"></span>Pardon my French, but WTF?</p>
<p>To rub salt into the wound, this is followed up with an <em><strong>Out of Memory</strong></em> error:</p>
<p align="center"><img title="Out of Memory" src="http://peltiertech.com/images/2009-06/outofmemory.png" alt="Out of memory" /></p>
<p>Sometimes these occur once, sometimes twice, when Excel is started, when a workbook with code is opened, or when an add-in is opened.</p>
<p><strong>Association with RefEdit Controls</strong></p>
<p>Closer examination sheds some light on the problem. It seems to be related to our old friend, the RefEdit control. If a workbook or add-in has a UserForm with a RefEdit, this error may occur. When the error occurs, it may be impossible to view the UserForm in the VB Editor, or if the UserForm can be viewed, any RefEdits have been stripped off the form. UserForms without RefEdit controls can be viewed with no problems.</p>
<p>On a computer that experiences the problem, the RefEdit library (refedit.dll) is not available in the references under the VB Editor&#8217;s Tools menu,and sometimes it turns up as a MISSING reference in this list. The RefEdit library is also not found in the list of Additional Controls which can be added to the VBE&#8217;s Controls Toolbox. If the RefEdit control had been present on the Controls Toolbox, its place may be taken by a gray square with a tooltip of &#8220;Unknown&#8221;.</p>
<p>Interestingly enough, the RefEdit library is present on the machine, in the Microsoft Office program directory. But the library is apparently not accessible.</p>
<p>There have been some recent changes in the RefEdit control, I believe on more than one occasion, but I have never documented the changes. There are no outwardly visible differences in the control, but the file size has changed, the file created or modified date has changed, and the file version has changed.</p>
<p><strong>History</strong></p>
<p>This famous Unspecified Error, its hex code 80004005 burned into many a programmer&#8217;s retinas, has been around for a long time, in Visual Basic 6 and probably earlier. It has been blamed on many factors, many of which seem like wild guesses, and most of which are irrelevant to our situation here. There are a lot of instances in which this error occurred in projects which dealt with retrieval of data from data controls or databases (Access, Oracle, SQL Server, and others). This error has also been associated with ODBC, ADO, IMAP, ASP and IIS, and DLLs from Microsoft (INET and HTML controls) and third parties. I suspect that this is a catch-all error message when there is an internal error in VB, when there is no specific information but the error is somehow associated with various controls or libraries.</p>
<p>At least one Microsoft Knowledge Base article discusses this error in the context of Excel. <a href="http://support.microsoft.com/kb/230888/" rel="nofollow" title="OFF2000: " >OFF2000: &#8220;System Error &amp;H80004005 (-2147467259)&#8221; Running Macro with UserForm</a> attributes the error to opening a file with a userform that contains a Microsoft DirectAnimation Sprite control (daxctle.ocx), whatever that is. Apparently that was the Office 2000 version of the problem, and in Office 2003/2007 it&#8217;s kicked off by a RefEdit control.</p>
<p>Google finds innumerable links to this error in web sites and newsgroups, related to Visual Basic, Office, and recently, Excel. I have spent dozens of hours poring through search results looking unsuccessfully for relief.</p>
<p>What makes this problem so much harder to document and troubleshoot is that it does not occur on all computers which have had nominally the same upgrades. One user will be hobbled by this error, while a user in the adjoining cubical will show no signs of it. I have had substantially identical VMs on the same computer, some with the problem, some immune.</p>
<p><strong>Remedies</strong></p>
<p>This problem has surfaced among users of my charting utilities, but usually I&#8217;ve been able to fix it. Also, one of my clients has a large Excel add-in package which has experienced this error. So far I think we&#8217;ve successfully treated all infections. (And no, I&#8217;m not the Excel Doctor.)</p>
<p>I have tried a wide range of remedies. A few involve chicken&#8217;s blood at full moon, but most involve various Windows and Office activities. Some remedies work some of the time, but what works on one computer may not work on another.</p>
<p>Because this problem was sporadic when I first encountered it, I didn&#8217;t document the exact steps taken to diagnose and remedy the issue. I&#8217;ve generally followed a brute force approach until recently.</p>
<p>In mild cases, deleting all files in the user&#8217;s temp directory and in the C:\Documents and Settings\<em>User Name</em>\Application Data\Microsoft\Forms directory will clear up the problem. This is all that my main machine needed, but generally it&#8217;s like trying to boil the ocean.</p>
<p>Reregistering the refedit.dll does not seem to help. I have tried replacing an older version of this library with a newer version from another computer, but I don&#8217;t recall this ever having helped.</p>
<p>Lately it seems that almost all cases in Excel 2003 are fixed using the Help menu&#8217;s <strong>Detect and Repair</strong> command, so this is my first step in treating the malady. Excel 2007 has a similar command deep under the Office Orb (Excel Options &gt; Resources &gt; Run <strong>Microsoft Office Diagnostics</strong> &gt; Diagnose). I&#8217;ve used this command, but it has never given me the same warm fuzzy feeling of a Detect and Repair operation. However, just today an end user ran the diagnostics, and the problem was cleared up.</p>
<p>There are a couple preventative measures which seem to make add-ins resistant to the unspecified error. Open the add-in in Excel 2000, uncheck the reference to the RefEdit library, compile the code (ignoring compile errors related to new Excel 2002-2003-2007 syntax), and save and close the add-in. The compile step may not be necessary. I&#8217;ve also removed RefEdit controls and replaced them in 2000, but this seems to be unnecessary (as well as tedious).</p>
<p>I&#8217;ve seen two or three cases in which these last few steps did not seem to work. But in at least one of these cases, I&#8217;m not sure the end user followed instructions and actually ran the Detect and Repair or Diagnostic command.</p>
<p><strong><em>Update 23 June 2009</em></strong></p>
<p><em>Bob Flanagan has shared a link to his detailed protocols for fixing this and other problems with Office installations at <a href="http://www.add-ins.com/how_to_repair_office.htm" rel="nofollow" title="What to do if Excel add-ins do not run" >What to do if Excel add-ins do not run</a>. Thanks, Bob.</em></p>
<p><strong>Other Experiences</strong></p>
<p>Have you encountered this problem? Have you documented it? Have you successfully treated it?</p>
<p>If so, I want to hear about it. Please leave a comment with as many details as you can muster.</p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;title=Unspecified%20but%20Painfully%20Frustrating%20Error&amp;bodytext=In%20recent%20months%20I%20have%20encountered%20a%20particularly%20troublesome%20error.%20It%20occurs%20in%20Excel%202003%20and%202007%20%28and%20perhaps%20in%20earlier%20versions%2C%20but%20I%20don%27t%20recall%20any%20such%20cases%29.%20The%20error%20is%20heralded%20by%20an%20unusually%20unhelpful%20error%20message%2C%20even%20my%20Micros" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;title=Unspecified%20but%20Painfully%20Frustrating%20Error&amp;notes=In%20recent%20months%20I%20have%20encountered%20a%20particularly%20troublesome%20error.%20It%20occurs%20in%20Excel%202003%20and%202007%20%28and%20perhaps%20in%20earlier%20versions%2C%20but%20I%20don%27t%20recall%20any%20such%20cases%29.%20The%20error%20is%20heralded%20by%20an%20unusually%20unhelpful%20error%20message%2C%20even%20my%20Micros" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;t=Unspecified%20but%20Painfully%20Frustrating%20Error" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Unspecified%20but%20Painfully%20Frustrating%20Error%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;title=Unspecified%20but%20Painfully%20Frustrating%20Error" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;title=Unspecified%20but%20Painfully%20Frustrating%20Error&amp;annotation=In%20recent%20months%20I%20have%20encountered%20a%20particularly%20troublesome%20error.%20It%20occurs%20in%20Excel%202003%20and%202007%20%28and%20perhaps%20in%20earlier%20versions%2C%20but%20I%20don%27t%20recall%20any%20such%20cases%29.%20The%20error%20is%20heralded%20by%20an%20unusually%20unhelpful%20error%20message%2C%20even%20my%20Micros" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;title=Unspecified%20but%20Painfully%20Frustrating%20Error" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;t=Unspecified%20but%20Painfully%20Frustrating%20Error" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Unspecified%20but%20Painfully%20Frustrating%20Error&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;title=Unspecified%20but%20Painfully%20Frustrating%20Error&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=In%20recent%20months%20I%20have%20encountered%20a%20particularly%20troublesome%20error.%20It%20occurs%20in%20Excel%202003%20and%202007%20%28and%20perhaps%20in%20earlier%20versions%2C%20but%20I%20don%27t%20recall%20any%20such%20cases%29.%20The%20error%20is%20heralded%20by%20an%20unusually%20unhelpful%20error%20message%2C%20even%20my%20Micros" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Funspecified-painfully-frustrating-error%2F&amp;submitHeadline=Unspecified%20but%20Painfully%20Frustrating%20Error&amp;submitSummary=In%20recent%20months%20I%20have%20encountered%20a%20particularly%20troublesome%20error.%20It%20occurs%20in%20Excel%202003%20and%202007%20%28and%20perhaps%20in%20earlier%20versions%2C%20but%20I%20don%27t%20recall%20any%20such%20cases%29.%20The%20error%20is%20heralded%20by%20an%20unusually%20unhelpful%20error%20message%2C%20even%20my%20Micros&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/unspecified-painfully-frustrating-error/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Stack Columns In Order Of Size With VBA</title>
		<link>http://peltiertech.com/WordPress/stack-columns-in-order-of-size-with-vba/</link>
		<comments>http://peltiertech.com/WordPress/stack-columns-in-order-of-size-with-vba/#comments</comments>
		<pubDate>Tue, 19 May 2009 09:30:57 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[Formatting]]></category>
		<category><![CDATA[Stacked Column Charts]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=1948</guid>
		<description><![CDATA[In Stack Columns In Order Of Size I showed a worksheet formula approach to sort columns in each stack in a stacked chart by size, not by series. When you create a stacked column chart the usual way, the columns are stacked in the same series order, as shown below.
 - &#8211; - 
This technique [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://peltiertech.com/WordPress/stack-columns-in-order-of-size/"title="Stack Columns In Order Of Size" >Stack Columns In Order Of Size</a> I showed a worksheet formula approach to sort columns in each stack in a stacked chart by size, not by series. When you create a stacked column chart the usual way, the columns are stacked in the same series order, as shown below.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/StackData1.png" alt="Stacked Column Chart Data" /> <span style="color: white;">- &#8211; -</span> <img src="http://peltiertech.com/images/2009-05/StackChart1a.png" alt="Stacked Column Chart, Unsorted" /></p>
<p>This technique required seven columns of intricate calculations to portray the three series, the columns of formulas increase faster than the number of visible series in the chart, and the formulas become longer as series are added. Too much tedium, too many chances to mess it up.</p>
<p><strong><span id="more-1948"></span>Programmatic Approach</strong></p>
<p>So I build a simple VBA routine. A1:D14 is the input data (the data need not start in A1). The first row contains series names and the cells are filled with the color to format the data points in the chart. The first column holds the category labels, and the data to be sorted and plotted is in B2:D14.</p>
<p>F1:L14 is the output data. The category labels and series names are copied over from the input range. (The bottom series in the chart is labeled with the first series name, regardless of which column of the original data provides the points for this series.) Each row of data values has been sorted left to right in decreasing order in G2:I14, and the series name corresponding to a particular data value is placed in the same cell within the range J1:L14.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/StackData3.png" alt="Stacked Column Chart Input Data and Program Output" /></p>
<p>Here is the chart created by the program.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/StackChart4.png" alt="Stacked Column Chart, Sorted" /></p>
<p>This procedure works just as well if there are four or more initially columns of data, unlike the manual worksheet formula version, which is already getting bogged down at three columns.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/StackData4.png" alt="Stacked Column Chart Input Data and Program Output" /></p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/StackChart5.png" alt="Stacked Column Chart, Sorted" /></p>
<p>Frankly, I&#8217;m not sure the charts with sorted columns are easier to read. In fact, when they are sorted it&#8217;s easier to keep track of which value goes with which series. With the blocks in different order for each stack, you have to keep referring to the legend. This negates any possible benefit of the blocks being in numerical order.</p>
<p>The VBA code is shown below. The procedure <tt>ProcessInputRange</tt> does all the work in the range passed to it. The procedure <tt>ProcessInputRangeSelection</tt> is run from Excel: it finds the selected range and passes it to <tt>ProcessInputRange</tt>. I separate my code into pieces like this to make it easier to reuse the pieces. For example, I could call <tt>ProcessInputRange</tt> from a different routine, perhaps cycling through all the sheets in a workbook and processing all of the data.</p>
<pre class="vbasmall">Sub ProcessInputRangeSelection()
  If TypeName(Selection) = "Range" Then
    ProcessInputRange Selection
  End If
  ActiveSheet.Cells(ActiveWindow.ScrollRow, ActiveWindow.ScrollColumn).Select
End Sub

Sub ProcessInputRange(rInput As Range)
  ' range- and array-related
  Dim vaInput As Variant
  Dim vaYValues As Variant
  Dim vaXValues As Variant
  Dim vaNames As Variant
  Dim vaLabels As Variant
  Dim vYValueTemp As Variant
  Dim vLabelTemp As Variant
  Dim nXValues As Long
  Dim nNames As Long
  Dim iRow As Long
  Dim iCol As Long
  Dim iCol1 As Long
  Dim iCol2 As Long
  Dim rOutput As Range
  ' chart-related
  Dim lLeft As Double
  Dim lTop As Double
  Dim lWidth As Double
  Dim lHeight As Double
  Dim cht As Chart
  Dim iSeries As Long
  Dim iPoint As Long
  Dim vaColor As Variant

  vaInput = rInput.Value2

  nXValues = UBound(vaInput, 1) - 1
  nNames = UBound(vaInput, 2) - 1

  ReDim vaNames(1 To 1, 1 To nNames)
  ReDim vaXValues(1 To nXValues, 1 To 1)
  ReDim vaYValues(1 To nXValues, 1 To nNames)
  ReDim vaLabels(1 To nXValues, 1 To nNames)

  ' populate arrays
  For iRow = 1 To nXValues
    vaXValues(iRow, 1) = vaInput(iRow + 1, 1)
  Next
  For iCol = 1 To nNames
    vaNames(1, iCol) = vaInput(1, iCol + 1)
  Next
  For iRow = 1 To nXValues
    For iCol = 1 To nNames
      vaYValues(iRow, iCol) = vaInput(iRow + 1, iCol + 1)
      vaLabels(iRow, iCol) = vaNames(1, iCol)
    Next
  Next

  ' bubble sort values in each row, sort labels at same time
  For iRow = 1 To nXValues
    For iCol1 = 1 To nNames - 1
      For iCol2 = iCol1 + 1 To nNames
        If vaYValues(iRow, iCol2) &gt; vaYValues(iRow, iCol1) Then
          vYValueTemp = vaYValues(iRow, iCol1)
          vaYValues(iRow, iCol1) = vaYValues(iRow, iCol2)
          vaYValues(iRow, iCol2) = vYValueTemp
          vLabelTemp = vaLabels(iRow, iCol1)
          vaLabels(iRow, iCol1) = vaLabels(iRow, iCol2)
          vaLabels(iRow, iCol2) = vLabelTemp
        End If
      Next
    Next
  Next

  ' put adjusted arrays into worksheet
  Set rOutput = rInput.Resize(1, 1).Offset(, nNames + 2)
  rOutput.Offset(, 1).Resize(, nNames).Value = vaNames
  rOutput.Offset(1).Resize(nXValues).Value = vaXValues
  rOutput.Offset(1, 1).Resize(nXValues, nNames).Value = vaYValues
  rOutput.Offset(1, 1 + nNames).Resize(nXValues, nNames).Value = vaLabels

  ' get colors
  With rInput.Offset(, 1).Resize(1, nNames)
    ReDim vaColor(1 To 1, 1 To nNames)
    For iCol = 1 To nNames
      vaColor(1, iCol) = .Cells(1, iCol).Interior.ColorIndex
    Next
  End With

  ' process chart
  lWidth = ActiveWindow.UsableWidth / 2
  lHeight = ActiveWindow.UsableHeight / 2
  lLeft = ActiveSheet.Columns(ActiveWindow.ScrollColumn).Left + lWidth / 2
  lTop = ActiveSheet.Rows(ActiveWindow.ScrollRow).Top + lHeight / 2

  Set cht = ActiveSheet.ChartObjects.Add(lLeft, lTop, lWidth, lHeight).Chart

  With cht
    .SetSourceData Source:=rOutput.Resize(nXValues + 1, nNames + 1), PlotBy:=xlColumns
    .ChartType = xlColumnStacked

    .ChartGroups(1).GapWidth = 100
    With .PlotArea
      .Border.LineStyle = xlNone
      .Interior.ColorIndex = xlNone
    End With
    With .Axes(xlValue)
      .Border.LineStyle = xlNone
      With .MajorGridlines.Border
        .ColorIndex = 48
        .Weight = xlThin
      End With
    End With
    .Legend.Border.LineStyle = xlNone
    With .ChartArea
      .Border.LineStyle = xlNone
      .AutoScaleFont = False
      .Font.Size = 9
    End With

    ' format points
    For iSeries = 1 To nNames
      With .SeriesCollection(iSeries)
        .Border.LineStyle = xlNone
        .Interior.ColorIndex = vaColor(1, iSeries)
        For iPoint = 1 To nXValues
          For iCol = 1 To nNames
            If vaLabels(iPoint, iSeries) = vaNames(1, iCol) Then
              .Points(iPoint).Interior.ColorIndex = vaColor(1, iCol)
              Exit For
            End If
          Next
        Next
      End With
    Next

  End With

End Sub
</pre>
<p>To use the program, select your input data range, press Alt+F8 to bring up the Macro dialog, select <tt>ProcessInputRangeSelection</tt>, and press Run.</p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;title=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA&amp;bodytext=In%20Stack%20Columns%20In%20Order%20Of%20Size%20I%20showed%20a%20worksheet%20formula%20approach%20to%20sort%20columns%20in%20each%20stack%20in%20a%20stacked%20chart%20by%20size%2C%20not%20by%20series.%20When%20you%20create%20a%20stacked%20column%20chart%20the%20usual%20way%2C%20the%20columns%20are%20stacked%20in%20the%20same%20series%20order%2C%20a" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;title=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA&amp;notes=In%20Stack%20Columns%20In%20Order%20Of%20Size%20I%20showed%20a%20worksheet%20formula%20approach%20to%20sort%20columns%20in%20each%20stack%20in%20a%20stacked%20chart%20by%20size%2C%20not%20by%20series.%20When%20you%20create%20a%20stacked%20column%20chart%20the%20usual%20way%2C%20the%20columns%20are%20stacked%20in%20the%20same%20series%20order%2C%20a" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;t=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;title=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;title=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA&amp;annotation=In%20Stack%20Columns%20In%20Order%20Of%20Size%20I%20showed%20a%20worksheet%20formula%20approach%20to%20sort%20columns%20in%20each%20stack%20in%20a%20stacked%20chart%20by%20size%2C%20not%20by%20series.%20When%20you%20create%20a%20stacked%20column%20chart%20the%20usual%20way%2C%20the%20columns%20are%20stacked%20in%20the%20same%20series%20order%2C%20a" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;title=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;t=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;title=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=In%20Stack%20Columns%20In%20Order%20Of%20Size%20I%20showed%20a%20worksheet%20formula%20approach%20to%20sort%20columns%20in%20each%20stack%20in%20a%20stacked%20chart%20by%20size%2C%20not%20by%20series.%20When%20you%20create%20a%20stacked%20column%20chart%20the%20usual%20way%2C%20the%20columns%20are%20stacked%20in%20the%20same%20series%20order%2C%20a" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fstack-columns-in-order-of-size-with-vba%2F&amp;submitHeadline=Stack%20Columns%20In%20Order%20Of%20Size%20With%20VBA&amp;submitSummary=In%20Stack%20Columns%20In%20Order%20Of%20Size%20I%20showed%20a%20worksheet%20formula%20approach%20to%20sort%20columns%20in%20each%20stack%20in%20a%20stacked%20chart%20by%20size%2C%20not%20by%20series.%20When%20you%20create%20a%20stacked%20column%20chart%20the%20usual%20way%2C%20the%20columns%20are%20stacked%20in%20the%20same%20series%20order%2C%20a&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/stack-columns-in-order-of-size-with-vba/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Extend Range to Add New Series (VBA)</title>
		<link>http://peltiertech.com/WordPress/extend-range-to-add-new-series-vba/</link>
		<comments>http://peltiertech.com/WordPress/extend-range-to-add-new-series-vba/#comments</comments>
		<pubDate>Mon, 11 May 2009 12:33:09 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=1905</guid>
		<description><![CDATA[In some of the analyses I do, I&#8217;ll have a range of data and some charts based on this range. As the analysis unfolds, more columns may be added to the data range, and these have to be added to the chart. Until last week, I would add a series by copying the series formula [...]]]></description>
			<content:encoded><![CDATA[<p>In some of the analyses I do, I&#8217;ll have a range of data and some charts based on this range. As the analysis unfolds, more columns may be added to the data range, and these have to be added to the chart. Until last week, I would add a series by copying the series formula of an existing series, selecting the plot area, pasting the formula into the formula bar, editing the formula, and pressing Enter. Adding series has been the most tedious part of the analysis, and finally I decided to speed it up.</p>
<p>I wrote some code to create a new series based on the range next to the last series in the chart. The code copies the series formula from the last series, then edits the formula to reference the next column for the name and Y value parameters of the formula. Finally a new series is created, and the edited formula is assigned to this series.</p>
<p>Wonderful! One click to add each new series. Well, a few clicks, but it&#8217;s still much faster than the old way.</p>
<p><span id="more-1905"></span>Then I embellished the routine, added some checks (is a chart selected, is the series formula well-formed, are the ranges readily manipulated, etc.), generalized it to series by rows as well as by columns, and ran a few tests. Worked like a charm.</p>
<p>Here&#8217;s a sample chart which initially uses the first three data columns as the Y values for its three series. (With the plot area or chart area selected, the source data range for the chart is highlighted.)</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/bycol_before.png" alt="Series by Column: Before" /></p>
<p>Run the macro: press Alt+F8, select the macro from the list, and click Run.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/macro_addseriestoend.png" alt="Macro Dialog: AddSeriesToEnd" /></p>
<p>Here&#8217;s the result, which adds the fourth column as the chart&#8217;s fourth series.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/bycol_after.png" alt="Series by Column: After" /></p>
<p>Naturally, this can be changed using the Source Data dialog, or by dragging the colored outlines that indicate the source data. However, if the existing source data is in a pivot table, the chart would be converted to a pivot chart. Also, if the data is on another worksheet (or multiple worksheets), or if it is discontiguous or even more complicated, this is likely to produce unexpected results, or fail completely.</p>
<p>This procedure only requires that the last series have a series formula that it can parse. The added series is related only to the last series in the chart, using the next row or column in the worksheet.</p>
<p>Here is a chart that initially uses the first three rows of the data range for its three series.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/byrow_before.png" alt="Series by Row: Before" /></p>
<p>After the macro is run, the chart has four series, using the first four rows of the data range.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-05/byrow_after.png" alt="Series by Row: After" /></p>
<p>Here&#8217;s the VBA procedure. If you&#8217;re not sure how to implement it, refer to my introductory VBA tutorial, <a href="http://peltiertech.com/WordPress/how-to-use-someone-elses-macro/"title="How To Use Someone Else's Macro" >How To: Use Someone Else&#8217;s Macro</a></p>
<pre class="vbasmall">Sub AddSeriesToEnd()
  '' Add series to active chart
  '' Use same X values
  '' Use Name and Y values one column to right of last existing series
  Dim sFmla As String
  Dim iParen As Long
  Dim sFmlaArgs As String
  Dim vFmlaArgs As Variant
  Dim sMsg As String
  Dim iOffset As Long
  Dim jOffset As Long
  Dim rValues As Range
  Dim rName As Range

  If ActiveChart Is Nothing Then
    sMsg = "Series Formula too complicated to parse"
    GoTo CantHandle
  End If

  With ActiveChart
    sFmla = .SeriesCollection(.SeriesCollection.count).Formula
    iParen = InStr(sFmla, "(")
    sFmlaArgs = Mid$(sFmla, iParen + 1)
    sFmlaArgs = Left$(sFmlaArgs, Len(sFmlaArgs) - 1)
    vFmlaArgs = Split(sFmlaArgs, ",")

    If UBound(vFmlaArgs) + 1 - LBound(vFmlaArgs) &lt;&gt; 4 Then
      sMsg = "Series Formula too complicated to parse"
      GoTo CantHandle
    End If

    On Error Resume Next
    Set rValues = Range(vFmlaArgs(2))
    Set rName = Range(vFmlaArgs(0))
    On Error GoTo 0

    If rValues Is Nothing Then
      sMsg = "Last series values are not in a range"
      GoTo CantHandle
    End If

    If rValues.Rows.count &gt; 1 And rValues.Columns.count = 1 Then
      ' series in columns
      jOffset = 1
    ElseIf rValues.Rows.count = 1 And rValues.Columns.count &gt; 1 Then
      ' series in rows
      iOffset = 1
    Else
      ' one cell or multiple rows and columns
      sMsg = "Series values range cannot be parsed"
      GoTo CantHandle
    End If

    vFmlaArgs(3) = vFmlaArgs(3) + 1
    If Not rName Is Nothing Then
      vFmlaArgs(0) = rName.Offset(iOffset, jOffset).Address(True, True, xlA1, True)
    Else
      vFmlaArgs(0) = "New Series " &amp; vFmlaArgs(3)
    End If
    vFmlaArgs(2) = rValues.Offset(iOffset, jOffset).Address(True, True, xlA1, True)

    sFmlaArgs = Join(vFmlaArgs, ",")
    sFmla = Left$(sFmla, iParen) &amp; sFmlaArgs &amp; ")"
    With .SeriesCollection.NewSeries
      .Formula = sFmla
    End With
  End With

ExitSub:
  Exit Sub

CantHandle:
  MsgBox sMsg, vbCritical + vbOKOnly
  GoTo ExitSub
End Sub
 
</pre>
<p><br class="spacer_" /></p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;title=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29&amp;bodytext=In%20some%20of%20the%20analyses%20I%20do%2C%20I%27ll%20have%20a%20range%20of%20data%20and%20some%20charts%20based%20on%20this%20range.%20As%20the%20analysis%20unfolds%2C%20more%20columns%20may%20be%20added%20to%20the%20data%20range%2C%20and%20these%20have%20to%20be%20added%20to%20the%20chart.%20Until%20last%20week%2C%20I%20would%20add%20a%20series%20by%20copyi" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;title=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29&amp;notes=In%20some%20of%20the%20analyses%20I%20do%2C%20I%27ll%20have%20a%20range%20of%20data%20and%20some%20charts%20based%20on%20this%20range.%20As%20the%20analysis%20unfolds%2C%20more%20columns%20may%20be%20added%20to%20the%20data%20range%2C%20and%20these%20have%20to%20be%20added%20to%20the%20chart.%20Until%20last%20week%2C%20I%20would%20add%20a%20series%20by%20copyi" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;t=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;title=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;title=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29&amp;annotation=In%20some%20of%20the%20analyses%20I%20do%2C%20I%27ll%20have%20a%20range%20of%20data%20and%20some%20charts%20based%20on%20this%20range.%20As%20the%20analysis%20unfolds%2C%20more%20columns%20may%20be%20added%20to%20the%20data%20range%2C%20and%20these%20have%20to%20be%20added%20to%20the%20chart.%20Until%20last%20week%2C%20I%20would%20add%20a%20series%20by%20copyi" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;title=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;t=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;title=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=In%20some%20of%20the%20analyses%20I%20do%2C%20I%27ll%20have%20a%20range%20of%20data%20and%20some%20charts%20based%20on%20this%20range.%20As%20the%20analysis%20unfolds%2C%20more%20columns%20may%20be%20added%20to%20the%20data%20range%2C%20and%20these%20have%20to%20be%20added%20to%20the%20chart.%20Until%20last%20week%2C%20I%20would%20add%20a%20series%20by%20copyi" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fextend-range-to-add-new-series-vba%2F&amp;submitHeadline=Extend%20Range%20to%20Add%20New%20Series%20%28VBA%29&amp;submitSummary=In%20some%20of%20the%20analyses%20I%20do%2C%20I%27ll%20have%20a%20range%20of%20data%20and%20some%20charts%20based%20on%20this%20range.%20As%20the%20analysis%20unfolds%2C%20more%20columns%20may%20be%20added%20to%20the%20data%20range%2C%20and%20these%20have%20to%20be%20added%20to%20the%20chart.%20Until%20last%20week%2C%20I%20would%20add%20a%20series%20by%20copyi&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/extend-range-to-add-new-series-vba/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Interactive Charts with Checkboxes and VBA</title>
		<link>http://peltiertech.com/WordPress/interactive-charts-with-checkboxes-and-vba/</link>
		<comments>http://peltiertech.com/WordPress/interactive-charts-with-checkboxes-and-vba/#comments</comments>
		<pubDate>Wed, 01 Apr 2009 10:00:11 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[Dynamic Charts]]></category>
		<category><![CDATA[Interactive]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=1732</guid>
		<description><![CDATA[By definition an interactive dashboard contains methods for users to control which information is displayed. This can be accomplished in many ways. In Interactive Charting by Combo Box or Checkbox I wrote about using checkboxes and worksheet formulas to do this, and I elaborated on the technique in Interactive Charts with Checkboxes and Formulas.
There are a [...]]]></description>
			<content:encoded><![CDATA[<p>By definition an interactive dashboard contains methods for users to control which information is displayed. This can be accomplished in many ways. In <a href="http://peltiertech.com/Excel/Charts/ChartByControl.html" rel="nofollow" title="Interactive Charting by Combo Box or Checkbox | PTS Charts" >Interactive Charting by Combo Box or Checkbox</a> I wrote about using checkboxes and worksheet formulas to do this, and I elaborated on the technique in <a href="http://peltiertech.com/WordPress/interactive-charts-with-checkboxes-and-formulas/"title="Interactive Charts with Checkboxes and Formulas" >Interactive Charts with Checkboxes and Formulas</a>.</p>
<p>There are a couple of shortcomings with the checkboxes-plus-formulas approach. If a series is not displayed, there is still a space for its bars in a column chart. Also, the legend key (marker in the legend) for the series is still present even if the label is hidden.</p>
<p>By using VBA and checkboxes together, these shortcomings can be avoided. This technique makes use of Excel&#8217;s default behavior, which does not show data in a chart if the data range is hidden. This behavior can be changed on a chart by chart basis, so that changing it for one chart does not change it for other charts.</p>
<p><strong>Checkboxes and VBA</strong></p>
<p>Start with a line or column chart that includes all of the series you will allow the user to select from (A, B, and C in this example). Add a checkbox from the Forms toolbar for each series.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-03/DynoVBALine01.png" alt="" /><br />
 <img src="http://peltiertech.com/images/2009-03/DynoVBACol01.png" alt="" /></p>
<p><span id="more-1732"></span>The data is very simple, as shown below. The source data for the chart is in range J3:M8, and K1:M1 are the cells that the checkboxes are linked to. The range K1:M1 is named &#8220;TrueFalseRange&#8221; (select the range, click in the Name Box and type <em>TrueFalseRange</em>, and press ENTER).</p>
<p align="center"><img src="http://peltiertech.com/images/2009-03/DynoVBAData01.png" alt="" /></p>
<p>In addition to the checkboxes being linked to cells in the TrueFalseRange, each is also assigned to a macro named ClickSeriesTextbox. The macro is very simple:</p>
<pre class="vbasmall">Sub ClickSeriesCheckbox()
    Dim rCell As Range
    For Each rCell In ActiveSheet.Range("TrueFalseRange").Cells
        rCell.EntireColumn.Hidden = Not rCell.Value
    Next
End Sub
 </pre>
<p>All this macro does is check each cell in TrueFalseRange, if the value is False it hides the whole column, otherwise the column is displayed.</p>
<p>When (for example) the checkbox for Series B is unchecked, column L is hidden (signified by the thicker line between the column headers for columns K and M).</p>
<p align="center"><img src="http://peltiertech.com/images/2009-03/DynoVBAData02.png" alt="" /></p>
<p>Series B is completely eradicated from the charts, leaving behind no spurious legend key, and no empty slot for Series B&#8217;s columns in the column chart.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-03/DynoVBALine02.png" alt="" /><br />
 <img src="http://peltiertech.com/images/2009-03/DynoVBACol02.png" alt="" /></p>
<p>If the user unchecks all of the checkboxes, all of the series value columns are hidden (note the darker line between the headers for columns J and N).</p>
<p align="center"><img src="http://peltiertech.com/images/2009-03/DynoVBAData03.png" alt="" /></p>
<p>Not only are all series removed from the charts, but so are the axes, titles, legends, and other chart features. The chart is only visible in this screen shot because it has been activated.</p>
<p align="center"><img src="http://peltiertech.com/images/2009-03/DynoVBACol03.png" alt="" /></p>
<p><a href="http://peltiertech.com/images/2009-03/CheckboxAndVBA.zip" rel="nofollow" title="Zipped workbook chowing checkbox and VBA approach to interactive charts" >CheckboxAndVBA.zip</a> is a zipped workbook that illustrates this technique.<br class="spacer_" /></p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;title=Interactive%20Charts%20with%20Checkboxes%20and%20VBA&amp;bodytext=By%20definition%20an%20interactive%20dashboard%20contains%20methods%20for%20users%20to%20control%20which%20information%20is%20displayed.%20This%20can%20be%20accomplished%20in%20many%20ways.%20In%C2%A0Interactive%20Charting%20by%20Combo%20Box%20or%20Checkbox%20I%20wrote%20about%20using%20checkboxes%20and%20worksheet%20formula" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;title=Interactive%20Charts%20with%20Checkboxes%20and%20VBA&amp;notes=By%20definition%20an%20interactive%20dashboard%20contains%20methods%20for%20users%20to%20control%20which%20information%20is%20displayed.%20This%20can%20be%20accomplished%20in%20many%20ways.%20In%C2%A0Interactive%20Charting%20by%20Combo%20Box%20or%20Checkbox%20I%20wrote%20about%20using%20checkboxes%20and%20worksheet%20formula" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;t=Interactive%20Charts%20with%20Checkboxes%20and%20VBA" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Interactive%20Charts%20with%20Checkboxes%20and%20VBA%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;title=Interactive%20Charts%20with%20Checkboxes%20and%20VBA" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;title=Interactive%20Charts%20with%20Checkboxes%20and%20VBA&amp;annotation=By%20definition%20an%20interactive%20dashboard%20contains%20methods%20for%20users%20to%20control%20which%20information%20is%20displayed.%20This%20can%20be%20accomplished%20in%20many%20ways.%20In%C2%A0Interactive%20Charting%20by%20Combo%20Box%20or%20Checkbox%20I%20wrote%20about%20using%20checkboxes%20and%20worksheet%20formula" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;title=Interactive%20Charts%20with%20Checkboxes%20and%20VBA" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;t=Interactive%20Charts%20with%20Checkboxes%20and%20VBA" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Interactive%20Charts%20with%20Checkboxes%20and%20VBA&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;title=Interactive%20Charts%20with%20Checkboxes%20and%20VBA&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=By%20definition%20an%20interactive%20dashboard%20contains%20methods%20for%20users%20to%20control%20which%20information%20is%20displayed.%20This%20can%20be%20accomplished%20in%20many%20ways.%20In%C2%A0Interactive%20Charting%20by%20Combo%20Box%20or%20Checkbox%20I%20wrote%20about%20using%20checkboxes%20and%20worksheet%20formula" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Finteractive-charts-with-checkboxes-and-vba%2F&amp;submitHeadline=Interactive%20Charts%20with%20Checkboxes%20and%20VBA&amp;submitSummary=By%20definition%20an%20interactive%20dashboard%20contains%20methods%20for%20users%20to%20control%20which%20information%20is%20displayed.%20This%20can%20be%20accomplished%20in%20many%20ways.%20In%C2%A0Interactive%20Charting%20by%20Combo%20Box%20or%20Checkbox%20I%20wrote%20about%20using%20checkboxes%20and%20worksheet%20formula&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/interactive-charts-with-checkboxes-and-vba/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Make Your Recorded Macro Independent of Which Sheet is Active</title>
		<link>http://peltiertech.com/WordPress/make-recorded-macro-independent-of-sheet/</link>
		<comments>http://peltiertech.com/WordPress/make-recorded-macro-independent-of-sheet/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 13:57:43 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[recorded macro]]></category>
		<category><![CDATA[Variables]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=1682</guid>
		<description><![CDATA[When you run a recorded macro, it may work great, except it keeps referring back to the original worksheet. It also refers back to the original data range. Both of these situations can be remedied by editing of the recorded macro.
In How To: Fix a Recorded Macro I showed how to make a recorded macro [...]]]></description>
			<content:encoded><![CDATA[<p>When you run a recorded macro, it may work great, except it keeps referring back to the original worksheet. It also refers back to the original data range. Both of these situations can be remedied by editing of the recorded macro.</p>
<p>In <a href="http://peltiertech.com/WordPress/how-to-fix-a-recorded-macro/"title="How To: Fix a Recorded Macro" >How To: Fix a Recorded Macro</a> I showed how to make a recorded macro more efficient by omitting and combining statements and eliminating the selection of objects during macro execution. This is a more specific example that makes the macro independent of the conditions (active sheet and selection) when it was recorded.</p>
<p>In this example, a macro is recorded after data is selected to capture the steps required to insert a chart object in the active worksheet. This recording was made in Excel 2003; the steps in 2007 are similar, and I&#8217;ll cover them in another post.</p>
<p>Here is our original data in C7:F13 of Sheet1:<span id="more-1682"></span></p>
<p align="center"><img src="http://peltiertech.com/images/2009-03/MacroData.png" alt="Data for recorded macro /&gt;&lt;/p&gt; &lt;p&gt;I recorded this macro while I created a chart, without changing any other formatting.&lt;/p&gt; &lt;pre&gt;Sub Macro1() ' ' Macro4 Macro ' Macro recorded 3/16/2009 by Jon Peltier '  '     Charts.Add     ActiveChart.ChartType = xlXYScatterLinesNoMarkers     ActiveChart.SetSourceData Source:=Sheets(" /></p>
<h2 style="margin: 0pt 0pt 18px; font-size: 1em;">The Recorded Macro</h2>
<p>I recorded this simple macro while creating the chart, without changing any formatting:</p>
<pre>Sub Macro1()
'
' Macro4 Macro
' Macro recorded 3/16/2009 by Jon Peltier
'

'
    Charts.Add
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range("C7:F13")
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Sheet1"
End Sub
 </pre>
<p>The worksheet name and selected range appear hard-coded in this macro. No matter what sheet is active or what range is selected, the chart will always appear on Sheet1 using the data in C7:F13 of Sheet1.</p>
<p>I will describe a couple ways to change this, both involving variables which are declared and assigned before the chart is created.</p>
<h2 style="margin: 0pt 0pt 18px; font-size: 1em;">Fixing the Macro with String Variables<br />
</h2>
<p>The sheet name and range address are present in the recorded macros as simple text. We can define string variables to contain the sheet name and the range address, as shown in this fixed macro.</p>
<pre>Sub Macro1a()
'
' Macro4 Macro
' Macro recorded 3/16/2009 by Jon Peltier
' Fixed to use text variables for sheet and range

'
    Dim <span style="color: #3366ff;">sSheetName</span> As String
    Dim <span style="color: #3366ff;">sDataRange</span> As String

    <span style="color: #3366ff;">sSheetName</span> = ActiveSheet.Name
    <span style="color: #3366ff;">sDataRange</span> = Selection.Address

    Charts.Add
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SetSourceData Source:=Sheets(<span style="color: #3366ff;">sSheetName</span>).Range(<span style="color: #3366ff;">sDataRange</span>)
    ActiveChart.Location Where:=xlLocationAsObject, Name:=<span style="color: #3366ff;">sSheetName</span>
End Sub
 </pre>
<h2 style="margin: 0pt 0pt 18px; font-size: 1em;">Fixing the Macro with Object Variables<br />
</h2>
<p>We can also use object variables, that is, a worksheet object for the active sheet and a range object for the data, as in the following fixed up macro.</p>
<pre>Sub Macro1b()
'
' Macro4 Macro
' Macro recorded 3/16/2009 by Jon Peltier
' Fixed to use object variables for sheet and range

'
    Dim <span style="color: #ff6600;">wksData</span> As Worksheet
    Dim <span style="color: #ff6600;">rngData</span> As Range

    Set <span style="color: #ff6600;">wksData</span> = ActiveSheet
    Set <span style="color: #ff6600;">rngData</span> = Selection

    Charts.Add
    ActiveChart.ChartType = xlXYScatterLinesNoMarkers
    ActiveChart.SetSourceData Source:=<span style="color: #ff6600;">rngData</span>
    ActiveChart.Location Where:=xlLocationAsObject, Name:=<span style="color: #ff6600;">wksData</span>.Name
End Sub
 </pre>
<p>By adding only a few lines of code, the recorded macro has been made much more flexible. The choice whether to use string or object variables is not important in this case. In more complex procedures, one or the other approach may be advantageous, if a particular variable type is more useful later in the procedure.</p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;title=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active&amp;bodytext=When%20you%20run%20a%20recorded%20macro%2C%20it%20may%20work%20great%2C%20except%20it%20keeps%20referring%20back%20to%20the%20original%20worksheet.%20It%20also%20refers%20back%20to%20the%20original%20data%20range.%20Both%20of%20these%20situations%20can%20be%20remedied%20by%20editing%20of%20the%20recorded%20macro.%0D%0A%0D%0AIn%20How%20To%3A%20Fix%20a" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;title=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active&amp;notes=When%20you%20run%20a%20recorded%20macro%2C%20it%20may%20work%20great%2C%20except%20it%20keeps%20referring%20back%20to%20the%20original%20worksheet.%20It%20also%20refers%20back%20to%20the%20original%20data%20range.%20Both%20of%20these%20situations%20can%20be%20remedied%20by%20editing%20of%20the%20recorded%20macro.%0D%0A%0D%0AIn%20How%20To%3A%20Fix%20a" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;t=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;title=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;title=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active&amp;annotation=When%20you%20run%20a%20recorded%20macro%2C%20it%20may%20work%20great%2C%20except%20it%20keeps%20referring%20back%20to%20the%20original%20worksheet.%20It%20also%20refers%20back%20to%20the%20original%20data%20range.%20Both%20of%20these%20situations%20can%20be%20remedied%20by%20editing%20of%20the%20recorded%20macro.%0D%0A%0D%0AIn%20How%20To%3A%20Fix%20a" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;title=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;t=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;title=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=When%20you%20run%20a%20recorded%20macro%2C%20it%20may%20work%20great%2C%20except%20it%20keeps%20referring%20back%20to%20the%20original%20worksheet.%20It%20also%20refers%20back%20to%20the%20original%20data%20range.%20Both%20of%20these%20situations%20can%20be%20remedied%20by%20editing%20of%20the%20recorded%20macro.%0D%0A%0D%0AIn%20How%20To%3A%20Fix%20a" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fmake-recorded-macro-independent-of-sheet%2F&amp;submitHeadline=Make%20Your%20Recorded%20Macro%20Independent%20of%20Which%20Sheet%20is%20Active&amp;submitSummary=When%20you%20run%20a%20recorded%20macro%2C%20it%20may%20work%20great%2C%20except%20it%20keeps%20referring%20back%20to%20the%20original%20worksheet.%20It%20also%20refers%20back%20to%20the%20original%20data%20range.%20Both%20of%20these%20situations%20can%20be%20remedied%20by%20editing%20of%20the%20recorded%20macro.%0D%0A%0D%0AIn%20How%20To%3A%20Fix%20a&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/make-recorded-macro-independent-of-sheet/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Validation Functions</title>
		<link>http://peltiertech.com/WordPress/validation-functions/</link>
		<comments>http://peltiertech.com/WordPress/validation-functions/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 15:52:38 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=1672</guid>
		<description><![CDATA[Today in Daily Dose of Excel, Dick wrote about Code Construction. One of his topics was to pull pieces of a routine out into a separate procedure, particularly if that piece of a routine included fiddling with On Error Resume Next and On Error Goto 0.
Dick&#8217;s example was to replace this section of code:
On Error [...]]]></description>
			<content:encoded><![CDATA[<p>Today in <strong>Daily Dose of Excel</strong>, Dick wrote about <a href="http://www.dailydoseofexcel.com/archives/2009/03/11/code-construction/" rel="nofollow" title="Code Construction" >Code Construction</a>. One of his topics was to pull pieces of a routine out into a separate procedure, particularly if that piece of a routine included fiddling with <tt class="tt">On Error Resume Next</tt> and <tt class="tt">On Error Goto 0</tt>.</p>
<p>Dick&#8217;s example was to replace this section of code:<span id="more-1672"></span></p>
<pre class="vbasmall">On Error Resume Next
mcolMyCollection.Add oMyObject, CStr(oMyObject.ID)
On Error GoTo ErrorHandler
 
</pre>
<p>with this single line</p>
<pre class="vbasmall">AddToColl mcolMyCollection, oMyObject, <span style="color: #0000dd;">CStr</span><span style="color: #008800;">(</span>oMyObject.ID<span style="color: #008800;">)</span>
 
</pre>
<p>that calls a small subroutine:</p>
<pre class="vbasmall">Public Sub AddToColl(col As Collection, vToAdd As Variant, sKey As String)
    On Error Resume Next
    col.Add vToAdd, sKey
    On Error GoTo 0
End Sub
 
</pre>
<p>This takes the interrupted error handling out of the main code, preventing a distracted programmer from turning it off altogether.</p>
<p>My suggestion was to turn the AddToColl subroutine into a function, which returns information to the calling sub. It might be a Boolean to indicate whether the action within the procedure was successful, or a Long (a User Defined Type) to indicate what error may have occurred, or as AlexJ suggested, a string containing an error message.</p>
<p>I often take a compound approach, where the function returns True or False to signify success, and an argument passes back to the calling procedure with an applicable message.</p>
<p>The following function is an example. It accepts the address of a range and returns a message as arguments, and returns True if the range meets the criteria.</p>
<pre class="vbasmall">Function NumericRangeValidator(sAddress As String, sMessage As String) As Boolean
  Dim rTest As Range

  ' false until it passes all tests
  NumericRangeValidator = False

  On Error Resume Next
  Set rTest = Range(sAddress)
  On Error GoTo 0

  ' is address a valid range
  If rTest Is Nothing Then
    sMessage = "Invalid address."
    GoTo ExitFunction
  End If

  ' is there anything in the range
  If WorksheetFunction.CountA(rTest) = 0 Then
    sMessage = "Empty range."
    GoTo ExitFunction
  End If

  ' is the range completely populated with numbers
  If WorksheetFunction.Count(rTest) &lt; rTest.Rows.Count * rTest.Columns.Count Then
    sMessage = "Range not fully populated with numbers."
    GoTo ExitFunction
  End If

  ' if execution got this far, it's a valid range
  NumericRangeValidator = True

ExitFunction:

End Function
 
</pre>
<p>The message describes the problem with the range (invalid address, empty range, etc.), and only if all tests are passed is the value of the function True.</p>
<p>The following example shows how to use such a function. An If statement tests the value of the function. If it is true, the range is processed further by the calling code (in this case it performs a simple sum). If the function is false, the error message passed back by the function is displayed.</p>
<pre class="vbasmall">Sub TestNumericRangeValidator()
  Dim strAddress As String, strMessage As String

  strAddress = Selection.Address

  If NumericRangeValidator(strAddress, strMessage) Then
    ' process the successful range
    strMessage = "The sum of values in the range is " &amp; WorksheetFunction.Sum(Range(strAddress))
    MsgBox strMessage, vbExclamation
  Else
    ' show the message
    MsgBox strMessage, vbCritical
  End If

End Sub
 
</pre>
<p>This approach is very flexible. The routine can be called from multiple places in a larger application. Each calling location can perform a different action based on the success of the function. The code above added the values, another routine could send it to a chart. If the range is not validated, the code above showed a message box, but it could have simply skipped the message box and died quietly. Or it could have shown the message in a label on a user form to explain to the user why the range they selected on the form cannot be used.</p>
<p>The benefit of passing arguments as well as an actual value for the function is I don&#8217;t have to test whether a string matches some stored string to determine whether the range was validated.</p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;title=Validation%20Functions&amp;bodytext=Today%20in%20Daily%20Dose%20of%20Excel%2C%20Dick%20wrote%20about%20Code%20Construction.%20One%20of%20his%20topics%20was%20to%20pull%20pieces%20of%20a%20routine%20out%20into%20a%20separate%20procedure%2C%20particularly%20if%20that%20piece%20of%20a%20routine%20included%20fiddling%20with%20On%20Error%20Resume%20Next%20and%20On%20Error%20Goto%200" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;title=Validation%20Functions&amp;notes=Today%20in%20Daily%20Dose%20of%20Excel%2C%20Dick%20wrote%20about%20Code%20Construction.%20One%20of%20his%20topics%20was%20to%20pull%20pieces%20of%20a%20routine%20out%20into%20a%20separate%20procedure%2C%20particularly%20if%20that%20piece%20of%20a%20routine%20included%20fiddling%20with%20On%20Error%20Resume%20Next%20and%20On%20Error%20Goto%200" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;t=Validation%20Functions" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Validation%20Functions%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;title=Validation%20Functions" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;title=Validation%20Functions&amp;annotation=Today%20in%20Daily%20Dose%20of%20Excel%2C%20Dick%20wrote%20about%20Code%20Construction.%20One%20of%20his%20topics%20was%20to%20pull%20pieces%20of%20a%20routine%20out%20into%20a%20separate%20procedure%2C%20particularly%20if%20that%20piece%20of%20a%20routine%20included%20fiddling%20with%20On%20Error%20Resume%20Next%20and%20On%20Error%20Goto%200" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;title=Validation%20Functions" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;t=Validation%20Functions" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Validation%20Functions&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;title=Validation%20Functions&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=Today%20in%20Daily%20Dose%20of%20Excel%2C%20Dick%20wrote%20about%20Code%20Construction.%20One%20of%20his%20topics%20was%20to%20pull%20pieces%20of%20a%20routine%20out%20into%20a%20separate%20procedure%2C%20particularly%20if%20that%20piece%20of%20a%20routine%20included%20fiddling%20with%20On%20Error%20Resume%20Next%20and%20On%20Error%20Goto%200" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvalidation-functions%2F&amp;submitHeadline=Validation%20Functions&amp;submitSummary=Today%20in%20Daily%20Dose%20of%20Excel%2C%20Dick%20wrote%20about%20Code%20Construction.%20One%20of%20his%20topics%20was%20to%20pull%20pieces%20of%20a%20routine%20out%20into%20a%20separate%20procedure%2C%20particularly%20if%20that%20piece%20of%20a%20routine%20included%20fiddling%20with%20On%20Error%20Resume%20Next%20and%20On%20Error%20Goto%200&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/validation-functions/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>VBA to Filter Chart Data Range</title>
		<link>http://peltiertech.com/WordPress/vba-to-filter-chart-data-range/</link>
		<comments>http://peltiertech.com/WordPress/vba-to-filter-chart-data-range/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 11:00:27 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[chart data]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=1147</guid>
		<description><![CDATA[In the comments of&#160;Choice of Category Axis Order a reader asked how to use only part of a range as the source data for a chart. Her data and chart initially looked like this:

The line &#8220;Total Other Items&#8221; is a sum of the items below it in the list, and the chart does not show [...]]]></description>
			<content:encoded><![CDATA[<p>In the comments of&nbsp;<a href="http://peltiertech.com/WordPress/2008/08/10/choice-of-category-axis-order/"title="Choice of Category Axis Order" >Choice of Category Axis Order</a> a reader asked how to use only part of a range as the source data for a chart. Her data and chart initially looked like this:</p>
<p align="center"><img src="http://peltiertech.com/images/2008-12/RangeAndChartOne.png" alt="original chart" /></p>
<p>The line &#8220;Total Other Items&#8221; is a sum of the items below it in the list, and the chart does not show this subtotal. The series formula looks like:</p>
<pre class="vbasmall">=SERIES(Sheet1!$B$1,(Sheet1!$A$2:$A$4,Sheet1!$A$6:$A$8),
                    (Sheet1!$B$2:$B$4,Sheet1!$B$6:$B$8),1)
&nbsp;</pre>
<p><span id="more-1147"></span>Notice both the X and Y arguments each consist of two ranges, separated by commas, and surrounded by parentheses. The reader inserted some items, both above and below the subtotal line:</p>
<p align="center"><img src="http://peltiertech.com/images/2008-12/RangeAndChartTwo.png" alt="intermediate chart" /></p>
<p>The chart does not show the updated data. The series formula looks like:</p>
<pre class="vbasmall">=SERIES(Sheet1!$B$1,(Sheet1!$A$2:$A$4,Sheet1!$A$7:$A$9),
                    (Sheet1!$B$2:$B$4,Sheet1!$B$7:$B$9),1)
&nbsp;</pre>
<p>The reader wanted some way to show all of the individual items, and not the subtotal, dynamically or programmatically, to avoid the tedium of updating the chart manually whenever the range was changed. I suggested two ranges, one without the subtotal for charting, and one with the subtotal for the boss. My suggestion was rejected.</p>
<p>I dusted off an old VBA routine which I had originally written to keep zero and blank values out of a chart, and modified it to keep the subtotal line out. The routine redefined some names in the worksheet, and the names were used in the chart. The thing to do is run the routine once, then adjust the chart series formula to this:</p>
<pre class="vbasmall">=SERIES(Sheet1!$B$1,Book1!myARange,Book1!myMRange,1)
&nbsp;</pre>
<p>Now whenever thee VBA code is run, it redefines myARange and myBRange. Since the chart series is defined by these, running the code indirectly changes the chart as well. Here is a chart of the table above, updated with the changed names:</p>
<p align="center"><img src="http://peltiertech.com/images/2008-12/RangeAndChartThree.png" alt="final chart" /></p>
<p>Assuming the data is in columns A and B, with headers in row 1 and actual data starting in row 2, and nothing clutters rows A and B beneath the data. This is the macro which, when run whenever the table is changed, will update the chart.</p>
<pre class="vbasmall">Sub FilterChartSourceDataRange()
  Dim myCell As Range
  Dim myARange As Range, myBRange As Range
  Dim nRow As Long, iRow As Long
  Dim sAvoid As String

  sAvoid = "Total Other Items"

  ' Find last row
  nRow = ActiveSheet.Cells(65536, "A").End(xlUp).Row

  For iRow = 2 To nRow
    Set myCell = ActiveSheet.Cells(iRow, "A")
    If myCell.Value &lt;&gt; sAvoid And Not IsEmpty(myCell) Then
      ' cell is okay, so add to charting ranges
      If myARange Is Nothing Then
        Set myARange = myCell
        Set myBRange = myCell.Offset(0, 1)
      Else
        Set myARange = Union(myARange, myCell)
        Set myBRange = Union(myBRange, myCell.Offset(0, 1))
      End If
    End If
  Next

  ' redefine ranges in worksheet
  ActiveWorkbook.Names.Add Name:="myARange", _
      RefersTo:="='" &amp; ActiveSheet.Name &amp; "'!" &amp; myARange.Address
  ActiveWorkbook.Names.Add Name:="myBRange", _
      RefersTo:="='" &amp; ActiveSheet.Name &amp; "'!" &amp; myBRange.Address

  ' clean up
  Set myCell = Nothing
  Set myARange = Nothing
  Set myBRange = Nothing
End Sub
&nbsp;</pre>
<p>&nbsp;</p>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;title=VBA%20to%20Filter%20Chart%20Data%20Range&amp;bodytext=In%20the%20comments%20of%26nbsp%3BChoice%20of%20Category%20Axis%20Order%20a%20reader%20asked%20how%20to%20use%20only%20part%20of%20a%20range%20as%20the%20source%20data%20for%20a%20chart.%20Her%20data%20and%20chart%20initially%20looked%20like%20this%3A%0D%0A%0D%0A%0D%0A%0D%0AThe%20line%20%22Total%20Other%20Items%22%20is%20a%20sum%20of%20the%20items%20below%20it%20in%20" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;title=VBA%20to%20Filter%20Chart%20Data%20Range&amp;notes=In%20the%20comments%20of%26nbsp%3BChoice%20of%20Category%20Axis%20Order%20a%20reader%20asked%20how%20to%20use%20only%20part%20of%20a%20range%20as%20the%20source%20data%20for%20a%20chart.%20Her%20data%20and%20chart%20initially%20looked%20like%20this%3A%0D%0A%0D%0A%0D%0A%0D%0AThe%20line%20%22Total%20Other%20Items%22%20is%20a%20sum%20of%20the%20items%20below%20it%20in%20" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;t=VBA%20to%20Filter%20Chart%20Data%20Range" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=VBA%20to%20Filter%20Chart%20Data%20Range%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;title=VBA%20to%20Filter%20Chart%20Data%20Range" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;title=VBA%20to%20Filter%20Chart%20Data%20Range&amp;annotation=In%20the%20comments%20of%26nbsp%3BChoice%20of%20Category%20Axis%20Order%20a%20reader%20asked%20how%20to%20use%20only%20part%20of%20a%20range%20as%20the%20source%20data%20for%20a%20chart.%20Her%20data%20and%20chart%20initially%20looked%20like%20this%3A%0D%0A%0D%0A%0D%0A%0D%0AThe%20line%20%22Total%20Other%20Items%22%20is%20a%20sum%20of%20the%20items%20below%20it%20in%20" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;title=VBA%20to%20Filter%20Chart%20Data%20Range" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;t=VBA%20to%20Filter%20Chart%20Data%20Range" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=VBA%20to%20Filter%20Chart%20Data%20Range&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;title=VBA%20to%20Filter%20Chart%20Data%20Range&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=In%20the%20comments%20of%26nbsp%3BChoice%20of%20Category%20Axis%20Order%20a%20reader%20asked%20how%20to%20use%20only%20part%20of%20a%20range%20as%20the%20source%20data%20for%20a%20chart.%20Her%20data%20and%20chart%20initially%20looked%20like%20this%3A%0D%0A%0D%0A%0D%0A%0D%0AThe%20line%20%22Total%20Other%20Items%22%20is%20a%20sum%20of%20the%20items%20below%20it%20in%20" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fvba-to-filter-chart-data-range%2F&amp;submitHeadline=VBA%20to%20Filter%20Chart%20Data%20Range&amp;submitSummary=In%20the%20comments%20of%26nbsp%3BChoice%20of%20Category%20Axis%20Order%20a%20reader%20asked%20how%20to%20use%20only%20part%20of%20a%20range%20as%20the%20source%20data%20for%20a%20chart.%20Her%20data%20and%20chart%20initially%20looked%20like%20this%3A%0D%0A%0D%0A%0D%0A%0D%0AThe%20line%20%22Total%20Other%20Items%22%20is%20a%20sum%20of%20the%20items%20below%20it%20in%20&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/vba-to-filter-chart-data-range/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Count Bold Cells in a Range</title>
		<link>http://peltiertech.com/WordPress/count-bold-cells-in-a-range/</link>
		<comments>http://peltiertech.com/WordPress/count-bold-cells-in-a-range/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 20:37:33 +0000</pubDate>
		<dc:creator>Jon Peltier</dc:creator>
				<category><![CDATA[VBA]]></category>
		<category><![CDATA[user defined functions]]></category>

		<guid isPermaLink="false">http://peltiertech.com/WordPress/?p=721</guid>
		<description><![CDATA[I was asked if there is any way to count all of the bold cells in a range. Of course there is. Here is a simple VBA function that carries out this task:
Function CountBoldCellsInRange(rng As Range) As Long
  Dim rCell As Range
  Dim iBold As Long

  iBold = 0
  For Each [...]]]></description>
			<content:encoded><![CDATA[<p>I was asked if there is any way to count all of the bold cells in a range. Of course there is. Here is a simple VBA function that carries out this task:</p>
<pre class="vbasmall">Function CountBoldCellsInRange(rng As Range) As Long
  Dim rCell As Range
  Dim iBold As Long

  iBold = 0
  For Each rCell In rng
    If rCell.Font.Bold Then
      iBold = iBold + 1
    End If
  Next

  CountBoldCellsInRange = iBold
End Function
 </pre>
<p><span id="more-721"></span>This function can be called in a number of ways. Here are two examples showing how to call the function from other VBA procedures.</p>
<pre class="vbasmall">Sub Test1CountBoldCellsInRange()
  Dim i As Long
  If TypeName(Selection) = "Range" Then
    i = CountBoldCellsInRange(Selection)
    MsgBox "There are " &amp; i &amp; " bold cells in the selected range."
  Else
    MsgBox "The selected object is not a range."
  End If
End Sub
 </pre>
<p align="center"><img src="http://peltiertech.com/WordPress/wp-content/img200810/CountBoldCells.png" alt="" /></p>
<pre class="vbasmall">Sub Test2CountBoldCellsInRange()
  Dim i As Long
  Dim r As Range
  Dim sAddress As String
  sAddress = "A1:F20"
  Set r = ActiveSheet.Range(sAddress)
  i = CountBoldCellsInRange(r)
  MsgBox "There are " &amp; i &amp; " bold cells in " &amp; sAddress &amp; "."
End Sub
 </pre>
<p>You can even call it from a worksheet cell, using this syntax.</p>
<pre class="vbasmall">=CountBoldCellsInRange(A1:F20)
 </pre>



Bookmark and share this entry:


	<a rel="nofollow"  href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;title=Count%20Bold%20Cells%20in%20a%20Range&amp;bodytext=I%20was%20asked%20if%20there%20is%20any%20way%20to%20count%20all%20of%20the%20bold%20cells%20in%20a%20range.%20Of%20course%20there%20is.%20Here%20is%20a%20simple%20VBA%20function%20that%20carries%20out%20this%20task%3A%0D%0A%0D%0AFunction%20CountBoldCellsInRange%28rng%20As%20Range%29%20As%20Long%0D%0A%20%20Dim%20rCell%20As%20Range%0D%0A%20%20Dim%20iBold%20As%20Lon" title="Digg"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;title=Count%20Bold%20Cells%20in%20a%20Range&amp;notes=I%20was%20asked%20if%20there%20is%20any%20way%20to%20count%20all%20of%20the%20bold%20cells%20in%20a%20range.%20Of%20course%20there%20is.%20Here%20is%20a%20simple%20VBA%20function%20that%20carries%20out%20this%20task%3A%0D%0A%0D%0AFunction%20CountBoldCellsInRange%28rng%20As%20Range%29%20As%20Long%0D%0A%20%20Dim%20rCell%20As%20Range%0D%0A%20%20Dim%20iBold%20As%20Lon" title="del.icio.us"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;t=Count%20Bold%20Cells%20in%20a%20Range" title="Facebook"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F" title="Technorati"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://twitter.com/home?status=Count%20Bold%20Cells%20in%20a%20Range%20-%20http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F" title="Twitter"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;title=Count%20Bold%20Cells%20in%20a%20Range" title="StumbleUpon"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;title=Count%20Bold%20Cells%20in%20a%20Range&amp;annotation=I%20was%20asked%20if%20there%20is%20any%20way%20to%20count%20all%20of%20the%20bold%20cells%20in%20a%20range.%20Of%20course%20there%20is.%20Here%20is%20a%20simple%20VBA%20function%20that%20carries%20out%20this%20task%3A%0D%0A%0D%0AFunction%20CountBoldCellsInRange%28rng%20As%20Range%29%20As%20Long%0D%0A%20%20Dim%20rCell%20As%20Range%0D%0A%20%20Dim%20iBold%20As%20Lon" title="Google Bookmarks"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;title=Count%20Bold%20Cells%20in%20a%20Range" title="Reddit"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;t=Count%20Bold%20Cells%20in%20a%20Range" title="MySpace"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://slashdot.org/bookmark.pl?title=Count%20Bold%20Cells%20in%20a%20Range&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F" title="Slashdot"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;title=Count%20Bold%20Cells%20in%20a%20Range&amp;source=PTS+Blog+PTS+Excel+Charts+and+Tutorials+Blog&amp;summary=I%20was%20asked%20if%20there%20is%20any%20way%20to%20count%20all%20of%20the%20bold%20cells%20in%20a%20range.%20Of%20course%20there%20is.%20Here%20is%20a%20simple%20VBA%20function%20that%20carries%20out%20this%20task%3A%0D%0A%0D%0AFunction%20CountBoldCellsInRange%28rng%20As%20Range%29%20As%20Long%0D%0A%20%20Dim%20rCell%20As%20Range%0D%0A%20%20Dim%20iBold%20As%20Lon" title="LinkedIn"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" class="sociable-hovers" /></a>
	<a rel="nofollow"  href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fpeltiertech.com%2FWordPress%2Fcount-bold-cells-in-a-range%2F&amp;submitHeadline=Count%20Bold%20Cells%20in%20a%20Range&amp;submitSummary=I%20was%20asked%20if%20there%20is%20any%20way%20to%20count%20all%20of%20the%20bold%20cells%20in%20a%20range.%20Of%20course%20there%20is.%20Here%20is%20a%20simple%20VBA%20function%20that%20carries%20out%20this%20task%3A%0D%0A%0D%0AFunction%20CountBoldCellsInRange%28rng%20As%20Range%29%20As%20Long%0D%0A%20%20Dim%20rCell%20As%20Range%0D%0A%20%20Dim%20iBold%20As%20Lon&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://peltiertech.com/WordPress/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" class="sociable-hovers" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://peltiertech.com/WordPress/count-bold-cells-in-a-range/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
