Language
Using Comments in VBA Code
Using Assignment Statements
Sub FormatCells()
‘ Exit if a range is not selected
If TypeName(Selection) “Range” Then
MsgBox “Select a range.”
Exit Sub
End If
‘ Format the cells
With Selection
.HorizontalAlignment = xlRight
.WrapText = False ‘ no wrap
.MergeCells = False ‘ no merged cells
End With
End Sub
Using Variables, Constants, and
Data Types
Since VBA is a real, live programming language, it utilizes various elements common to all programming languages. Comment is the simplest type of VBA statement. Apostrophe (‘) is used to start a comment. Comments can be inserted at the end of a line of code or you use the entire line for your comment. See example below:
Arrays
*Property of STI
I0033
1 __________________________ 2 _________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
____________________________ ___________________________
VBA does not interpret an apostrophe inside a set of quotation marks as a comment indicator. The example below illustrates this condition, though there is an apostrophe:
Edit Toolbar under the View Menu
Msg = “Can’t continue”
VBA will ignore statements starting with apostrophes when a routine is executed. Simply remove the apostrophes to make those statements active again. The quick way to convert a series of statements to comments in VBE is to select View > Toolbars > Edit to show the Edit Toolbar. Select the statements and click the
Comment Block button to convert the series of statements into comments. Select the statements