<?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/"
	>

<channel>
	<title>Daily Dose of Excel</title>
	<atom:link href="http://www.dailydoseofexcel.com/index.php?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.dailydoseofexcel.com</link>
	<description>Daily posts of Excel tips...and other stuff</description>
	<pubDate>Thu, 02 Sep 2010 11:30:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Setting a Base Directory</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/09/02/setting-a-base-directory/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/09/02/setting-a-base-directory/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 11:30:25 +0000</pubDate>
		<dc:creator>Dick Kusleika</dc:creator>
		
		<category><![CDATA[File Operations]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4148</guid>
		<description><![CDATA[If you want your Excel app to default to a specific directory when opening or saving files, see Changing the Current Directory.  Be sure to read the comments.
I have a slightly different situation.  I want my app to open to a specific directory that will give me easy access to sub directories.  [...]]]></description>
			<content:encoded><![CDATA[<p>If you want your Excel app to default to a specific directory when opening or saving files, see <a href="http://www.dailydoseofexcel.com/archives/2005/08/30/changing-the-current-directory/">Changing the Current Directory</a>.  Be sure to read the comments.</p>
<p>I have a slightly different situation.  I want my app to open to a specific directory that will give me easy access to sub directories.  However, if the current directory is already a sub directory of my base directory, then it&#8217;s pretty likely to be in the one I want and I don&#8217;t want to change it.  For example, my base directory is</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">S:\Flash\Payroll\</div></div>
<p>If I&#8217;m in <code class="codecolorer text default"><span class="text">S:\Flash\Payroll\2010\0824\&quot;</span></code>, then that&#8217;s probably the directory I want and don&#8217;t want to change.  However, if I&#8217;m in <code class="codecolorer text default"><span class="text">S:\Flash\Accounting\Reporting\</span></code>, then I want to change to my base directory.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> SetFolderToPayroll()<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> InStr(1, CurDir, <span style="color: #800000;">&quot;S:\Flash\Payroll&quot;</span>) = 0 <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ChDrive <span style="color: #800000;">&quot;S:&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ChDir <span style="color: #800000;">&quot;S:\Flash\Payroll\&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>Pretty simple.  If it&#8217;s at the base or a sub directory, don&#8217;t do anything.  If it&#8217;s anything else, go to the base directory.  Upon further reflection, though, it seems that a more general purpose procedure is in order.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Public</span> <span style="color: #000080;">Sub</span> SetBaseDirectory(sBase <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>)<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> Left$(CurDir, Len(sBase)) &lt;&gt; sBase <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; ChDrive Left$(sBase, 2)<br />
&nbsp; &nbsp; &nbsp; &nbsp; ChDir sBase<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>Now I can call it from multiple locations and pass in the base directory.  If the left x characters is the base directory, don&#8217;t do anything.  Otherwise, change the drive to the first two characters of sBase (e.g. &#8220;S:&#8221;) and change the directory to the base.  It won&#8217;t work with UNC paths.  Any other problems with it?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/09/02/setting-a-base-directory/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Count Active Customers</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/09/01/count-active-customers/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/09/01/count-active-customers/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 11:30:50 +0000</pubDate>
		<dc:creator>Dick Kusleika</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4144</guid>
		<description><![CDATA[Jake wants to know, given an active date and an inactive date, how to count the customers that were active in a certain time period.

The ones we want are highlighted in yellow.  The formula is
=COUNT(D2:D21)-SUMPRODUCT(($C$2:$C$21&#62;=D25)+($D$2:$D$21&#60; =C25))
It&#8217;s easier to figure out who is not active during that date range and subtract it from the total. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.dailydoseofexcel.com/archives/2004/03/29/sumif-between-two-dates/#comment-49565">Jake wants to know</a>, given an active date and an inactive date, how to count the customers that were active in a certain time period.</p>
<p><img src="http://www.dailydoseofexcel.com/blogpix/countifactive.gif" height="465" width="516" alt="" /></p>
<p>The ones we want are highlighted in yellow.  The formula is</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=COUNT(D2:D21)-SUMPRODUCT(($C$2:$C$21&gt;=D25)+($D$2:$D$21&lt; =C25))</div></div>
<p>It&#8217;s easier to figure out who is not active during that date range and subtract it from the total.  The formula starts by counting everyone using the COUNT function.  The SUMPRODUCT function is then subtracted from that.  It gives the total of all the customers who became active after our End date plus the customers who became inactive before our Start date.</p>
<p>Note that this formula excludes the actual Start and End date.  Customer 17 isn&#8217;t included because we&#8217;re really looking at 7/16/2010 to 7/24/2010.  If you want the formula to be inclusive, simply change the &lt;= and >= to &lt; and >, respectively.</p>
<p>If you don&#8217;t like SUMPRODUCT, you can get there with COUNTIF</p>
<p></code></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">=COUNT(D2:D21)-(COUNTIF(C2:C21,&quot;&gt;=&quot;&amp;D25)+COUNTIF(D2:D21,&quot;&lt; =&quot;&amp;C25))</div></div>
<p>Sometimes it&#8217;s easier to turn the problem around and figure out who&#8217;s excluded.</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/09/01/count-active-customers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Dymo LabelWriter Part II</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/31/dymo-labelwriter-part-ii/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/31/dymo-labelwriter-part-ii/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 11:30:33 +0000</pubDate>
		<dc:creator>Dick Kusleika</dc:creator>
		
		<category><![CDATA[Automation]]></category>

		<category><![CDATA[Printing]]></category>

		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4141</guid>
		<description><![CDATA[A couple of weeks ago, I posted some code to print labels on a Dymo LabelWriter 450.  I wanted to post the finished code because it has a few more tricks in it.
Function PrintBoardFileLabel(ws As Worksheet) As Boolean

&#160; &#160; Dim bReturn As Boolean
&#160; &#160; Dim vaPrinters As Variant
&#160; &#160; Dim i As Long
&#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago, I <a href="http://www.dailydoseofexcel.com/archives/2010/08/10/printing-to-a-dymo-labelwriter-450-from-vba/">posted some code</a> to print labels on a Dymo LabelWriter 450.  I wanted to post the finished code because it has a few more tricks in it.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Function</span> PrintBoardFileLabel(ws <span style="color: #000080;">As</span> Worksheet) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> bReturn <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> vaPrinters <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Const</span> sLABELFILE <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;C:\BoardFile.label&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Const</span> sMSGNOFILE <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;Label file not found at &quot;</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Const</span> sMSGNODYMO <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;Dymo label printer not found.&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Const</span> sSOURCE <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;PrintBoardFileLabel()&quot;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> ErrorHandler<br />
&nbsp; &nbsp; bReturn = <span style="color: #000080;">True</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> Len(Dir(sLABELFILE)) &gt; 0 <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">If</span> mdyAddin <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Or</span> mdyLabel <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreateDymoObjects<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">If</span> <span style="color: #000080;">Not</span> mdyAddin <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Or</span> <span style="color: #000080;">Not</span> mdyLabel <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vaPrinters = Split(mdyAddin.GetDymoPrinters, <span style="color: #800000;">&quot;|&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">For</span> i = <span style="color: #000080;">LBound</span>(vaPrinters) <span style="color: #000080;">To</span> <span style="color: #000080;">UBound</span>(vaPrinters)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">If</span> mdyAddin.IsPrinterOnline(vaPrinters(i)) <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mdyAddin.SelectPrinter vaPrinters(i)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Exit</span> <span style="color: #000080;">For</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Next</span> i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mdyAddin.<span style="color: #000080;">Open</span> sLABELFILE<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mdyLabel.SetField <span style="color: #800000;">&quot;Text&quot;</span>, ws.Range(<span style="color: #800000;">&quot;rngComp1Serial&quot;</span>).Value &amp; <span style="color: #800000;">&quot; &quot;</span> &amp; ws.Range(<span style="color: #800000;">&quot;rngProdOrder&quot;</span>).Value &amp; _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vbNewLine &amp; StripItem(ws.Range(<span style="color: #800000;">&quot;rngCustomer&quot;</span>).Value) &amp; <span style="color: #800000;">&quot; &quot;</span> &amp; ws.Range(<span style="color: #800000;">&quot;rngPO&quot;</span>).Value<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mdyAddin.Print2 1, <span style="color: #000080;">True</span>, 1<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Err.Raise glHANDLED_ERROR, sSOURCE, sMSGNODYMO<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Err.Raise glHANDLED_ERROR, sSOURCE, sMSGNOFILE &amp; sLABELFILE<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
<br />
ErrorExit:<br />
&nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span><br />
&nbsp; &nbsp; PrintBoardFileLabel = bReturn<br />
&nbsp; &nbsp; <span style="color: #000080;">Exit</span> <span style="color: #000080;">Function</span><br />
<br />
ErrorHandler:<br />
&nbsp; &nbsp; bReturn = <span style="color: #000080;">False</span><br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> bCentralErrorHandler(msMODULE, sSOURCE) <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Stop</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Resume</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Resume</span> ErrorExit<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
<br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></div></div>
<p>The procedure is now a function that returns a Boolean because I use the error logging scheme described in <a href="http://www.amazon.com/exec/obidos/redirect?link_code=ur2&#038;tag=dailydoseofex-20&#038;camp=1789&#038;creative=9325&#038;path=http://www.amazon.com/gp/product/0321262506?v=glance%26n=283155%26s=books%26v=glance">PED<a />.  But if you ignore all that stuff, there are a couple of changes worth noting.</p>
<p>First, I made the Dymo objects module level variables by putting this at the top of the module</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Private</span> mdyAddin <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span><br />
<span style="color: #000080;">Private</span> mdyLabel <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span></div></div>
<p>and moved the creation of these variables into a separate procedure</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> CreateDymoObjects()<br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> mdyAddin = CreateObject(<span style="color: #800000;">&quot;Dymo.DymoAddin&quot;</span>)<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> mdyLabel = CreateObject(<span style="color: #800000;">&quot;Dymo.DymoLabels&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>In addition to converting these to late-bound (using CreateObject and the Object variable type instead of setting a reference) so that it works well on different PCs, I needed to keep these objects live through the whole session.  In the cases where multiple labels would be printed, I didn&#8217;t want to incur the overhead of creating and destroying the Dymo objects each time.  The module level variables stay in scope until the add-in is closed and I check in my code whether they exist yet.</p>
<p>Another change I made was to find the proper printer.  In my first iteration, I had one printer.  So I used the GetDymoPrinters method with impunity.  When I connected a second printer for testing, this no longer worked and I needed something more robust.  The GetDymoPrinters returns a pipe (|) delimited string.  This code</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">vaPrinters = Split(mdyAddin.GetDymoPrinters, <span style="color: #800000;">&quot;|&quot;</span>)<br />
<span style="color: #000080;">For</span> i = <span style="color: #000080;">LBound</span>(vaPrinters) <span style="color: #000080;">To</span> <span style="color: #000080;">UBound</span>(vaPrinters)<br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> mdyAddin.IsPrinterOnline(vaPrinters(i)) <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mdyAddin.SelectPrinter vaPrinters(i)<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Exit</span> <span style="color: #000080;">For</span><br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
<span style="color: #000080;">Next</span> i</div></div>
<p>splits the returned printer names into a Variant array.  I then loop through that array and check the IsPrinterOnline property.  When I find one that returns True, I use SelectPrinter to make it the &#8220;one&#8221; and exit the loop.</p>
<p>Almost all of the methods in the Dymo library return True or False indicating success or failure.  I should have, but haven&#8217;t, written code like this</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">If</span> mdyAddin.<span style="color: #000080;">Open</span>(sLABELFILE) <span style="color: #000080;">Then</span></div></div>
<p>That would prevent errors if someone moves or renames the label file.  Always build in some potential errors for job security (just kidding, don&#8217;t do that).  Someday when I have some more time, I&#8217;ll tighten up the code further.  But for now, it will have to do.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/31/dymo-labelwriter-part-ii/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Abbreviating Company Names</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/30/abbreviating-company-names/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/30/abbreviating-company-names/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 15:12:18 +0000</pubDate>
		<dc:creator>Dick Kusleika</dc:creator>
		
		<category><![CDATA[VBA Basics]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4138</guid>
		<description><![CDATA[Last week I was creating file folder labels with my new Dymo LaserWriter 450.  The information on the folder label is serial number, part number, company name, and purchase order.  Normally, this works great.  However, I ran into one on Friday with a 30 character part name and a 29 character company [...]]]></description>
			<content:encoded><![CDATA[<p>Last week I was creating file folder labels with my <a href="http://www.dailydoseofexcel.com/archives/2010/08/10/printing-to-a-dymo-labelwriter-450-from-vba/">new Dymo LaserWriter 450</a>.  The information on the folder label is serial number, part number, company name, and purchase order.  Normally, this works great.  However, I ran into one on Friday with a 30 character part name and a 29 character company name.  With shrink-to-fit, the font came out at about 4 points, surprisingly readable, but too small for these old eyes.</p>
<p>When we did this manually on our old label maker, we would abbreviate the company name and I wanted to replicate that in my code.  It&#8217;s easy to do for humans.  Less so for VBA.  I started by removing all of the lower-case vowels.</p>
<p>General Electric -> Gnrl Elctrc</p>
<p>Not bad.  Not, of course, how I would abbreviate it manually (GE), but not bad.  By only removing lower case, I preserved the first letter and any initials that would seem to be key to deciphering the name.  I still had names that were too long.  I was trying to get them to 15 characters or less, and this method only got about half of them to less than that.  Next, I removed Inc., LTD, Corporation, Corp and all that extraneous stuff that&#8217;s not critical to identifying the name. You have to be careful with case sensitivity or you can end up with </p>
<p>Principle Technologies, Inc. -> Prcpl Tchnlgs</p>
<p>because of the embedded &#8220;inc&#8221; in Principle.</p>
<p>Finally, I removed punctuation and got</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> RemoveVowels(sInput <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">String</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> vaVowels <span style="color: #000080;">As</span> <span style="color: #000080;">Variant</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> sReturn <span style="color: #000080;">As</span> <span style="color: #000080;">String</span><br />
<br />
&nbsp; &nbsp; sReturn = sInput<br />
&nbsp; &nbsp; vaVowels = Array(<span style="color: #800000;">&quot;Corporation&quot;</span>, <span style="color: #800000;">&quot;Corp&quot;</span>, <span style="color: #800000;">&quot;Co.&quot;</span>, <span style="color: #800000;">&quot; LTD&quot;</span>, <span style="color: #800000;">&quot;.&quot;</span>, <span style="color: #800000;">&quot;,&quot;</span>, <span style="color: #800000;">&quot;-&quot;</span>, <span style="color: #800000;">&quot;The &quot;</span>, <span style="color: #800000;">&quot; of&quot;</span>, <span style="color: #800000;">&quot; and&quot;</span>, <span style="color: #800000;">&quot; Inc&quot;</span>, <span style="color: #800000;">&quot;a&quot;</span>, <span style="color: #800000;">&quot;e&quot;</span>, <span style="color: #800000;">&quot;i&quot;</span>, <span style="color: #800000;">&quot;o&quot;</span>, <span style="color: #800000;">&quot;u&quot;</span>)<br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">For</span> i = <span style="color: #000080;">LBound</span>(vaVowels) <span style="color: #000080;">To</span> <span style="color: #000080;">UBound</span>(vaVowels)<br />
&nbsp; &nbsp; &nbsp; &nbsp; sReturn = Replace$(sReturn, vaVowels(i), <span style="color: #800000;">&quot;&quot;</span>, 1, , vbBinaryCompare)<br />
&nbsp; &nbsp; <span style="color: #000080;">Next</span> i<br />
<br />
&nbsp; &nbsp; sReturn = Replace$(sReturn, <span style="color: #800000;">&quot; &nbsp;&quot;</span>, <span style="color: #800000;">&quot; &quot;</span>)<br />
<br />
&nbsp; &nbsp; RemoveVowels = Trim(sReturn)<br />
<br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></div></div>
<p>If you implement something like this, you want to make sure that the longer terms come before the shorter ones.  If you remove Corp before you remove Corporation, you&#8217;ll end up with &#8220;oration&#8221;, which doesn&#8217;t make any sense.</p>
<p>In the end, and a little to my surprise, this didn&#8217;t work very well.  Looking at the transformed names, I was able to identify about 80% of them.  That&#8217;s not very good, in my opinion.  Then my co-worker suggested I use two lines on the file folder label.  I smacked my forehead and did just that, and of course, it works beautifully.  It was a fun exercise nonetheless.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/30/abbreviating-company-names/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Bushy Trees</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/27/bushy-trees/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/27/bushy-trees/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 05:37:06 +0000</pubDate>
		<dc:creator>Tushar Mehta</dc:creator>
		
		<category><![CDATA[CodeCritic]]></category>

		<category><![CDATA[VBA Basics]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4125</guid>
		<description><![CDATA[A favorite peeve of mine is code with &#8220;bushy trees.&#8221;  I first saw this phrase in Kernighan and Plauger&#8217;s Elements of Programming Style.
Recently, I saw some code that checked if a RefEdit control referred to a single cell that contained a non negative integer.  I cleaned up the formatting some since the original [...]]]></description>
			<content:encoded><![CDATA[<p>A favorite peeve of mine is code with &#8220;bushy trees.&#8221;  I first saw this phrase in <a href="http://www.amazon.com/Elements-Programming-Style-Brian-Kernighan/dp/0070342075/ref=sr_1_2?ie=UTF8&#038;s=books&#038;qid=1282970447&#038;sr=8-2">Kernighan and Plauger&#8217;s Elements of Programming Style</a>.</p>
<p>Recently, I saw some code that checked if a RefEdit control referred to a single cell that contained a non negative integer.  I cleaned up the formatting some since the original indentation style might be best described as &#8220;random tabs.&#8221;  But, code formatting is not the subject of this post.</p>
<p>The IFs are fairly easy to understand since they essentially follow how most people would think to validate the string parameter.  The tricky part comes in ensuring that one has the correct Else clause at the correct indentation level.  In this case, that too might not be too difficult since the Else clauses are fairly trivial, each consisting of a single boolean assignment.  But, imagine how much more difficult the task would be if there were further If clauses or loop structures in some of the Else clauses!</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Option Explicit<br />
Public Function getNonNegInt(aRefEditText As String, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; ByRef Rslt As Integer) As Boolean<br />
&nbsp; &nbsp; Dim aRng As Range<br />
&nbsp; &nbsp; Const MaxInt As Integer = 32767<br />
&nbsp; &nbsp; getNonNegInt = True<br />
&nbsp; &nbsp; On Error Resume Next<br />
&nbsp; &nbsp; Set aRng = Range(aRefEditText)<br />
&nbsp; &nbsp; On Error GoTo 0<br />
&nbsp; &nbsp; If Not aRng Is Nothing Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; If aRng.Cells.Count = 1 Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If IsNumeric(aRng.Value) Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CDbl(aRng.Value) &gt;= 0 Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CDbl(aRng.Value) = CLng(aRng.Value) Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CLng(aRng.Value) &lt; = MaxInt Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rslt = CInt(aRng.Value)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; End Function</div></div>
<p><strong>Code Sample 1</strong></p>
<p>As a first pass, one could remove all the boolean assignments by first setting the function value to false and not to true as in Code Sample 1.  Then only if we have an acceptable value do we return a True value.<br />
</code></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Option Explicit<br />
Public Function getNonNegInt(aRefEditText As String, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; ByRef Rslt As Integer) As Boolean<br />
&nbsp; &nbsp; Dim aRng As Range<br />
&nbsp; &nbsp; Const MaxInt As Integer = 32767<br />
&nbsp; &nbsp; On Error Resume Next<br />
&nbsp; &nbsp; Set aRng = Range(aRefEditText)<br />
&nbsp; &nbsp; On Error GoTo 0<br />
&nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; If Not aRng Is Nothing Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; If aRng.Cells.Count = 1 Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If IsNumeric(aRng.Value) Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CDbl(aRng.Value) &gt;= 0 Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CDbl(aRng.Value) = CLng(aRng.Value) Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CLng(aRng.Value) &lt; = MaxInt Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rslt = CInt(aRng.Value)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = True<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; End Function</div></div>
<p><strong>Code Sample 2</strong></p>
<p>However, this still doesn&#8217;t help with all the nested If and End If clauses.</p>
<p>So, how does one clean up this deeply nested code?  How about if we reverse the tests?  Instead of testing if the range is not nothing, test if it is nothing.  Instead of testing if the range contains 1 cell, test if it contains more than 1 cell.  And, so on.  The result as shown in Code Sample 3 is a single If statement (with multiple ElseIf clauses) that is &#8216;flat&#8217; &#8212; with no confusing nesting!</p>
<p></code></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Option Explicit<br />
<br />
Public Function getNonNegInt(aRefEditText As String, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; ByRef Rslt As Integer) As Boolean<br />
&nbsp; &nbsp; Dim aRng As Range<br />
&nbsp; &nbsp; Const MaxInt As Integer = 32767<br />
&nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; On Error Resume Next<br />
&nbsp; &nbsp; Set aRng = Range(aRefEditText)<br />
&nbsp; &nbsp; On Error GoTo 0<br />
&nbsp; &nbsp; If aRng Is Nothing Then<br />
&nbsp; &nbsp; ElseIf aRng.Cells.Count &gt; 1 Then<br />
&nbsp; &nbsp; ElseIf Not IsNumeric(aRng.Value) Then<br />
&nbsp; &nbsp; ElseIf CDbl(aRng.Value) &lt; 0 Then<br />
&nbsp; &nbsp; ElseIf CDbl(aRng.Value) &lt;&gt; CLng(aRng.Value) Then<br />
&nbsp; &nbsp; ElseIf CLng(aRng.Value) &gt; MaxInt Then<br />
&nbsp; &nbsp; Else<br />
&nbsp; &nbsp; &nbsp; &nbsp; Rslt = CInt(aRng.Value)<br />
&nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = True<br />
&nbsp; &nbsp; &nbsp; &nbsp; End If<br />
&nbsp; &nbsp; End Function</div></div>
<p><strong>Code Sample 3</strong></p>
<p>The code above uses a very powerful concept &#8212; that of a &#8216;null clause.&#8217;</p>
<p>In most cases, when we have a If&#8230;Then, we have some statement in the True branch of the If statement.  It might be a series of assignments or it might be another If or a loop of some sort but there is something in the True branch.  For example, in Code Sample 1 and Code Sample 2 above, there are two assignments.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; If CLng(aRng.Value) &lt; = MaxInt Then<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rslt = CInt(aRng.Value)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getNonNegInt = True<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;End If</div></div>
<p>However, in Code Sample 3, each Then is followed not by a statement but by the ElseIf clause.  This results in a null statement (or empty block) in the True branch.  This is perfectly legal in every programming language I&#8217;ve used and in this case it serves a very powerful role in simplifying the code.</p>
<p>For the sake of completeness, we will look at one more way of coding the above.  In this particular scenario since there is no further processing after the string is validated, one could use use a series of Ifs that simply go to an exit point for bad data.  But, this would not work for scenarios in which we wanted to do additional processing after the string of Ifs.<br />
</code></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Option Explicit<br />
Public Function getNonNegInt(aRefEditText As String, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; ByRef Rslt As Integer) As Boolean<br />
&nbsp; &nbsp; Dim aRng As Range<br />
&nbsp; &nbsp; Const MaxInt As Integer = 32767<br />
&nbsp; &nbsp; On Error Resume Next<br />
&nbsp; &nbsp; Set aRng = Range(aRefEditText)<br />
&nbsp; &nbsp; On Error GoTo 0<br />
&nbsp; &nbsp; If aRng Is Nothing Then GoTo ErrXIT<br />
&nbsp; &nbsp; If aRng.Cells.Count &gt; 1 Then GoTo ErrXIT<br />
&nbsp; &nbsp; If Not IsNumeric(aRng.Value) Then GoTo ErrXIT<br />
&nbsp; &nbsp; If CDbl(aRng.Value) &lt; 0 Then GoTo ErrXIT<br />
&nbsp; &nbsp; If CDbl(aRng.Value) &lt;&gt; CLng(aRng.Value) Then GoTo ErrXIT<br />
&nbsp; &nbsp; If CLng(aRng.Value) &gt; MaxInt Then GoTo ErrXIT<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Rslt = CInt(aRng.Value)<br />
&nbsp; &nbsp; getNonNegInt = True<br />
&nbsp; &nbsp; Exit Function<br />
ErrXIT:<br />
&nbsp; &nbsp; getNonNegInt = False<br />
&nbsp; &nbsp; End Function</div></div>
<p><strong>Code Sample 4</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/27/bushy-trees/feed/</wfw:commentRss>
		</item>
		<item>
		<title>A Hundred Thousand Name Managers!</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/19/a-hundred-thousand-name-managers/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/19/a-hundred-thousand-name-managers/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 07:55:15 +0000</pubDate>
		<dc:creator>jkpieterse</dc:creator>
		
		<category><![CDATA[Add-ins]]></category>

		<category><![CDATA[Downloads]]></category>

		<category><![CDATA[MVP]]></category>

		<category><![CDATA[Names]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4119</guid>
		<description><![CDATA[Hi folks,
Many of the regulars here probably know the Name Manager utility, which Charles Williams and I created and give away for free on our websites.
Rumour has it this is one of best tools ever built for the Excel developer. I won&#8217;t argue with that!
Anyway, as I was looking at my web stats today I [...]]]></description>
			<content:encoded><![CDATA[<p>Hi folks,</p>
<p>Many of the regulars here probably know the <a href="http://www.jkp-ads.com/officemarketplacenm-en.asp">Name Manager utility</a>, which <a href="http://www.decisionmodels.com">Charles Williams</a> and I created and give away for free on our websites.</p>
<p>Rumour has it this is one of best tools ever built for the Excel developer. I won&#8217;t argue with that!</p>
<p>Anyway, as I was looking at my web stats today I discovered a nice feat: We&#8217;ve just passed the 100,000 download count on the tool (this excludes the downloads from Charles&#8217; site, so we can safely assume the true number is at least 50% more than that). Time for a celebration:</p>
<p>Hurray!</p>
<p>Regards,</p>
<p>Jan Karel Pieterse<br />
<a href="http://www.jkp-ads.com">www.jkp-ads.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/19/a-hundred-thousand-name-managers/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Showing Hidden Sheet and Workbooks Dialog in VBA</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/12/showing-hidden-sheet-and-workbooks-dialog-in-vba/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/12/showing-hidden-sheet-and-workbooks-dialog-in-vba/#comments</comments>
		<pubDate>Thu, 12 Aug 2010 11:30:54 +0000</pubDate>
		<dc:creator>Dick Kusleika</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4116</guid>
		<description><![CDATA[A tip from Scott:
Windows - Unhide, from the menu, shows the Unhide dialog box for unhiding workbooks.

In vba, Application.Dialogs(xlDialogUnhide).Show gets the job done.
Where it gets tricky is unhiding worksheets.  Via the menu, Format - Sheets - Unhide

In vba, the name of the dialog is not so intuitive.
Application.Dialogs(xlDialogWorkbookUnhide).Show
Poorly named, for sure, but you&#8217;ve been warned. [...]]]></description>
			<content:encoded><![CDATA[<p>A tip from Scott:</p>
<p>Windows - Unhide, from the menu, shows the Unhide dialog box for unhiding workbooks.</p>
<p><img src="http://www.dailydoseofexcel.com/blogpix/unhidedialog1.gif" height="203" width="311" alt="" /></p>
<p>In vba, <code class="codecolorer vb default"><span class="vb">Application.Dialogs(xlDialogUnhide).Show</span></code> gets the job done.</p>
<p>Where it gets tricky is unhiding worksheets.  Via the menu, Format - Sheets - Unhide</p>
<p><img src="http://www.dailydoseofexcel.com/blogpix/unhidedialog2.gif" height="204" width="311" alt="" /></p>
<p>In vba, the name of the dialog is not so intuitive.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Application.Dialogs(xlDialogWorkbookUnhide).Show</div></div>
<p>Poorly named, for sure, but you&#8217;ve been warned.  Thanks Scott.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/12/showing-hidden-sheet-and-workbooks-dialog-in-vba/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Printing to a DYMO LabelWriter 450 from VBA</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/10/printing-to-a-dymo-labelwriter-450-from-vba/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/10/printing-to-a-dymo-labelwriter-450-from-vba/#comments</comments>
		<pubDate>Tue, 10 Aug 2010 11:30:50 +0000</pubDate>
		<dc:creator>Dick Kusleika</dc:creator>
		
		<category><![CDATA[Automation]]></category>

		<category><![CDATA[Printing]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4097</guid>
		<description><![CDATA[I recently had to make some file folders at work.  About five minutes after I was done, I purchased a DYMO LabelWriter 450.  I was using one of those label makers where you punch in the text, hit print, and press down on a lever to cut the label.  Then you have [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to make some file folders at work.  About five minutes after I was done, I purchased a DYMO LabelWriter 450.  I was using one of those label makers where you punch in the text, hit print, and press down on a lever to cut the label.  Then you have to take a pair of scissors and cut along the dotted line to get the right length.  THEN you have to spend ten minutes fumbling with the backing.  Brutal.</p>
<p>When I installed the software, it installed quite a few libraries.  I wasn&#8217;t sure which one to pick, but after a little experimenting, I chose the Dymo Label Software v.8 SDK.</p>
<p><img src="http://www.dailydoseofexcel.com/blogpix/dymolabelwriter1.gif" height="364" width="449" alt="" /></p>
<p>With my reference set, I tried a few different objects without much success.  If there&#8217;s documentation for this, I haven&#8217;t found it.  As usual, the object model leaves a lot to be desired.  Nevertheless, I persevered and came up with this:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Sub</span> TestLabel()<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> myDymo <span style="color: #000080;">As</span> DYMO_DLS_SDK.DymoHighLevelSDK<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> dyAddin <span style="color: #000080;">As</span> DYMO_DLS_SDK.ISDKDymoAddin<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> dyLabel <span style="color: #000080;">As</span> DYMO_DLS_SDK.ISDKDymoLabels<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> myDymo = <span style="color: #000080;">New</span> DYMO_DLS_SDK.DymoHighLevelSDK<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> dyAddin = myDymo.DymoAddin<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> dyLabel = myDymo.DymoLabels<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; dyAddin.SelectPrinter dyAddin.GetDymoPrinters<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; dyAddin.<span style="color: #000080;">Open</span> Environ$(<span style="color: #800000;">&quot;USERPROFILE&quot;</span>) &amp; <span style="color: #800000;">&quot;\My Documents\DYMO Label\Labels\BoardFile.label&quot;</span><br />
&nbsp; &nbsp; dyLabel.SetField <span style="color: #800000;">&quot;Text&quot;</span>, <span style="color: #800000;">&quot;My text goes here&quot;</span><br />
&nbsp; &nbsp; dyAddin.Print2 1, <span style="color: #000080;">True</span>, 1<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> myDymo = <span style="color: #000080;">Nothing</span><br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>Let&#8217;s step through this and try to see what&#8217;s happening.  I start with a DymoHighLevelSDK object - what an awful name.  This object has two properties: DymoAddin (what an awful name) and DymoLabels.  From what I can tell, DymoLabels is only one label, from which I can only conclude&#8230;wait for it&#8230;they gave it an awful name.  I needed to create both the DymoAddin object and the DymoLabels object.  I would have thought that I could get the current label from the DymoAddin object, but that doesn&#8217;t appear to be the case.</p>
<p>I set the printer I want to use using SelectPrinter and GetDymoPrinters.  I guess if you have more than one Dymo printer, GetDymoPrinters returns some delimited string.  But I only have one, so I don&#8217;t know.  Either way, it&#8217;s stupid.  I discovered that GetDymoPrinters returns my one printer, so I pass that to SelectPrinter and that seems to have worked.</p>
<p>Next I open my .label template file.  There&#8217;s also an Open2 method, but I don&#8217;t know the difference.  Am I done bashing the DYMO programmers yet?  Not even close.  If you have two Open methods, don&#8217;t freaking name them Open and Open2.  Name them something understandable like Open and OpenPriorVersion.</p>
<p>I tried <code class="codecolorer vb default"><span class="vb"><span style="color: #000080;">Set</span> dyLabel = dyAddin.<span style="color: #000080;">Open</span>(etc...)</span></code> but got a Type Mismatch error.  As far as I can tell, when I call the Open method to the DymoAddin object, the DymoLabels object automatically becomes whatever was open.  Idiotic.</p>
<p>My label has one object on it called &#8220;Text&#8221;, so the SetField method was pretty straight forward.  It reminds me of Quickbooks in that there are few, if any, properties and everything is a method.</p>
<p>Finally I tried the Print method, but was rewarded with &#8220;Object doesn&#8217;t support this property or method.&#8221;  One of my favorite errors.  Out of desparation, I chose the Print2 method.  I had to include a third argument for PaperTray, which is utterly ludicrous if you look at the LabelWriter 450.  But it worked.</p>
<p>I wanted to set the ShrinkToFit property of the Textbox to TRUE, but I couldn&#8217;t figure out how to do it.  It did it automatically because I set that &#8220;property&#8221; when I created the label template, but it would be nice to be able to set it in code.</p>
<p>If I had written the object model, my code would have looked like this:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Sub</span> TestLabelNonJerkyWay()<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> myDymo <span style="color: #000080;">As</span> Dymo.Application<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> dyLabel <span style="color: #000080;">As</span> Dymo.DymoLabel<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> myDymo = <span style="color: #000080;">New</span> Dymo.Application<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> dyLabel = myDymo.<span style="color: #000080;">Open</span>(<span style="color: #800000;">&quot;FilePathandName&quot;</span>)<br />
&nbsp; &nbsp; myDymo.ActivePrinter = <span style="color: #800000;">&quot;Dymo 450&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">With</span> dyLabel.Fields(<span style="color: #800000;">&quot;Text&quot;</span>)<br />
&nbsp; &nbsp; &nbsp; &nbsp; .ShrinkToFit = <span style="color: #000080;">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .Text = <span style="color: #800000;">&quot;My text goes here&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">With</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; dyLabel.<span style="color: #000080;">Print</span> 1<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; myDymo.<span style="color: #000080;">Close</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> myDymo = <span style="color: #000080;">Nothing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>Anyway, this post should get a million hits.  Or maybe it will just get 100% of the hits from the 12 people who care about this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/10/printing-to-a-dymo-labelwriter-450-from-vba/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Contextual PivotTable Userform</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/09/contextual-pivottable-userform/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/09/contextual-pivottable-userform/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 11:30:27 +0000</pubDate>
		<dc:creator>Dick Kusleika</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4094</guid>
		<description><![CDATA[Sometimes when I write code, I feel as if I&#8217;m writing on a cloud with a unicorn&#8217;s horn dipped in angel tears.  And sometimes I feel as if I&#8217;m using a sledge hammer.  This is the latter.
There have been some PivotTable shortcuts posted about (here, here, and here).  When I get too [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when I write code, I feel as if I&#8217;m writing on a cloud with a unicorn&#8217;s horn dipped in angel tears.  And sometimes I feel as if I&#8217;m using a sledge hammer.  This is the latter.</p>
<p>There have been some PivotTable shortcuts posted about (<a href="http://www.dailydoseofexcel.com/archives/2010/08/03/toggle-pivotfields-from-count-to-sum/">here</a>, <a href="http://www.dailydoseofexcel.com/archives/2010/06/18/formatting-pivot-tables/">here</a>, and <a href="http://datapigtechnologies.com/blog/index.php/auto-format-pivottables-to-match-source-data/">here</a>).  When I get too many shortcuts, I&#8217;m inclined to put them on a userform (like I did with <a href="http://www.dailydoseofexcel.com/archives/2009/08/18/formatting-taskpane/">cell formatting</a>).  To that end, I want a userform to appear whenever the Pivot Table Field List task pane appears, that is, whenever the activecell is within the boundaries of a PivotTable.</p>
<p>I start with a class module to capture three events.  I created a class called CAppEvents that looks like this:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Public</span> <span style="color: #000080;">WithEvents</span> appEvents <span style="color: #000080;">As</span> Application<br />
<br />
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> appEvents_SheetActivate(<span style="color: #000080;">ByVal</span> Sh <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>)<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; ShowHideForm Sh, ActiveCell<br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<br />
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> appEvents_SheetSelectionChange(<span style="color: #000080;">ByVal</span> Sh <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>, <span style="color: #000080;">ByVal</span> Target <span style="color: #000080;">As</span> Range)<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; ShowHideForm Sh, Target<br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<br />
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> appEvents_WindowActivate(<span style="color: #000080;">ByVal</span> Wb <span style="color: #000080;">As</span> Workbook, <span style="color: #000080;">ByVal</span> Wn <span style="color: #000080;">As</span> Window)<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; ShowHideForm ActiveSheet, ActiveCell<br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<br />
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> ShowHideForm(Sh <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>, Target <span style="color: #000080;">As</span> Range)<br />
<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> pt <span style="color: #000080;">As</span> PivotTable<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">'Start by hiding the userform<br />
</span> &nbsp; &nbsp;<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; gufPivotTable.Hide<br />
&nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> 0<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #008000;">'if the cell is within a pivot table on the sheet, show the userform<br />
</span> &nbsp; &nbsp;<span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> pt <span style="color: #000080;">In</span> Sh.PivotTables<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">If</span> <span style="color: #000080;">Not</span> Intersect(Target, pt.TableRange2) <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Unload gufPivotTable<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Set</span> gufPivotTable = <span style="color: #000080;">Nothing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Set</span> gufPivotTable = <span style="color: #000080;">New</span> UPivotTable<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> 0<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gufPivotTable.Left = pt.TableRange2.Left + pt.TableRange2.Width + 50<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gufPivotTable.Top = pt.TableRange2.Top + 50<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gufPivotTable.Show<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Exit</span> <span style="color: #000080;">For</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Next</span> pt<br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>When the cell selection is changed, a different worksheet is selected, or a different workbook is activated, the ShowHideForm procedure is called.  It looks at all the PivotTables on the sheet and sees if the ActiveCell is inside one of them.  If it is, it shows a userform.</p>
<p>Globally scoped userforms can be tricky. Just because you close a userform, doesn&#8217;t mean that global variable is Nothing and you get an automation error.  Now for the sledge hammer part: I hide the userform, destroy it, and recreate it every time.  It avoids the error, but isn&#8217;t exactly pretty.  I need to work on retaining its position too, as now it simply shows it in the same spot.</p>
<p>The userform has two special properties set.  I set ShowModal to False so that code execution continues and so that the user can work with the PivotTable while the userform is visible.  And since I have multiple monitors, I set StartUpPosition to 3 - Windows Default.  With a name like &#8220;Windows Default&#8221;, you&#8217;d think it would be the default in Excel, but it&#8217;s not so.</p>
<p>To make the magic happen, I have a module called MEntryPoints that holds the global userform variable, the global class variable, and the code to set it all up.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Public</span> gclsAppEvents <span style="color: #000080;">As</span> CAppEvents<br />
<span style="color: #000080;">Public</span> gufPivotTable <span style="color: #000080;">As</span> UPivotTable<br />
<br />
<span style="color: #000080;">Sub</span> Auto_Open()<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> gclsAppEvents = <span style="color: #000080;">New</span> CAppEvents<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> gclsAppEvents.appEvents = Application<br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<br />
<span style="color: #000080;">Sub</span> Auto_Close()<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> gclsAppEvents = <span style="color: #000080;">Nothing</span><br />
&nbsp; &nbsp; <br />
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>It seems to work reasonably well.  I&#8217;m sure I&#8217;m missing an event that has to do with the ActiveWindow, but I&#8217;m not too worried about that.  I really detest using the SelectionChange event.  It&#8217;s constantly firing even when I&#8217;m not using a PivotTable.  When I find myself using SelectChange, I usually abandon the code and trigger it manually, which I may end up doing here.</p>
<p>Then there&#8217;s the issue of what to put on the userform.  I initially want three things.  I want some pivot field formatting options.  I want to toggle between Sum and Count and maybe a couple of others.  I want to group by Years and Months and Years, as those are the two I group by the most.  Anything else?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/09/contextual-pivottable-userform/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Regular chart version of a sparkline</title>
		<link>http://www.dailydoseofexcel.com/archives/2010/08/08/regular-chart-version-of-a-sparkline/</link>
		<comments>http://www.dailydoseofexcel.com/archives/2010/08/08/regular-chart-version-of-a-sparkline/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 05:58:06 +0000</pubDate>
		<dc:creator>Tushar Mehta</dc:creator>
		
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=4101</guid>
		<description><![CDATA[In one of the social.answers.microsoft.com web forums (Microsoft&#8217;s replacement for the newsgroups no longer hosted on its own server) someone wanted to see a regular chart version of a sparkline when s/he clicked on the cell.  I thought that was an interesting idea since one can see much more detail in a large object [...]]]></description>
			<content:encoded><![CDATA[<p>In one of the social.answers.microsoft.com web forums (Microsoft&#8217;s replacement for the newsgroups no longer hosted on its own server) someone wanted to see a regular chart version of a sparkline when s/he clicked on the cell.  I thought that was an interesting idea since one can see much more detail in a large object than a tiny sparkline.  Here&#8217;s my take on it.  The version below improves on what I posted in social.answers by correctly handling cases where the source of a sparkline is another sheet / book and by hiding the chart if the selected range contains multiple cells or if the selected cell does not contain a sparkline.  This version is also more modularized.</p>
<p><img src="http://www.dailydoseofexcel.com/blogpix/bigsis1-300x203.png" alt="bigsis1" title="bigsis1" width="300" height="203" class="aligncenter size-medium wp-image-4104" /></p>
<p>In the code module of the workbook that contains the sparklines:</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span><br />
<br />
<span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Workbook_SheetSelectionChange(<span style="color: #000080;">ByVal</span> Sh <span style="color: #000080;">As</span> <span style="color: #000080;">Object</span>, <span style="color: #000080;">ByVal</span> Target <span style="color: #000080;">As</span> Range)<br />
&nbsp; &nbsp; AutoBigChart Target<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
<p>Then, the code below goes in a regular module.</p>
<p>The function getBigChart returns a reference to an existing chart that was previously created by the code or creates a new one, if necessary.  If it finds an existing chartobject named SparklineBigSis it does not adjust any format or size attribute.  This allows the user to customize the chart once it is created.</p>
<p>The subroutine setChartType selects a chart type based on the type of sparkline in the selected cell.</p>
<p>The setChartSource routine adds a series to the chart and sets the series&#8217; source to that of the sparkline.  It also accounts for the different formats in which a sparkline returns its SourceData &#8212; if the data are in the same sheet as the sparkline there is no sheet information. </p>
<p>The AutoBigChart routine then essentially just calls each of the support routines as needed.</p>
<div class="codecolorer-container vb default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;"><div class="vb codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span><br />
<br />
<span style="color: #000080;">Const</span> BigSisRatio <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = 5<br />
&nbsp; &nbsp; <span style="color: #008000;">'big chart is this much larger than cell size<br />
</span><span style="color: #000080;">Const</span> BigSisName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;SparklineBigSis&quot;</span><br />
<span style="color: #000080;">Function</span> getBigChart(aCell <span style="color: #000080;">As</span> Range) <span style="color: #000080;">As</span> ChartObject<br />
&nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> aChartObj <span style="color: #000080;">As</span> ChartObject<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> aChartObj = aCell.Parent.ChartObjects(BigSisName)<br />
&nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> 0<br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> aChartObj <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Set</span> aChartObj = aCell.Parent.ChartObjects.Add( _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aCell.Left + aCell.Width, aCell.Top, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aCell.Width * BigSisRatio, aCell.Height * BigSisRatio)<br />
&nbsp; &nbsp; &nbsp; &nbsp; aChartObj.Name = BigSisName<br />
&nbsp; &nbsp; <span style="color: #000080;">Else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">With</span> aChartObj<br />
&nbsp; &nbsp; &nbsp; &nbsp; .Left = aCell.Left + aCell.Width<br />
&nbsp; &nbsp; &nbsp; &nbsp; .Top = aCell.Top<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">With</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">If</span><br />
&nbsp; &nbsp; aChartObj.Visible = <span style="color: #000080;">True</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> getBigChart = aChartObj<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Function</span><br />
<span style="color: #000080;">Sub</span> deleteAllSeries(aChart <span style="color: #000080;">As</span> Chart)<br />
&nbsp; &nbsp; <span style="color: #000080;">Do</span> <span style="color: #000080;">While</span> aChart.SeriesCollection.Count &gt; 0<br />
&nbsp; &nbsp; &nbsp; &nbsp; aChart.SeriesCollection(1).Delete<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Loop</span><br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<span style="color: #000080;">Sub</span> setChartType(aSparkline <span style="color: #000080;">As</span> SparklineGroup, aChart <span style="color: #000080;">As</span> Chart)<br />
&nbsp; &nbsp; <span style="color: #000080;">Select</span> <span style="color: #000080;">Case</span> aSparkline.<span style="color: #000080;">Type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Case</span> xlsparkline:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aChart.ChartType = xlLine<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Case</span> xlSparkColumn:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aChart.ChartType = xlColumnClustered<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Case</span> xlSparkColumnStacked100:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aChart.ChartType = xlColumnStacked100<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">Case</span> <span style="color: #000080;">Else</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MsgBox <span style="color: #800000;">&quot;Unknown type of sparkline chart (=&quot;</span> _<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp; aSparkline.<span style="color: #000080;">Type</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Select</span><br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<span style="color: #000080;">Sub</span> hideChartObj(aRng <span style="color: #000080;">As</span> Range)<br />
&nbsp; &nbsp; <span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span><br />
&nbsp; &nbsp; aRng.Parent.ChartObjects(BigSisName).Visible = <span style="color: #000080;">False</span><br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<span style="color: #000080;">Sub</span> setChartSource(<span style="color: #000080;">ByRef</span> aChart <span style="color: #000080;">As</span> Chart, _<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">ByVal</span> aSparkGroup <span style="color: #000080;">As</span> SparklineGroup)<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> aRng <span style="color: #000080;">As</span> Range<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> aRng = Range(aSparkGroup.SourceData)<br />
&nbsp; &nbsp; aChart.SeriesCollection.Add <span style="color: #800000;">&quot;=&quot;</span> &amp; aRng.Address(<span style="color: #000080;">True</span>, <span style="color: #000080;">True</span>, xlA1, <span style="color: #000080;">True</span>)<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<span style="color: #000080;">Sub</span> AutoBigChart(aRng <span style="color: #000080;">As</span> Range)<br />
&nbsp; &nbsp; <span style="color: #000080;">With</span> aRng<br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> .Cells.Count &lt;&gt; 1 <span style="color: #000080;">Then</span> <span style="color: #000080;">GoTo</span> XIT<br />
&nbsp; &nbsp; <span style="color: #000080;">If</span> .SparklineGroups.Count = 0 <span style="color: #000080;">Then</span> <span style="color: #000080;">GoTo</span> XIT<br />
&nbsp; &nbsp; <span style="color: #000080;">Dim</span> aChartObj <span style="color: #000080;">As</span> ChartObject<br />
&nbsp; &nbsp; <span style="color: #000080;">Set</span> aChartObj = getBigChart(.Cells(1))<br />
&nbsp; &nbsp; deleteAllSeries aChartObj.Chart<br />
&nbsp; &nbsp; setChartSource aChartObj.Chart, .SparklineGroups(1)<br />
&nbsp; &nbsp; setChartType .SparklineGroups(1), aChartObj.Chart<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">With</span><br />
&nbsp; &nbsp; <span style="color: #000080;">Exit</span> <span style="color: #000080;">Sub</span><br />
XIT:<br />
&nbsp; &nbsp; hideChartObj aRng<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span><br />
<span style="color: #000080;">Sub</span> makeBigChart()<br />
&nbsp; &nbsp; AutoBigChart ActiveCell<br />
&nbsp; &nbsp; <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.dailydoseofexcel.com/archives/2010/08/08/regular-chart-version-of-a-sparkline/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
