<?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>output stream &#187; VBA</title>
	<atom:link href="http://duncanandmeg.org/blogs/code/category/vba/feed/" rel="self" type="application/rss+xml" />
	<link>http://duncanandmeg.org/blogs/code</link>
	<description>riotous events in amateur development</description>
	<lastBuildDate>Fri, 18 Feb 2011 21:28:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Clean pastes with Excel VBA</title>
		<link>http://duncanandmeg.org/blogs/code/2007/12/14/clean-pastes-with-excel-vba/</link>
		<comments>http://duncanandmeg.org/blogs/code/2007/12/14/clean-pastes-with-excel-vba/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 16:50:31 +0000</pubDate>
		<dc:creator>dtjohnso</dc:creator>
				<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://duncanandmeg.org/blogs/code/2007/12/14/clean-pastes-with-excel-vba/</guid>
		<description><![CDATA[I&#8217;ve talked about some of the Excel VBA I&#8217;ve done before, and here&#8217;s a new thing I learned today. Problem One part of my application selects a range from one worksheet in the spreadsheet and pastes it onto another. Originally, the code I used looked like this: destinationWorksheet.Range("A2").Select ActiveSheet.Paste&#60;/pre&#62; There is one problem I was [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve talked about <a href="/blogs/code/2007/09/13/fixing-memory-leaks-in-excel-vba-we-hope/" class="liinternal">some of the Excel VBA I&#8217;ve done</a> before, and here&#8217;s a new thing I learned today.<br />
<h3>Problem</h3>
<p>One part of my application selects a range from one worksheet in the spreadsheet and pastes it onto another. Originally, the code I used looked like this:</p>
<pre><code class=\'prettyprint\'  class="prettyprint">destinationWorksheet.Range("A2").Select
ActiveSheet.Paste&lt;/pre&gt;</code>

There is one problem I was unaware of. Every time the .Paste operation executed, it added a new "Named Range" to the ActiveSheet. I only realized this when snooping around in the Properties for the Excel file, where I found (to much dismay) 840 different "Named Ranges" referring to data long since thrown away! An older version of my application, which used this unspecified Paste operation much more frequently, had even more of these attached to numbers of other worksheets.<span id="more-23"></span>
<h3>Solution</h3>

The first thing I looked into was the properties of the .Paste method itself. Turns out there are two of them, "Destination" and "Link," and they are more or less mutually exclusive. The Excel VBA Help provides good explanation of that which I won't reproduce here.

The reason my old call for an unparameterized .Paste worked was that it simply used the current selection as the "Destination" property. This is explained from this portion of the Help document:
<blockquote>
<strong><em>Destination</em></strong> Optional <strong>Variant</strong>. ... If this argument is omitted, the current selection is used.</blockquote>

I had assumed that this parameter was irrelevant. The increase in these weird "Named Ranges" made me suspicious.

I modified my .Paste call this way to see if it would make a difference:
<pre><code class=\'prettyprint\'  class="prettyprint">ActiveSheet.Paste Destination:=ActiveCell</code></pre>
<p>Adding a parameter that specifies the default parameter anyway isn't a pretty fix, would it work? After executing a few calls to the .Paste statement in question, I was shocked! It <em>did</em> make a difference! No more "Named Ranges" were being appended to the ActiveSheet.<br />
<h3>Cleanup</h3>
<p>Wonderful, now I had 840 "Named Ranges" stuck in my file for no reason, referring to data long since wiped off the sheet. Some quick Help file searching taught me some things.</p>
<p>Each Worksheet object in a Workbook contains a collection of Name objects called Names. The Name objects in this collection are the "Named Ranges" in question. With some quick 'n dirty coding, I wiped them all out. Solution is below:</p>
<pre><code class=\'prettyprint\'  class="prettyprint">Private Sub NameDR4ng3z()
    ThisWorkbook.Worksheets("destinationWorksheetName").Activate

    For Each myName In ActiveSheet.Names 'list '
        Debug.Print myName.Name
    Next myName

    For Each myName In ActiveSheet.Names 'delete '
        myName.Delete
    Next myName

    Debug.Print "Names deleted? We'll see"
    For Each myName In ActiveSheet.Names 'double-check '
        Debug.Print myName.Name
    Next myName
End Sub</code></pre>
<p>This worked. I'm happy.</p>
]]></content:encoded>
			<wfw:commentRss>http://duncanandmeg.org/blogs/code/2007/12/14/clean-pastes-with-excel-vba/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing memory leaks in Excel VBA&#8230; (we hope)</title>
		<link>http://duncanandmeg.org/blogs/code/2007/09/13/fixing-memory-leaks-in-excel-vba-we-hope/</link>
		<comments>http://duncanandmeg.org/blogs/code/2007/09/13/fixing-memory-leaks-in-excel-vba-we-hope/#comments</comments>
		<pubDate>Fri, 14 Sep 2007 02:05:52 +0000</pubDate>
		<dc:creator>dtjohnso</dc:creator>
				<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://duncanandmeg.org/blogs/code/2007/09/13/fixing-memory-leaks-in-excel-vba-we-hope/</guid>
		<description><![CDATA[One project I constantly return to is an application I built in Excel VBA that does a lot of different things. One specific problem remains unsolved with this application, so I&#8217;ve decided to throw it out here and see if any gurus have an answer. Basically, as a result of some Excel memory leaks beyond [...]]]></description>
			<content:encoded><![CDATA[<p>One project I constantly return to is an application I built in Excel VBA that does a lot of different things. One specific problem remains unsolved with this application, so I&#8217;ve decided to throw it out here and see if any gurus have an answer.</p>
<p>Basically, as a result of some Excel memory leaks beyond my control (I can&#8217;t install recommended hotfixes on the computers where this application is used, and need to be able to handle the issue with/without hotfixes anyway), when I shut down Excel it typically remains stuck in memory (and visible under &#8220;Processes&#8221; in &#8220;Task Manager&#8221;) even though it looks like it is gone to the user.<br />
<h3>Paranoia</h3>
<p>I realize that many would argue that since this bug is Microsoft&#8217;s problem and not mine, that I shouldn&#8217;t trouble with addressing it. However, since this application I&#8217;ve built is something that gets used frequently (but not constantly) throughout the day, I can imagine this becoming a problem (or at least wasting resources) at some point.</p>
<p>Another rejoinder to this charge is quite simple: I&#8217;ve actually ignored this issue for about a year now already, and am only revisiting it because I&#8217;ve learned some things about VBA that enable me to possibly defeat it. If I didn&#8217;t have any hope of fixing this, I wouldn&#8217;t try.<span id="more-7"></span><br />
<h3>Problem (in more depth)</h3>
<p>Here is what I know about why one of my memory leak problems exists (this article won&#8217;t deal with all my memory leak issues in the application, since I still haven&#8217;t made any headway on the others). I may not have quite the right diagnosis here, but tests to this point suggest I might be on the right track.</p>
<p>The main worksheet in my application includes numerous embedded control buttons that marshall up various cool features I&#8217;ve built for this VBA app. Unfortunately, it appears that <a href="http://support.microsoft.com/default.aspx?scid=kb;en-us;238570" class="liexternal">embedding any sort of control (as far as I can tell) on a worksheet can cause Excel to commit memory leaks</a>.</p>
<p>Before going further, a few notes about the KB article I just mentioned:</p>
<ol>
<li>This article refers to an issue with Excel 97.</li>
<li>I&#8217;m not sure that worksheet buttons are considered ActiveX or OCX controls</li>
</ol>
<p>The problem here is simple: I did some research on this a year ago when I first discovered the issue, and didn&#8217;t keep track of the KB article I found that suggested the problem with embedded buttons. I remember reading something like that then, but can&#8217;t find it now. The only reason I think this might actually be the source of the problem is because my hacked solution seems to prevent the memory leak in question.<br />
<h3>Ugly solution</h3>
<p>I learned that it is possible in Excel VBA to <a href="http://support.microsoft.com/kb/141563" class="liexternal">write an Auto_Close sub to override default behavior when closing a spreadsheet</a>. <strike>(If I ever track down the documentation that tipped me off to this, I&#8217;ll link to it here.)</strike></p>
<p>This suggested that I might be able to do something on the file&#8217;s close event that might at least catch and destroy the source of the memory leak before the Excel app slipped out of my control until an inevitable Ctrl+Alt+Del. Here&#8217;s the ugly, scary code:</p>
<pre><code class=\'prettyprint\'  class="prettyprint">Sub Auto_Close()
    'Deletes the spreadsheet with the embedded buttons '
    Application.DisplayAlerts = False
    ThisWorkbook.Worksheets("Sheet1").Delete

    'Prevents prompt to save changes, so that workbook closes without saving '
    'This prevents the sheet from being really deleted '
    ThisWorkbook.Saved = True
End Sub</code></pre>
<h3>Concerns</h3>
<p>After this function fires, Excel closes as normal, without saving the file. Since this fires whenever the user closes the file, it seems safe enough to do this, but you can&#8217;t help being a little bit frightened with an approach like this.<br />
<h3>Tests and reasoning</h3>
<p>I think this works. Before, when I would exit Excel, an instance of EXCEL.EXE would remain in my &#8220;Process&#8221; list in Task Manager until I manually terminated it, logged out, or rebooted. Now when I exit Excel, it disappears from the list as normal (unless I&#8217;ve used some other parts of the application which induce the other memory leak problems I&#8217;m not discussing here). It appears that the embedded button memory leak problem is bypassed (I really hesitate to say &#8220;solved&#8221;).<br />
<h3>Suggestions?</h3>
<p>If you have handled this problem before, have more information, or know how a better (and saner) way to fix this, please post a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://duncanandmeg.org/blogs/code/2007/09/13/fixing-memory-leaks-in-excel-vba-we-hope/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

