<?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>nandeshwar.info &#187; stack columns</title>
	<atom:link href="http://nandeshwar.info/tag/stack-columns/feed/" rel="self" type="application/rss+xml" />
	<link>http://nandeshwar.info</link>
	<description></description>
	<lastBuildDate>Mon, 12 Dec 2011 21:33:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Stack Columns of Data on one column</title>
		<link>http://nandeshwar.info/2005/12/23/stack-columns-of-data-on-one-column/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=stack-columns-of-data-on-one-column</link>
		<comments>http://nandeshwar.info/2005/12/23/stack-columns-of-data-on-one-column/#comments</comments>
		<pubDate>Fri, 23 Dec 2005 17:55:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Useful Procedures]]></category>
		<category><![CDATA[excel]]></category>
		<category><![CDATA[stack columns]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://a7n9.wordpress.com/2005/12/23/stack-columns-of-data-on-one-column/</guid>
		<description><![CDATA[I have modified this code for better explanation and error handling (includes a function to check if a worksheet exists or not). To run this code follow these steps: 1. Insert a new module in your workbook, 2. Copy and paste this code, 3. Go back to worksheet with data in it, 4. Press Alt [...]]]></description>
			<content:encoded><![CDATA[<p>I have modified this code for better explanation and error handling (includes a function to check if a worksheet exists or not). To run this code follow these steps:<br />
1. Insert a new module in your workbook,<br />
2. Copy and paste this code,<br />
3. Go back to worksheet with data in it,<br />
4. Press Alt + F8 to bring the macro window<br />
5. Select this procedure and hit run<br />
6. Enter the new worksheet name in the input box<br />
7. If everything went well, you should have a new worksheet with all the data from original worksheet in one column with the column headers. See the screen shots for example.<br />
Before stacking-the original data:<br />
<img title="Data before stacking" src="http://www.nandeshwar.info/projects/xlblog/uploaded_images/BeforeStack-754055.bmp" alt="Before Stack" /><br />
After stacking:<br />
<img title="Data after stacking" src="http://www.nandeshwar.info/projects/xlblog/uploaded_images/AfterStack-754068.bmp" alt="After Stack" /><br />
Here&#8217;s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #000080;">Option</span> <span style="color: #000080;">Explicit</span>
&nbsp;
<span style="color: #000080;">Sub</span> Stack_cols()
&nbsp;
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> Stack_cols_Error
&nbsp;
<span style="color: #000080;">Dim</span> lNoofRows <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>, lNoofCols <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
<span style="color: #000080;">Dim</span> lLoopCounter <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>, lCountRows <span style="color: #000080;">As</span> <span style="color: #000080;">Long</span>
<span style="color: #000080;">Dim</span> sNewShtName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>
<span style="color: #000080;">Dim</span> shtOrg <span style="color: #000080;">As</span> Worksheet, shtNew <span style="color: #000080;">As</span> Worksheet
&nbsp;
<span style="color: #008000;">'Turn off the screen update to make macro run faster
</span>Application.ScreenUpdating = <span style="color: #000080;">False</span>
<span style="color: #008000;">'Ask for a new sheet name, if not provided use newsht
</span>sNewShtName = InputBox(<span style="color: #800000;">&quot;Enter the new worksheet name&quot;</span>, <span style="color: #800000;">&quot;Enter name&quot;</span>, <span style="color: #800000;">&quot;newsht&quot;</span>)
<span style="color: #008000;">'Set a sheet variable for the sheet where the data resides
</span><span style="color: #000080;">Set</span> shtOrg = ActiveSheet
<span style="color: #008000;">'Add a new worksheet, rename it and set it to a variable
</span><span style="color: #000080;">If</span> <span style="color: #000080;">Not</span> SheetExists(sNewShtName) <span style="color: #000080;">Then</span>
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sNewShtName
<span style="color: #000080;">Set</span> shtNew = Worksheets(sNewShtName)
<span style="color: #000080;">Else</span>
MsgBox <span style="color: #800000;">&quot;Worksheet name exists. Try again&quot;</span>, vbInformation, <span style="color: #800000;">&quot;Sheet Exists&quot;</span>
<span style="color: #000080;">Exit</span> <span style="color: #000080;">Sub</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
&nbsp;
<span style="color: #000080;">With</span> shtOrg
<span style="color: #008000;">'Get the last column number
</span><span style="color: #008000;">'Replace .Range(&quot;IV1&quot;) with .Range(&quot;XFD1&quot;) for Excel 2007
</span>lNoofCols = .Range(<span style="color: #800000;">&quot;IV1&quot;</span>).<span style="color: #000080;">End</span>(xlToLeft).Column
<span style="color: #008000;">'Start a loop to copy and paste data from the first column to the last column
</span><span style="color: #000080;">For</span> lLoopCounter = 1 <span style="color: #000080;">To</span> lNoofCols
<span style="color: #008000;">'Count the number of rows in the looping column
</span><span style="color: #008000;">'Replace .Cells(65536, lLoopCounter) with .Cells(1048576, lLoopCounter) for Excel 2007
</span>lNoofRows = .Cells(65536, lLoopCounter).<span style="color: #000080;">End</span>(xlUp).Row
.Range(.Cells(1, lLoopCounter), .Cells(lNoofRows, lLoopCounter)).Copy Destination:=shtNew.Range(shtNew.Cells(lCountRows + 1, 1), shtNew.Cells(lCountRows + lNoofRows, 1))
<span style="color: #008000;">'count the number of rows in the new worksheet
</span>lCountRows = lCountRows + lNoofRows
<span style="color: #000080;">Next</span> lLoopCounter
<span style="color: #000080;">End</span> <span style="color: #000080;">With</span>
&nbsp;
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> 0
SmoothExit_Stack_cols:
Application.ScreenUpdating = <span style="color: #000080;">True</span>
<span style="color: #000080;">Exit</span> <span style="color: #000080;">Sub</span>
&nbsp;
Stack_cols_Error:
MsgBox <span style="color: #800000;">&quot;Error &quot;</span> &amp; Err.Number &amp; <span style="color: #800000;">&quot; (&quot;</span> &amp; Err.Description &amp; <span style="color: #800000;">&quot;) in Sub:Stack_cols&quot;</span>
<span style="color: #000080;">Resume</span> SmoothExit_Stack_cols
<span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
<span style="color: #008000;">'Check if a worksheet exists or not
</span><span style="color: #000080;">Public</span> <span style="color: #000080;">Function</span> SheetExists(sShtName <span style="color: #000080;">As</span> <span style="color: #000080;">String</span>) <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">Resume</span> <span style="color: #000080;">Next</span>
&nbsp;
<span style="color: #000080;">Dim</span> wsSheet <span style="color: #000080;">As</span> Worksheet, bResult <span style="color: #000080;">As</span> <span style="color: #000080;">Boolean</span>
bResult = <span style="color: #000080;">False</span>
<span style="color: #000080;">Set</span> wsSheet = Sheets(sShtName)
&nbsp;
<span style="color: #000080;">On</span> <span style="color: #000080;">Error</span> <span style="color: #000080;">GoTo</span> 0
<span style="color: #000080;">If</span> <span style="color: #000080;">Not</span> wsSheet <span style="color: #000080;">Is</span> <span style="color: #000080;">Nothing</span> <span style="color: #000080;">Then</span>
bResult = <span style="color: #000080;">True</span>
<span style="color: #000080;">End</span> <span style="color: #000080;">If</span>
SheetExists = bResult
<span style="color: #000080;">End</span> <span style="color: #000080;">Function</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://nandeshwar.info/2005/12/23/stack-columns-of-data-on-one-column/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

