<?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>theMike.com &#187; vba</title>
	<atom:link href="http://www.themike.com/tag/vba/feed" rel="self" type="application/rss+xml" />
	<link>http://www.themike.com</link>
	<description></description>
	<lastBuildDate>Sat, 29 Oct 2011 18:56:32 +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>More than a &#8220;Hello World&#8221; in VBA</title>
		<link>http://www.themike.com/more-than-a-hello-world-in-vba</link>
		<comments>http://www.themike.com/more-than-a-hello-world-in-vba#comments</comments>
		<pubDate>Mon, 24 May 2010 18:31:42 +0000</pubDate>
		<dc:creator>mike.hanson</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[vba]]></category>

		<guid isPermaLink="false">http://www.themike.com/?p=55</guid>
		<description><![CDATA[When I started at Bates Group, LLC one of my first assignments was to debug an Excel VBA macro.  Knowing nothing about the language I fought my way through the bug and fixed the macro.  After that I quickly decided &#8230; <a href="http://www.themike.com/more-than-a-hello-world-in-vba">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>When I started at Bates Group, LLC one of my first assignments was to debug an Excel VBA macro.  Knowing nothing about the language I fought my way through the bug and fixed the macro.  After that I quickly decided to learn more about the language.   Since &#8220;Hello World&#8221; only gets me so far, I decided to do something a little tougher.  What better way to do that than to think back to my college assignments?</p>
<p>Back then one of the assignments I had was to write a random walk function.  Imagine standing next to a lamppost on the street.  From the lamppost you can take a step in one of four directions; North, South, East, or West.  You take a step in a random direction and then look at where you are.  From your new location you take another step in a random direction and you keep taking these random steps for a while.  Finally you stop and look up, how far away from the lamppost are you?</p>
<p>The following function does that only much faster than you or I could.  It takes 20,000 steps total and colors them along the way.  Every 2,000 steps it will change colors leaving a cool trail as it goes along.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Sub</span> TakeAWalk()
    Workbooks(<span style="color: #800000;">&quot;themikecom_vba_helloworld.xls&quot;</span>).Activate
    ActiveWorkbook.Worksheets(<span style="color: #800000;">&quot;Board&quot;</span>).<span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
    <span style="color: #008000;">' Where on the sheet should we start?
</span>    ActiveSheet.Range(<span style="color: #800000;">&quot;EE150&quot;</span>).<span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
    <span style="color: #008000;">' How many steps per turn should we take?
</span>    STEPS_PER_TURN = 2000
&nbsp;
    <span style="color: #008000;">' How many turns should we take?
</span>    TURNS = 10
&nbsp;
    <span style="color: #8D38C9; font-weight: bold;">For</span> j = 3 <span style="color: #8D38C9; font-weight: bold;">To</span> (TURNS + 3)
    <span style="color: #8D38C9; font-weight: bold;">For</span> i = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> STEPS_PER_TURN
&nbsp;
        <span style="color: #008000;">' Should we step east or west?
</span>        randomX = Int(4 * Rnd)
&nbsp;
        <span style="color: #008000;">' Should we step north or south?
</span>        randomY = Int(4 * Rnd)
&nbsp;
        <span style="color: #008000;">' Move west-east
</span>        <span style="color: #8D38C9; font-weight: bold;">Select</span> <span style="color: #8D38C9; font-weight: bold;">Case</span> randomX
            <span style="color: #8D38C9; font-weight: bold;">Case</span> 2 <span style="color: #008000;">' Move one step west
</span>                <span style="color: #8D38C9; font-weight: bold;">If</span> ActiveCell.Column &amp;lt; 1 <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #008000;">' Do not overstep the west border
</span>                    ActiveCell.Offset(0, -1).<span style="color: #8D38C9; font-weight: bold;">Select</span>
                <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span>
&nbsp;
            <span style="color: #008000;">' Case 1 - Stay in the same spot
</span>
            <span style="color: #8D38C9; font-weight: bold;">Case</span> 0 <span style="color: #008000;">' Move one step east
</span>                <span style="color: #8D38C9; font-weight: bold;">If</span> ActiveCell.Column &amp;lt;= 255 <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #008000;">' Do not overstep the east border                     ActiveCell.Offset(0, 1).Select                 End If         End Select         ' Move north-south         Select Case randomY             Case 2 ' Move one step north                 If ActiveCell.Row &amp;gt; 1 Then ' Do not overstep the north border
</span>                    ActiveCell.Offset(-1, 0).<span style="color: #8D38C9; font-weight: bold;">Select</span>
                <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span>
&nbsp;
            <span style="color: #008000;">' Case 1 - Stay in the same spot
</span>
            <span style="color: #8D38C9; font-weight: bold;">Case</span> 0 <span style="color: #008000;">' Move one step south
</span>                <span style="color: #8D38C9; font-weight: bold;">If</span> ActiveCell.Row &amp;lt;= 65535 <span style="color: #8D38C9; font-weight: bold;">Then</span> <span style="color: #008000;">' Do not overstep the south border
</span>                    ActiveCell.Offset(1, 0).<span style="color: #8D38C9; font-weight: bold;">Select</span>
                <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">If</span>
        <span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
        <span style="color: #008000;">' Leave a trail
</span>        ActiveCell.Interior.ColorIndex = j
&nbsp;
    <span style="color: #8D38C9; font-weight: bold;">Next</span> i
    <span style="color: #8D38C9; font-weight: bold;">Next</span> j
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></pre></div></div>

<p><img class="alignleft" title="Macro Flower Shot" src="http://www.themike.com/wp-content/uploads/2010/05/macro.jpg" alt="Macro Flower Shot" width="200" height="200" />With that done I wanted to add another function to learn how to create a menu.  I came up with the square flower.  This function will generate a square of random size with each section of the square filled with a different color.  This function taught me some tricks about looping in VBA, some ways are a lot faster than others.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;"><span style="color: #E56717; font-weight: bold;">Public</span> <span style="color: #E56717; font-weight: bold;">Sub</span> Flower()
    Workbooks(<span style="color: #800000;">&quot;themikecom_vba_helloworld.xls&quot;</span>).Activate
    ActiveWorkbook.Worksheets(<span style="color: #800000;">&quot;Board&quot;</span>).<span style="color: #8D38C9; font-weight: bold;">Select</span>
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">Dim</span> start <span style="color: #151B8D; font-weight: bold;">As</span> Range
    <span style="color: #151B8D; font-weight: bold;">Dim</span> Length <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
    <span style="color: #151B8D; font-weight: bold;">Dim</span> Width <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
    <span style="color: #151B8D; font-weight: bold;">Dim</span> Color <span style="color: #151B8D; font-weight: bold;">As</span> <span style="color: #F660AB; font-weight: bold;">Integer</span>
&nbsp;
    <span style="color: #008000;">' The starting point of the flower
</span>    <span style="color: #151B8D; font-weight: bold;">Set</span> start = ActiveCell
&nbsp;
    <span style="color: #008000;">' The maximum size of the flower
</span>    size = Int(57 * Rnd)
&nbsp;
    <span style="color: #008000;">' Ignore boundry errors for now
</span>    <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #151B8D; font-weight: bold;">Resume</span> <span style="color: #8D38C9; font-weight: bold;">Next</span>
&nbsp;
    <span style="color: #8D38C9; font-weight: bold;">For</span> z = 0 <span style="color: #8D38C9; font-weight: bold;">To</span> size
        <span style="color: #008000;">' Generate a random color for this row
</span>        Color = Int((56 - 1 + 1) * Rnd + 1)
&nbsp;
        <span style="color: #008000;">' Left side
</span>        Range(start.Offset(0, 0), start.Offset(Length, 0)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #008000;">' Bottom side
</span>        Range(start.Offset(Length, 0), start.Offset(Length, Length)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #008000;">' Upper side
</span>        Range(start.Offset(0, 0), start.Offset(0, Width)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #008000;">' Right side
</span>        Range(start.Offset(0, Width), start.Offset(Width, Width)).Interior.ColorIndex = Color
&nbsp;
        <span style="color: #151B8D; font-weight: bold;">Set</span> start = start.Offset(-1, -1)
        Length = Length + 2
        Width = Width + 2
    <span style="color: #8D38C9; font-weight: bold;">Next</span> z
&nbsp;
    <span style="color: #151B8D; font-weight: bold;">On</span> <span style="color: #151B8D; font-weight: bold;">Error</span> <span style="color: #8D38C9; font-weight: bold;">GoTo</span> 0
<span style="color: #8D38C9; font-weight: bold;">End</span> <span style="color: #E56717; font-weight: bold;">Sub</span></pre></div></div>

<p>So what did I learn after all of this?  Mostly that I have a strong dislike for VBA.  It works well for small projects with small data sets.  However those small projects quickly expand into real programs which need to be maintained.  You are better off doing it right the first time instead of maintaining a large clunky macro.<img class="size-full  wp-image-56 alignright" title="Excel Random Walk" src="http://www.themike.com/wp-content/uploads/2010/05/excel.jpg" alt="Excel Random Walk" width="300" height="212" /></p>
<p><a href="http://www.themike.com/wp-content/uploads/2011/04/themikecom_vba_helloworld.xls">Download the complete macro here.</a> You will need to enable macros in your security settings to get them to work.  Once enabled, select &#8220;Random Walk&#8221; from the &#8220;theMike.com &#8211; Hello World VBA&#8221; menu.  This will start a random walk which will finish after a couple of seconds.  The &#8220;Square Flower&#8221; menu item will create a square flower under your cursor.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.themike.com/more-than-a-hello-world-in-vba/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

