← Introduction au VBA
Leçon 1 / 10 — Chapitre 9: Mise en pratique : automatiser la gestion associative
Calculer la cotisation selon la catégorie de membre
Cette leçon combine une Function et une boucle pour calculer automatiquement le montant de cotisation dû par chaque membre, selon sa catégorie d'adhésion.
Function CalculerCotisation(categorie As String) As Double
If categorie = "Famille" Then
CalculerCotisation = 80
ElseIf categorie = "Étudiant" Then
CalculerCotisation = 20
Else
CalculerCotisation = 40
End If
End Function
Sub AppliquerCotisations()
Dim i As Integer
For i = 2 To 15
Cells(i, 3).Value = CalculerCotisation(Cells(i, 2).Value)
Next i
End Sub
La Function centralise les trois tarifs (Famille, Étudiant, Individuel par défaut), et la boucle l'applique à chaque ligne de la liste des membres, en lisant la catégorie en colonne B et en écrivant le montant en colonne C.
À vous de jouer : adaptez cette Function pour ajouter une catégorie "Bienfaiteur" à 150.
À vous de jouer
À faireVoici un extrait de code, où B4 = 'Étudiant' :
Function CalculerCotisation(categorie As String) As Double
If categorie = "Famille" Then
CalculerCotisation = 80
ElseIf categorie = "Étudiant" Then
CalculerCotisation = 20
Else
CalculerCotisation = 40
End If
End Function
Sub Test()
MsgBox CalculerCotisation(Cells(4, 2).Value)
End Sub
Que va afficher le MsgBox lors de l'exécution ?
Réalisez la tâche, puis saisissez votre réponse ci-dessous pour vérification.