Let Statement

See Also2ZYLGX5                 ExampleGU9XHM>Low

Assigns the value of an expression to a variable.


[Let] variable = valueexpression


The Let statement has these parts:

Part                 Description


variable            Name of the variable

=                      Assignment operator

valueexpression          Value assigned to the variable


The reserved word43US84 Let is always optional.  You can assign a String or numeric expression to a variable without using the Let reserved word.  In fact, most Basic programmers never use the Let reserved word.

A value expression can be assigned to a variable only if the data types3GYXY7 they have are compatible.  You can't assign String expressions to numeric variables, and you can't assign numeric expressions to String variables.  If you do, a Type mismatch error occurs at compile time.

Variant8PHEAW3 variables can be assigned either String or numeric expressions.  However, the reverse is not true.  Any Variant except a Null can be assigned to a String variable, but only a Variant that contains a value that can be interpreted as a number can be assigned to a numeric variable.  Use the IsNumeric function to determine if the Variant can be converted to a number.


Caution          Assigning an expression of one numeric data type to a variable of a different numeric data type coerces the value of the expression into the data type of the resulting variable.


Let statements can be used to assign one record variable to another only when both variables are of the same user-defined type.  Use the LSet statement to assign record variables of different user-defined types.

See Also

Const Statement3DARGS

IsNumeric Function1MXC66A

LSet Statement103F0PY

Set StatementLANSET

Let Statement Example

The example uses statements with and without the Let reserved word to assign the value of an expression to a variable.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.


Sub Form_Click ()

  Dim Msg, Pi ' Declare variable.

  ' Assign Pi using Let statement.

  Let Pi = 4 * Atn(1)

  ' Assign Msg without Let statement.

  Msg = "The area of circle whose radius is 3 inches is "

  Msg = Msg & (Pi * (3^2)) & " inches."

  MsgBox Msg  ' Display message.

End Sub