Table of contents
TOC
Collapse the table of content
Expand the table of content

Writing Visual Basic Statements

office 365 dev account|Last Updated: 6/12/2017
|
1 Contributor

A statement in Visual Basic is a complete instruction. It can containkeywords, operators, variables, constants, and expressions. Each statement belongs to one of the following three categories:

  • Declaration statements, which name a variable, constant, or procedure and can also specify a data type.Writing Declaration Statements

  • Assignment statements, which assign a value or expression to a variable or constant.Writing Assignment Statements

  • Executable statements, which initiate actions. These statements can execute a method or function, and they can loop or branch through blocks of code. Executable statements often contain mathematical or conditional operators.Writing Executable Statements

Continuing a Statement over Multiple Lines

A statement usually fits on one line, but you can continue a statement onto the next line using a line-continuation character. In the following example, the MsgBox executable statement is continued over three lines:

Sub DemoBox() 'This procedure declares a string variable, 
 ' assigns it the value Claudia, and then displays 
 ' a concatenated message. 
 Dim myVar As String 
 myVar = "John" 
 MsgBox Prompt:="Hello " &; myVar, _ 
 Title:="Greeting Box", _ 
 Buttons:=vbExclamation 
End Sub

Adding Comments

Comments can explain a procedure or a particular instruction to anyone reading your code. Visual Basic ignores comments when it runs your procedures. Comment lines begin with an apostrophe ( ' ) or with Rem followed by a space, and can be added anywhere in a procedure. To add a comment to the same line as a statement, insert an apostrophe after the statement, followed by the comment. By default, comments are displayed as green text.

Checking Syntax Errors

If you press ENTER after typing a line of code and the line is displayed in red (an error message may display as well), you must find out what's wrong with your statement, and then correct it.

© 2017 Microsoft