VBA Programming Series...19 - IT Skills

This blog is for IT lovers, accounts and finance executives who want to add value in their professional life.

test

Welcome to our blog. This blog is for IT lovers, Accounts/Finance Executives who wants to add value in their career path.

Search This Blog

Tuesday, 29 May 2018

VBA Programming Series...19

test
Because VBA is a real, live programming language, it uses many elements common to all programming languages. In this article, I introduce you

to several of these elements: comments, variables, constants, data types, arrays, and a few other goodies. If you’ve programmed with other languages, some of this material will be familiar. If you’re a programming newbie, it’s time to roll up your sleeves and get busy.


Using Comments in Your VBA Code


A comment is the simplest type of VBA statement. Because VBA ignores these statements, they can consist of anything you want. You can insert a comment to remind yourself why you did something or to clarify some particularly elegant code you wrote. Use comments liberally and extensively to describe what the code does (which isn’t always obvious by reading the code itself). Often, code that makes perfect sense today mystifies you tomorrow. Been there. Done that.


You begin a comment with an apostrophe ('). VBA ignores any text that fol-lows an apostrophe in a line of code. You can use a complete line for your comment or insert your comment at the end of a line of code. The following example shows a VBA procedure with three comments, although they’re not necessarily good comments:
Sub CommentsDemo()
‘   This procedure does nothing of value
x = 0 ‘x represents nothingness
‘Display the result
MsgBox x
End Sub

The “apostrophe indicates a comment” rule has one exception: VBA doesn’t interpret an apostrophe inside a set of quotation marks as a comment indica-tor. For example, the following statement doesn’t contain a comment, even though it has an apostrophe:


Msg = “Can’t continue”

When you’re writing code, you may want to test a procedure by excluding a particular statement or group of statements. You could delete the statements and then retype them later. But that’s a waste of time. A better solution is to simply turn those statements into comments by inserting apostrophes. VBA ignores statements beginning with apostrophes when executing a routine. To reactivate those “commented” statements, just remove the apostrophes.


Here’s a quick way to convert a block of statements to comments. In the VBE, choose View➪Toolbars➪Edit to display the Edit toolbar. To convert a block of statements to comments, select the statements and click the Comment Block button. To remove the apostrophes, select the statements and click the Uncomment Block button.

Although comments can be helpful, not all comments are created equal. For example, the following procedure uses lots of comments, but they add noth-ing of value. In this case, the code is clear enough without the comments.


Sub BadComments()

‘ Declare variables Dim x As Integer Dim y As Integer Dim z As Integer ‘ Start the routine

x = 100 ‘ Assign 100 to x y = 200 ‘ Assign 200 to y ‘ Add x and y and store in z

z = x + y
‘   Show the result
MsgBox z
End Sub

Everyone develops his or her own style of commenting. To be useful, how-ever, comments should convey information that’s not immediately obvious from reading the code. Otherwise, comments just chew up bytes and make files larger than necessary.
The following tips can help you make effective use of comments:

✓ Briefly describe the purpose of each Sub or Function procedure you write. ✓ Use comments to keep track of changes you make to a procedure.

✓ Use a comment to indicate that you’re using a function or a construct in an unusual or nonstandard manner.

✓ Use comments to describe the variables you use, especially if you don’t use meaningful variable names.

✓ Use a comment to describe any workarounds you develop to overcome bugs in Excel.

✓ Write comments as you develop code, instead of saving the task for a final step.

✓ Depending on your work environment, consider adding a joke or two as a comment. The person who takes over your job when you get promoted might appreciate the humor.

No comments:

Post a Comment

Popular

Welcome to our blog. If you want to; learn writing skills, preparation for CSS/Corporate laws, IT skills, downloading Business/IT books, and many more; this blog is for you.