FANDOM


  • 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

TypesEdit

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

SpecialEdit

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 LogicEdit

If StatementEdit

If <condition> Then
ElseIf <condition> Then
Else
End If

For LoopEdit

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 LoopEdit

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 StatementEdit

Select Case <expression>

Case [value]
Case [multi values: 2, 5, 7]
Case [range: 2 To 7]
Case [comparison: val > 10]
Case Else

End Select

AssignmentEdit

  • Use = to assign to a variable or constant
  • Use of Let is optional
  • Use Set to assign object

CommentEdit

  • Start with apostrophe(') or Rem

Line ContinuationEdit

  • Use underscore(_)

String ConcatenationEdit

  • Use ampersand(&)

Function and SubroutineEdit

FunctionEdit

  • 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

SubroutineEdit

  • Does not return any value
  • Call: mySub arg1, arg2 or Call mySub(arg1,arg2)

Named ArgumentsEdit

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 ArgumentsEdit

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

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.