<?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>Techkeyla &#187; excel</title>
	<atom:link href="http://www.techkeyla.com/tag/excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.techkeyla.com</link>
	<description>Served Neat</description>
	<lastBuildDate>Sat, 24 Jul 2010 14:55:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Excel Tips: How To Create a Dynamic Range of Cell Selection</title>
		<link>http://www.techkeyla.com/excel-tips-how-to-create-a-dynamic-range-of-cell-selection/</link>
		<comments>http://www.techkeyla.com/excel-tips-how-to-create-a-dynamic-range-of-cell-selection/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 10:27:57 +0000</pubDate>
		<dc:creator>Sujoy</dc:creator>
				<category><![CDATA[excel]]></category>
		<category><![CDATA[how to]]></category>
		<category><![CDATA[analysis]]></category>
		<category><![CDATA[techkeyla]]></category>

		<guid isPermaLink="false">http://www.techkeyla.com/?p=381</guid>
		<description><![CDATA[How To Create a Dynamic Range of Cell Selection in Excel Macro.]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="Excel Tips" src="http://randomgeekspace.files.wordpress.com/2009/10/excel.jpg" alt="" width="173" height="172" /><em>In the past couple of months of working on Excel and VBA Macros, I have been exposed to a few challenges which in the course of time, I have overcome. So, I thought I would share them with my beloved readers. This section: Excel Tips aims at sharing the small tips and tricks that I have come across which makes life easier for anyone working with Excel or VBA Macros. Cheers!</em></p>
<p>Here we&#8217;ll be focussing on how to select a range dynamically and perform further operations to it etc.<br />
For e.g. If we have a Sheet &#8220;Input&#8221; of which one cell is required say column B, and we need to perform some kind of mathematical operation on them &#8211; viz. average, sum, etc. we need to do that by selecting the entire range of non-empty values of those two cells.</p>
<p>The problem here is that the number of entries in column A will vary from time to time, but we want a command button which automatically knows the amount of non-empty values and accordingly runs the mathematical operation. This is known as Dynamic Range Selection.</p>
<p><span id="more-381"></span></p>
<p>In other words, in case 1, if Cell B has values from B2 to B300, The Average formula should read: =AVERAGE(B2:B300), and in case the number of entries increases, the second input parameter in the average formula should change. The way to overcome this is a simple VBA code which records the number of entries in a particular cell and stores in a variable defined as a Long. This is:</p>
<p><code>mlngRange = CLng(Sheets("Input").Range("B2", Range("B2").End(xlDown)).Count)</code></p>
<p>Here, mlngRange is the Long variable in which the number of values in cell B from B2 onwards is recorded. Please remember if the declaration of mlngRange is done as a Long, include CLng as VBA by default, processes output as a String.</p>
<p>If the start point of selection is to be changed from B2 to say B4, this needs to be edited in the above code.</p>
<p><strong>Limitation: </strong>The limitation of this code is that it recognises the first empty value in a cell, and considers it as the end-point. In case of column B having a few empty values in between, the remaining cell values will not be counted in mlngRange.</p>
<p>Hence, the next code is necessary. This code is unlike the previous one, which counts in a downward direction from the top columns to the bottom ones. Instead, this code counts in an upward direction, from the last non-empty cell onwards. The code is:<br />
<code>mlngRange = CLng(Worksheets("Input").Cells(Rows.Count, 2).End(xlUp).Row) - 1</code></p>
<p>Here, the &#8217;2&#8242; denotes the Column. As in A=1, B=2, etc. The (-1) in the end denotes that the first value in Column B should not be considered as in this case, Cell B1 is a label. If the count is to be considered from B2, this should be replaced by (-2)  and so on.</p>
<p>So what after storing the value of the range in a range variable?</p>
<p>Performing mathematical operations:</p>
<p>The output formula can be:</p>
<p><code>ActiveCell.Formula = "=AVERAGE(B2:B" &amp; mlngRange + 1 &amp; ")"</code></p>
<p>Please let me know if you face any issues with this. Cheerio!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.techkeyla.com/excel-tips-how-to-create-a-dynamic-range-of-cell-selection/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
