Tutorial by Examples

Select Case can be used when many different conditions are possible. The conditions are checked from top to bottom and only the first case that match will be executed. Sub TestCase() Dim MyVar As String Select Case MyVar 'We Select the Variable MyVar to Work with Case "...
The For Each loop construct is ideal for iterating all elements of a collection. Public Sub IterateCollection(ByVal items As Collection) 'For Each iterator must always be variant Dim element As Variant For Each element In items 'assumes element can be converted to a stri...
Public Sub DoLoop() Dim entry As String entry = "" 'Equivalent to a While loop will ask for strings until "Stop" in given 'Prefer using a While loop instead of this form of Do loop Do While entry <> "Stop" entry = InputBox("...
'Will return whether an element is present in the array Public Function IsInArray(values() As String, ByVal whatToFind As String) As Boolean Dim i As Integer i = 0 While i < UBound(values) And values(i) <> whatToFind i = i + 1 Wend IsInArray = valu...
The For loop is used to repeat the enclosed section of code a given number of times. The following simple example illustrates the basic syntax: Dim i as Integer 'Declaration of i For i = 1 to 10 'Declare how many times the loop shall be executed Debug.Print i 'Th...

Page 1 of 1