How do I assign a value to a variable?
Here are examples of simple declarations of variable types, and how to assign values to them:
' This program illustrates simple assignments of ' values to variables and elements in arrays. Sub Main
Dim i As Long, j As Long Dim c As String Dim x As Double Dim xx(10) As Double Dim xx1(1 To 10) As Double i=1 j=2 x=i+j ' Note that the Str(x) function converts x ' to a string. c="1+2=" & Str(x) ' This function will display a message box ' with the text "1+2=3" MsgBox c xx(0)=x xx1(1)=x xx(10)=xx(0) + xx1(1) ' Note that the _ (underline) at the end of a ' line can be used to continue a statement to the ' next line. c="1+2 + 1+2 =" _ & Str(xx(10)) MsgBox c
End Sub
- Assigning objects to variables
- When assigning objects to variables (see
What is meant by the term "object model?"), you need to use the syntax Set Variable = Object; for example:
Sub Main
Dim wb As Workbook Dim ss As Spreadsheet Dim g As Graph Set wb = ActiveWorkbook ' Note that the _ (underline) at the end of a| ' line can be used to continue a statement to the ' next line. Set ss = Spreadsheets.Open( _ Path & "\Examples\Datasets\Adstudy.sta") ss.Visible = True Set g = ActiveGraph
End Sub