Gestion des Tableaux en VBA

Les éléments vues dans cette vidéo sont: Option Base, Array, UBound, LBound, ReDim et Preserve.


Code Source:

Option Explicit
Option Base 1

Sub test()
Dim Trimestre(4) As String, i As Integer
For i = 1 To 4
    Trimestre(i) = "Trim " & i
Next
For i = 1 To 4
    Debug.Print i & " " & Trimestre(i)
Next
End Sub

Sub test1()
Dim Trimestre(4) As String, i As Integer
For i = LBound(Trimestre) To UBound(Trimestre)
    Trimestre(i) = "Trim " & i
Next
For i = LBound(Trimestre) To UBound(Trimestre)
    Debug.Print i & " " & Trimestre(i)
Next
End Sub


Sub test2()
Dim Trimestre() As Variant, i As Integer
Trimestre = Array("Trim 1", "Trim 2", "Trim 3", "Trim 4")

For i = LBound(Trimestre) To UBound(Trimestre)
    Debug.Print i & " " & Trimestre(i)
Next

End Sub

Sub test3()
Dim Trimestre() As Variant, i As Integer
Trimestre = Array("Trim 1", "Trim 2", "Trim 3")
ReDim Trimestre(4)
Trimestre(4) = "Trim 4"
For i = LBound(Trimestre) To UBound(Trimestre)
    Debug.Print i & " " & Trimestre(i)
Next
End Sub

Sub test4()
Dim Trimestre() As Variant, i As Integer
Trimestre = Array("Trim 1", "Trim 2", "Trim 3")
ReDim Preserve Trimestre(4)
Trimestre(4) = "Trim 4"
For i = LBound(Trimestre) To UBound(Trimestre)
    Debug.Print i & " " & Trimestre(i)
Next
End Sub

Sub test5()
Dim Trimestre() As Variant, i As Integer, e As Variant
Trimestre = Array("Trim 1", "Trim 2", "Trim 3")
ReDim Preserve Trimestre(4)
Trimestre(4) = "Trim 4"
For Each e In Trimestre
    Debug.Print e
Next
End Sub

Sub test6()
Dim Trimestre() As Variant, i As Integer, e As Variant
Trimestre = Array("Trim 1", "Trim 2", "Trim 3", "Trim 4", "Trim x")
For Each e In Trimestre
    Debug.Print e
Next
'Supprimer le dernier élément
ReDim Preserve Trimestre(UBound(Trimestre) - 1)
For Each e In Trimestre
    Debug.Print e
Next
End Sub

Sub test7()
Dim Trimestre() As Variant, Mois(4) As Integer, i As Integer, e As Variant
Trimestre = Array("Trim 1", "Trim 2", "Trim 3", "Trim 4")
For i = 1 To 4
    Mois(i) = i
Next
Erase Trimestre
Erase Mois
End Sub

Commentaires

Posts les plus consultés de ce blog

Nom des services Windows en Français et en Anglais

Comment afficher le mot de passe d’une connexion wifi mémorisé sur une machine Windows

Techniques de Recherche sur google