- Visual Basic isn't case-sensitive, but it preserves the capitalization in the statement where the name is declared.
http://msdn.microsoft.com/en-us/library/ee441152.aspx
Types
Edit
- Variant
- Default data type if not specify
- Slower to process and large foot-print
- Array
- Store elements of single type
- Index start from 0. Declaration also assume start from 0. Can be change with "Option Base" at the top of a module.
- Inefficient is don't know the exact size of the array
- Contiguous
- Declare: Dim myArray(4) As Currency 'declare 5 elements
- Access: myArray(index)
- Multi-dimension: Dim myArray(0 To 4, 0 To 9) As Integer 'declare 5x10 array. Max 60-dimensional
- Note: Array of variant data
- Dim myArray(5) As Variant or
- Dim myArray As Variant
- myArray = Array(...)
- Collection
- Like dynamic array, elements can be of different types
- Declare: Dim myArray As Collection
- Add: myArray.Add "Element1", "Element2"
- Access: myArray.Item(1) or myArray(1) or myArray("Element2")
- Remove: myArray.Remove 1 or myArray.Remove "Element2"
- Remove all: Set myArray = New Collection or Set myArray = Nothing
Special
Edit
- Empty
- Only for variant
- Initial value of variant type
- Check with IsEmpty()
- Null
- For variant, fields, and controls
- Check with IsNull()
- Cannot be compared to each other
Conditional Logic
Edit
If Statement
Edit
- If <condition> Then
- ElseIf <condition> Then
- Else
- End If
For Loop
Edit
Use "Exit For" to exit the loop
- For counter=<start> To <end> [Step <step>]
Next [counter]
- For Each <object> In <collection|array>
Next [counter]
Do Loop
Edit
Use "Exit Do" to exit the loop
- Do While <condition>
Loop
- Do
Loop While <condition>
- Do Until <condition>
Loop
- Do
Loop Until <condition>
Select Case Statement
Edit
Select Case <expression>
- Case [value]
- Case [multi values: 2, 5, 7]
- Case [range: 2 To 7]
- Case [comparison: val > 10]
- Case Else
End Select
Assignment
Edit
- Use = to assign to a variable or constant
- Use of Let is optional
- Use Set to assign object
Comment
Edit
- Start with apostrophe(') or Rem
Line Continuation
Edit
- Use underscore(_)
String Concatenation
Edit
- Use ampersand(&)
Function and Subroutine
Edit
Function
Edit
- Return value
- Set variable name the same as function name to return the value
- To ignore the return value, call the function the way we call Sub--without parenthesis and pass parameters as usual
Subroutine
Edit
- Does not return any value
- Call: mySub arg1, arg2 or Call mySub(arg1,arg2)
Named Arguments
Edit
Say we have Sub mySub(arg1 As Integer, arg2 As String)
We can call: mySub(5, "Friday") --parameter positions are important or
Use named parameters: mySub(arg2:="Friday", arg1:=5) --parameter positions are not important
Optional Arguments
Edit
Sub mySub(arg1 As Integer, Optional arg2 As String) 'without default value
Sub mySub(arg1 As Integer, Optional arg2="Holiday" As String) 'with default value