Branchements conditionnels en VBA
Dans cette vidéo on montre l'utilisation des instructions suivantes:
If Then End If
If Then Else End If
IIF
If Then ElseIf Else End If
Select Case end select
Code source:
Option Explicit
Sub test1()
Dim i As Integer
i = WorksheetFunction.RandBetween(0, 100)
If i Mod 2 = 0 Then Beep: Debug.Print i & " est paire"
End Sub
Sub test2()
Dim i As Integer
i = WorksheetFunction.RandBetween(0, 100)
If i Mod 2 = 0 Then
Debug.Print i & " est paire"
Else
Debug.Print i & " est impaire"
End If
End Sub
Sub test3()
Dim i As Integer
i = WorksheetFunction.RandBetween(0, 100)
Debug.Print i & " est " & IIf(i Mod 2 = 0, "paire", "impaire")
End Sub
Sub test4()
Dim i As Integer
i = WorksheetFunction.RandBetween(0, 100)
Debug.Print i & " est " & IIf(i Mod 2 = 0, "", "im") & "paire"
End Sub
Sub test5()
Dim i
i = InputBox("Taper un nombre entre 0 et 100")
If IsNumeric(i) Then
Debug.Print i & " est " & IIf(i Mod 2 = 0, "", "im") & "paire"
Else
MsgBox "Taper un nombre entre 0 et 100"
End If
End Sub
Function IGR(revenu As Currency) As Currency
'Revenu annuel
If revenu < 30000 Then
IGR = 0
ElseIf revenu >= 30001 And revenu <= 50000 Then
IGR = revenu * 0.1
ElseIf revenu >= 50001 And revenu <= 60000 Then
IGR = revenu * 0.2
ElseIf revenu >= 60001 And revenu <= 80000 Then
IGR = revenu * 0.3
ElseIf revenu >= 80001 And revenu <= 180000 Then
IGR = revenu * 0.34
Else
IGR = revenu * 0.38
End If
End Function
Sub test6()
Dim salairebrut As Currency
salairebrut = 50001
Debug.Print "Salairebrut=" & salairebrut & ",IGR=" & IGR2(salairebrut) & _
",Reste=" & salairebrut - IGR2(salairebrut) & ",Mensuel Net=" & _
(salairebrut - IGR2(salairebrut)) / 12 & " DH"
End Sub
Function IGR2(revenu As Currency) As Currency
'Revenu annuel
Select Case revenu
Case Is < 30000
IGR2 = 0
Case 30000 To 50000
IGR2 = revenu * 0.1
Case 50000 To 60000
IGR2 = revenu * 0.2
Case 60000 To 80000
IGR2 = revenu * 0.3
Case 80000 To 180000
IGR2 = revenu * 0.34
Case Else
IGR2 = revenu * 0.38
End Select
End Function
Commentaires