[VBA] Calc TVA HT/TTC petit prob de if

Résolu/Fermé
jpub Messages postés 43 Date d'inscription mardi 10 mai 2011 Statut Membre Dernière intervention 19 janvier 2016 - Modifié par pijaku le 15/09/2014 à 14:15
jpub Messages postés 43 Date d'inscription mardi 10 mai 2011 Statut Membre Dernière intervention 19 janvier 2016 - 17 sept. 2014 à 11:11
Bonjour,

Je viens de mettre en place un petit userform dans lequel l'utilisateur peut, en mettant un nombre, récuperer ou enlever la TVA de 5.5% ou 20%.

J'ai un petit problème de if imbriqué, si quelqu'un peut me donner une piste ?

Voici mon code en vous remerciant

Private Sub CommandButton1_Click()

'on veut le HT
If OptionButton4.Enabled Then
  If OptionButton2.Enabled Then 'HT 20%
    Label3.Caption = "HT 20%"
    Label4.Caption = Format(TextBox1.Value / 1.2, "0.0000")
    Label8.Caption = (TextBox1.Value - Label4.Caption)
    If OptionButton1.Enabled Then
      Label3.Caption = "HT 5.5%" 'HT5.5%
      Label4.Caption = Format(TextBox1.Value / 1.055, "0.0000")
      Label8.Caption = (TextBox1.Value - Label4.Caption)
      If OptionButton5.Enabled Then
        Label3.Caption = "HT " & TextBox2.Value & "%" 'HT autre
        Label4.Caption = Format(TextBox1.Value / TextBox2.Value, "0.0000")
        Label8.Caption = (TextBox1.Value - TextBox1.Value)
      End If
    End If
  End If
Else 'On veut le TTC
  If OptionButton2.Enabled Then 'TTC 20%
    Label3.Caption = "TTC 20%"
    Label4.Caption = Format(TextBox2.Value * 1.2, "0.0000")
    Label8.Caption = (Label4.Caption - TextBox2.Value)
    If OptionButton1.Enabled Then 'TTC 5.5%
      Label3.Caption = "TTC 5.5%"
      Label4.Caption = Format(TextBox2.Value * 1.055, "0.0000")
      Label8.Caption = (Label4.Caption - TextBox2.Value)
      If OptionButton5.Enabled Then
        Label3.Caption = "HT " & TextBox2.Value & "%" 'TTC autre
        Label4.Caption = Format(TextBox1.Value / TextBox2.Value, "0.0000")
        Label8.Caption = (TextBox1.Value - TextBox1.Value)
      End If
    End If
  End If
End If
End Sub

Cordialement,

Jonathan


4 réponses

pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 744
15 sept. 2014 à 14:19
Bonjour,

C'est difficilement compréhensible comme cela...
Je dirais qu'il ne faut peut être pas imbriquer les If... Comme ceci peut être :

Private Sub CommandButton1_Click()

'on veut le HT
If OptionButton4.Enabled Then
  If OptionButton2.Enabled Then 'HT 20%
    Label3.Caption = "HT 20%"
    Label4.Caption = Format(TextBox1.Value / 1.2, "0.0000")
    Label8.Caption = (TextBox1.Value - Label4.Caption)
  End If
  If OptionButton1.Enabled Then
    Label3.Caption = "HT 5.5%" 'HT5.5%
    Label4.Caption = Format(TextBox1.Value / 1.055, "0.0000")
    Label8.Caption = (TextBox1.Value - Label4.Caption)
  End If
  If OptionButton5.Enabled Then
    Label3.Caption = "HT " & TextBox2.Value & "%" 'HT autre
    Label4.Caption = Format(TextBox1.Value / TextBox2.Value, "0.0000")
    Label8.Caption = (TextBox1.Value - TextBox1.Value)
  End If
Else 'On veut le TTC
  If OptionButton2.Enabled Then 'TTC 20%
    Label3.Caption = "TTC 20%"
    Label4.Caption = Format(TextBox2.Value * 1.2, "0.0000")
    Label8.Caption = (Label4.Caption - TextBox2.Value)
  End If
  If OptionButton1.Enabled Then 'TTC 5.5%
    Label3.Caption = "TTC 5.5%"
    Label4.Caption = Format(TextBox2.Value * 1.055, "0.0000")
    Label8.Caption = (Label4.Caption - TextBox2.Value)
  End If
  If OptionButton5.Enabled Then
    Label3.Caption = "HT " & TextBox2.Value & "%" 'TTC autre
    Label4.Caption = Format(TextBox1.Value / TextBox2.Value, "0.0000")
    Label8.Caption = (TextBox1.Value - TextBox1.Value)
  End If
End If
End Sub

0
jpub Messages postés 43 Date d'inscription mardi 10 mai 2011 Statut Membre Dernière intervention 19 janvier 2016 1
Modifié par pijaku le 16/09/2014 à 12:42
Bonjour Franck,

Merci pour votre aide, j'avoue que je voulais faire compliqué pour pas grand chose, même si le if imbriqués m'auraient bien plus pour comprendre la mécanique.

J'ai donc modifié mon userform avec 4 boutons et une seule textbox

voici le code

Private Sub CommandButton1_Click()
If TextBox1 = "" Then
MsgBox "Veuillez renseigner un montant"
Exit Sub
End If
    Label3.Caption = "TTC 20%"
    Label4.Caption = Format(TextBox1.Value * 1.2, "0.0000")
    Label8.Caption = (Label4.Caption - TextBox1.Value)
End Sub

Private Sub CommandButton2_Click()
If TextBox1 = "" Then
MsgBox "Veuillez renseigner un montant"
Exit Sub
End If
    Label3.Caption = "HT 20%"
    Label4.Caption = Format(TextBox1.Value / 1.2, "0.0000")
    Label8.Caption = (Label4.Caption - TextBox1.Value)
End Sub
Private Sub CommandButton3_Click()
If TextBox1 = "" Then
MsgBox "Veuillez renseigner un montant"
Exit Sub
End If
    Label3.Caption = "TTC 5.5%"
    Label4.Caption = Format(TextBox1.Value * 1.055, "0.0000")
    Label8.Caption = (Label4.Caption - TextBox1.Value)
End Sub

Private Sub CommandButton4_Click()
If TextBox1 = "" Then
MsgBox "Veuillez renseigner un montant"
Exit Sub
End If
    Label3.Caption = "HT 20%"
    Label4.Caption = Format(TextBox1.Value / 1.055, "0.0000")
    Label8.Caption = (Label4.Caption - TextBox1.Value)
End Sub

Private Sub UserForm_Click()

End Sub



Merci à vous,

Jonathan
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 744
16 sept. 2014 à 12:42
Je penses que ta première idée était la bonne. Lorsque tu laisse un choix d'option à l'utilisateur, le bon contrôle à utiliser est le bouton d'option.

Regarde ce classeur exemple :
https://www.cjoint.com/?DIqmSMFR7Zn

Et son code :
Option Explicit

Sub CalculTVA(NumOption As Integer)
Dim MaVal As Double
If TextBox1 = "" Then MsgBox "Veuillez renseigner un montant": Exit Sub
MaVal = CDbl(Replace(TextBox1, ".", ","))
Select Case NumOption
    Case 1 'TTC 20%
        TextBox2.Value = Format(MaVal * 1.2, "0.0000") - MaVal
    Case 2 'TTC 5,5%
        TextBox2.Value = Format(MaVal * 1.055, "0.0000") - MaVal
    Case 3 'HT 20%
        TextBox2.Value = Format(MaVal / 1.2, "0.0000") - MaVal
    Case 4 'HT 5,5%
        TextBox2.Value = Format(MaVal / 1.055, "0.0000") - MaVal
End Select
End Sub

Private Sub OptionButton1_Click()
If OptionButton1 = True Then CalculTVA 1
End Sub

Private Sub OptionButton2_Click()
If OptionButton2 = True Then CalculTVA 2
End Sub

Private Sub OptionButton3_Click()
If OptionButton3 = True Then CalculTVA 3
End Sub

Private Sub OptionButton4_Click()
If OptionButton4 = True Then CalculTVA 4
End Sub

0
jpub Messages postés 43 Date d'inscription mardi 10 mai 2011 Statut Membre Dernière intervention 19 janvier 2016 1
17 sept. 2014 à 11:11
Bonjour Franck,

Effectivement ça à l'air bien mieux.
Je vais rester sur mon code en attendant de comprendre le tiens.

je vais le travailler pour voir comme il marche et le remplacerait une fois que j'aurais bien compris. (histoire de pouvoir le modifier le cas échéant)

Merci à toi.

Jonathan,
0