Monday, January 30, 2012

How to Keep an Idiot Busy for Hours - the VBA edition

Triviality has many faces. You can have something that is both trivial and inane - like "Thou must not end thy sentences with prepositions" (another topic for another day). You can have something that is trivial and interesting - the banana slug on the sidewalk this morning, for example. There is a third category I'm going to posit: the things that are trivial, inane, and make me smile. This category includes hats with ridiculous pom-poms, "Sh*t _______ Say/s" videos, clever graffiti, and this bit of code I wrote this morning:
Sub FindMeThisCell()
   Worksheets("Sheet2").Activate
   Range("A2").Activate
   X = Range("A2")
   MsgBox X
   ActiveCell.Font.Bold = True
End Sub
This code does next to nothing. It's worse, actually - it does next to nothing and it doesn't even do what it says it does, namely, finding a cell. There is no searching involved here, to be honest. You should never, ever code like this, and if you do, you should find absolutely no enjoyment in using it. You should never smile at what it does, nor cause it to execute multiple times in a row because you think it's actually kind of cute.

Let me explain exactly how bad this really is. This snippet of code is a subroutine. A subroutine is a bit of code that you stick in your spreadsheet's module (accessory code) that you can call up whenever you feel like it - when today's date is after May 5th, when you click a button, when cell B1 turns green, whatever. Subroutine (abbreviated "Sub") FindMeThisCell  has a name - FindMeThisCell - that I can use to identify it later. The parentheses at the end just tell you that you don't need any information to run this code. *

So much for Sub, End Sub (does what you think it does), and FindMeThisCell(). The next two lines 
Sub FindMeThisCell()
   Worksheets("Sheet2").Activate
   Range("A2").Activate
   X = Range("A2")
   MsgBox X
   ActiveCell.Font.Bold = True
End Sub
pick out a particular cell in my file and turn it on - "activate" it. This is sort of like playing Pokemon. Go Sheet2! Activate! Go Range("A2")! Now cell A2 in Sheet 2 is ready and raring to go. 
Now I pick a variable - the quintessential "x", why not - 
Sub FindMeThisCell()
   Worksheets("Sheet2").Activate
   Range("A2").Activate

   X = Range("A2")
   MsgBox X
   ActiveCell.Font.Bold = True
End Sub
and read the contents of A2 to it. "X" dutifully memorizes this information. Next I choose a Message Box (or "MsgBox" - can't ever spare the vowels, those boxes) and tell it to recite whatever "X" says. 
Sub FindMeThisCell()
   Worksheets("Sheet2").Activate
   Range("A2").Activate
   X = Range("A2")

   MsgBox X
   ActiveCell.Font.Bold = True
End Sub
The next part is the worst bit. It takes the active cell, A2, and makes it bold. For absolutely no reason. No one benefits from this. The world is not better for it. I'm not any happier about it. It's worthless, and here I am wasting another whole paragraph about it. Enough!
Sub FindMeThisCell()
   Worksheets("Sheet2").Activate
   Range("A2").Activate
   X = Range("A2")
   MsgBox X

   ActiveCell.Font.Bold = True
End Sub
Now I go back to my original spreadsheet. I show the Developer tab (Office>Options>check "show Developer tab in ribbon"). In the Forms toolbar, I click on the rectangular icon



 to make a button. I title this button "Find me data!" and link it to the FindMeThisCell macro.



 And lastly, abashedly, I go to cell A2 and type in "Found me!" 

Then I click the button. And god help me, I smile at it. 







Click. Found me! Click. Found me! Click. Found me!

Isn't it awful?




*(As a counterexample, if you wrote some code to calculate the area of a rectangle, you would have to write Sub FindMeTheAreaOfThisRectangle(height,width), which would mean that every time you wanted to run it, you'd have to go and track down a rectangle to use it on. Very inconvenient..)

No comments:

Post a Comment