xlsvbah15.3

Attribute VB_Name = "mod3Antwoorden"

Option Explicit

' Opdracht 1: maak een collection met 5 willekeurige waarden en druk deze af in het direct venster

Sub Antwoord1()
    
    Dim Coll As Collection
    Dim i As Integer
     
    Set Coll = New Collection
    
    Coll.Add "Z"
    Coll.Add "X"
    Coll.Add "P"
    Coll.Add "R"
    Coll.Add "T"

    For i = 1 To Coll.Count
        Debug.Print Coll.Item(i)
    Next i

End Sub

' Opdracht 2: breid opdracht 1 uit door de collection te converteren naar een array

Sub Antwoord2()

    Dim Arr()
    
    Dim Coll As Collection
    Dim i As Integer
     
    Set Coll = New Collection
    
    Coll.Add "Z"
    Coll.Add "X"
    Coll.Add "P"
    Coll.Add "R"
    Coll.Add "T"

    ReDim Arr(1 To Coll.Count)

    For i = 1 To Coll.Count
        Arr(i) = Coll.Item(i)
    Next i

    For i = LBound(Arr) To UBound(Arr)
        Debug.Print Arr(i)
    Next i

End Sub

' Opdracht 3: breid opdracht 2 en sorteer de array

Sub Antwoord3()

    Dim Arr()
    
    Dim Coll As Collection
    Dim i As Integer
    Dim j As Integer
    Dim s As String
     
    Set Coll = New Collection
    
    Coll.Add "Z"
    Coll.Add "X"
    Coll.Add "P"
    Coll.Add "R"
    Coll.Add "T"

    ReDim Arr(1 To Coll.Count)

    For i = 1 To Coll.Count
        Arr(i) = Coll.Item(i)
    Next i
    
    For i = LBound(Arr) To UBound(Arr) - 1
        For j = i + 1 To UBound(Arr)
            If Arr(i) > Arr(j) Then
                s = Arr(j)
                Arr(j) = Arr(i)
                Arr(i) = s
            End If
        Next j
    Next i
    
    For i = LBound(Arr) To UBound(Arr)
        Debug.Print Arr(i)
    Next i

End Sub

' Opdracht 4: breid opdracht 3 uit door een aparte functie te maken voor het sorteren

Sub Antwoord4()

    Dim Arr()
    
    Dim Coll As Collection
    Dim i As Integer
    Dim j As Integer
    Dim s As String
     
    Set Coll = New Collection
    
    Coll.Add "Z"
    Coll.Add "X"
    Coll.Add "P"
    Coll.Add "R"
    Coll.Add "T"

    ReDim Arr(1 To Coll.Count)

    For i = 1 To Coll.Count
        Arr(i) = Coll.Item(i)
    Next i
    
    Arr = sorteerArr(Arr)

    For i = LBound(Arr) To UBound(Arr)
        Debug.Print Arr(i)
    Next i
    
End Sub

Function sorteerArr(Arr)

    Dim i As Integer
    Dim j As Integer
    Dim s As String
    
    For i = LBound(Arr) To UBound(Arr) - 1
        For j = i + 1 To UBound(Arr)
            If Arr(i) > Arr(j) Then
                s = Arr(j)
                Arr(j) = Arr(i)
                Arr(i) = s
            End If
        Next j
    Next i
    
    sorteerArr = Arr

End Function

Download hier het bestand.