<?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>Techno m'lounge - Where technology meets human senses.&#187; VBA</title>
	<atom:link href="http://sumitghosh.co.in/tag/vba/feed/" rel="self" type="application/rss+xml" />
	<link>http://sumitghosh.co.in</link>
	<description>Technology, if not handled with care becomes disruptive, Iam a live example...</description>
	<lastBuildDate>Fri, 06 Jan 2012 13:53:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Writing an Excel Macro</title>
		<link>http://sumitghosh.co.in/writing-an-excel-macro/</link>
		<comments>http://sumitghosh.co.in/writing-an-excel-macro/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 10:17:26 +0000</pubDate>
		<dc:creator>Sumit Ghosh</dc:creator>
				<category><![CDATA[MS Excel]]></category>
		<category><![CDATA[Office Tools]]></category>
		<category><![CDATA[MS Excel Macro]]></category>
		<category><![CDATA[VBA]]></category>

		<guid isPermaLink="false">http://sumitghosh.co.in/?p=117</guid>
		<description><![CDATA[Recently I got another project , where I had to create some cool Excel Macros for doing some data churning stored in excel. This was the first time I was writing a Macro so I decided to go after it with full valour. Excel Macros are written using Visual Basic which has been one of [...]]]></description>
			<content:encoded><![CDATA[<p>
<!-- Begin Google Adsense code -->
<script type="text/javascript"><!--
google_ad_client = "pub-2400903147192847";
/* 300x250, created 9/27/09 - bysumit */
google_ad_slot = "3492758136";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- End Google Adsense code -->
<br />
Recently I got another project , where I had to create some cool Excel Macros for doing some data churning stored in excel. This was the first time I was writing a Macro so I decided to go after it with full valour. Excel Macros are written using Visual Basic which has been one of my favourite languages from school days(I did my first commercial project using VB 6.0 and I was paid handsomely for it <img src='http://sumitghosh.co.in/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).I have posted a tutorial on whatever I learnt.<br />
<span id="more-117"></span><br />
Introduction -</p>
<p>Visual Basic for Applications (or VBA) is a variation on the Visual Basic language which extends the basic syntax to include specific object trees used to describe Microsoft Office application components. These include documents, worksheets, and other containers, as well as some useful built-in functions for manipulating them.</p>
<p>Like VB, VBA is based on the BASIC language, and so should be familiar. Unlike VB, it is interpreted-only, when used in the context of Microsoft Office, and as such might be significantly slower than it&#8217;s compiled cousin. It is generally only used to enhance an application document, or connect across Microsoft Office applications.</p>
<p>Task Accomplished</p>
<p>I had some data in an Excel Sheet , I had to do three tasks.</p>
<ul>
<li>Write a module to Load some data(User Data) into a new row in excel at intervals of 30 rows.</li>
<li>Delete all those rows where the column-&#8221;userdata&#8221; was empty.</li>
<li>Copy data in intervals from one excel sheet to another sheet.</li>
</ul>
<p>I will post the code which I had written, and explain what all I did, hope this you helps in writing your own excel macros.</p>
<p><strong>&#8211;If you want to programatically add values to some rows in a sheet use this code.</strong><br />
<code><br />
Sub UserLoad_Calc()<br />
''declare userLoad<br />
Dim UserLoad As Long<br />
Dim StepLoad As Long<br />
Dim counter As Long</code></p>
<p>StepLoad = 50<br />
UserLoad = 50<br />
&#8221;The for Loop to loop through all the rows with steps of 30<br />
For counter = 2 To Worksheets(&#8220;UserLoadData&#8221;).rows.Count Step 30</p>
<p>If UserLoad &lt; 4000 Then<br />
Worksheets(&#8220;UserLoadData&#8221;).Cells(counter, 9) = UserLoad<br />
UserLoad = UserLoad + StepLoad<br />
Else<br />
Worksheets(&#8220;UserLoadData&#8221;).Cells(counter, 9) = UserLoad<br />
End If<br />
Next counter<br />
End Sub</p>
<p><strong>&#8211;If You want to delete those rows if a particular cell is empty use this code. In my case column 9 of each cell is empty.</strong></p>
<p><code><br />
Sub deleteRows()</code></p>
<p>Dim rows As Long<br />
Dim rng As Range<br />
Set rng = ActiveSheet.UsedRange.rows<br />
With Application<br />
.Calculation = xlCalculationManual<br />
.ScreenUpdating = False &#8221; To increase the performance since this is a high CPU activity.</p>
<p>Debug.Print rng.rows.Count<br />
For i = rng.rows.Count To 2 Step -1<br />
&#8221;find the row whose cell number 9 is empty.<br />
If Application.WorksheetFunction.CountA(rng.Cells(i, 9)) = 0 Then<br />
rng.rows(i).EntireRow.Delete<br />
ActiveSheet.UsedRange<br />
End If<br />
Next i<br />
&#8221;setting back to normal<br />
.Calculation = xlCalculationAutomatic<br />
.ScreenUpdating = True<br />
End With<br />
End Sub</p>
<p><strong>&#8211;If you want to programatically fill up rows/columns of one sheet from filtered data from some other sheet, use this code.</strong><br />
<code><br />
Sub LoadNewData()<br />
Dim i As Long<br />
i = 2</code></p>
<p>&#8221;The for Loop to loop through all the rows with steps of 30<br />
For counter = 2 To Worksheets(&#8220;Data&#8221;).rows.Count Step 30</p>
<p>&#8221;fill request execution time<br />
Worksheets(&#8220;UserLoadData&#8221;).Cells(i, 10) = Worksheets(&#8220;Data&#8221;).Cells(counter, 17)</p>
<p>&#8221;fill request wait time<br />
Worksheets(&#8220;UserLoadData&#8221;).Cells(i, 13) = Worksheets(&#8220;Data&#8221;).Cells(counter, 18)</p>
<p>&#8221;fill requests queued<br />
Worksheets(&#8220;UserLoadData&#8221;).Cells(i, 14) = Worksheets(&#8220;Data&#8221;).Cells(counter, 20)<br />
i = i + 1<br />
Next counter<br />
End Sub</p>
<p>Do drop me a line if you have any questions regarding automating your excel tasks or creating macros using VBA.<br />

<!-- Begin Google Adsense code -->
<script type="text/javascript"><!--
google_ad_client = "pub-2400903147192847";
/* 300x250, created 9/27/09 - bysumit */
google_ad_slot = "3492758136";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<!-- End Google Adsense code -->
</p>
<img src="http://sumitghosh.co.in/?ak_action=api_record_view&id=117&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://sumitghosh.co.in/writing-an-excel-macro/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

