IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

VBA: Input box qui s'affiche à nouveau si l'utilisateur saisit : cash_out > account_balance [XL-2016]


Sujet :

Macros et VBA Excel

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Assistant aux utilisateurs
    Inscrit en
    Novembre 2019
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Assistant aux utilisateurs
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2019
    Messages : 35
    Points : 33
    Points
    33
    Par défaut VBA: Input box qui s'affiche à nouveau si l'utilisateur saisit : cash_out > account_balance
    Bonjour,
    Je souhaite réafficher l'input box "How many cash in?" si l'utilisateur saisit un montant de cash_out supérieur à account_balance.
    Comment faire? Merci.

    Compute tax cryptocurrency.xlsm

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    Option Explicit
     
    Sub compute_tax_crypto()
     
      Worksheets("Sheet1").Activate
      Dim number_of_session As Single
      Dim cash_in As Single
      Dim profit_or_loss As Single
      Dim account_balance As Single
      Dim cash_out As Single
      Dim end_session_profit_or_loss_before_tax As Single
      Dim tax_percentage As Single
      tax_percentage = 30
      Dim end_session_profit_or_loss_after_tax As Single
      Dim i As Integer
      i = 1
      Dim irow As Single
     
      number_of_session = InputBox("How many sessions did you trade?")
      Range("A2").Value = number_of_session
     
     
      Do Until i >= number_of_session
        Cells(1, 8) = i
        i = i + 1
     
     
          cash_in = InputBox("How many cash-in?")
          Range("B2").Value = cash_in
          profit_or_loss = InputBox("How many profit or loss?")
          Range("C2").Value = profit_or_loss
          account_balance = cash_in + profit_or_loss
          Range("D2").Value = account_balance
          cash_out = InputBox("How many cash out?")
            If cash_out > account_balance Then
              Sub DefaultMsgBox()
              MsgBox ("Cash out is too high compared to account_balance! Put a smaller cash out.")
     
              Return
              Else
              End Sub
          Range("E2").Value = cash_out
          end_session_profit_or_loss_before_tax = cash_out - (cash_in * (cash_out / account_balance))
          Range("F2").Value = end_session_profit_or_loss_before_tax
              If Range("F2").Value <= 0 Then
                 Range("G2").Value = 0
                 MsgBox ("The end session profit or loss after tax is : " & 0)
                 Else
                 end_session_profit_or_loss_after_tax = (tax_percentage / 100) * end_session_profit_or_loss_before_tax
                 Range("G2").Value = end_session_profit_or_loss_after_tax
                 MsgBox ("The end session profit or loss after tax is : " & end_session_profit_or_loss_after_tax)
                 End If
     
      Loop
     
     
     
     
    End Sub

  2. #2
    Membre chevronné Avatar de mfoxy
    Homme Profil pro
    Automation VBA
    Inscrit en
    Février 2018
    Messages
    752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Automation VBA
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2018
    Messages : 752
    Points : 1 971
    Points
    1 971
    Par défaut
    Bonjour,

    Bien que peu orthodoxe, une boucle tant que X inférieur à Y, MsgBox erreur, Inputbox pour récupérer Y.

    Au vu de votre code actuel, je vous conseillerai aussi la lecture de ce petit tutoriel sur les bases du langage vba.

    https://mhubiche.developpez.com/vba/...yntaxes/bases/

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Assistant aux utilisateurs
    Inscrit en
    Novembre 2019
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Assistant aux utilisateurs
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2019
    Messages : 35
    Points : 33
    Points
    33
    Par défaut
    Si j'ai bien compris, pour la forme, je dois enlever les parenthèses de MsgBox?
    Nom : Capture.PNG
Affichages : 154
Taille : 3,2 Ko

    Pour le fond, je vois bien qu'il faut procéder de manière suivante:
    1: boucle tant que x inférieur à y [ou tant que account_balance < cash_out]
    2: msgbox erreur
    3: inputbox pour récupérer y

    MAIS JE NE SAIS PAS LE CODER..

  4. #4
    Nouveau membre du Club
    Homme Profil pro
    Assistant aux utilisateurs
    Inscrit en
    Novembre 2019
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Assistant aux utilisateurs
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2019
    Messages : 35
    Points : 33
    Points
    33
    Par défaut
    J'ai essayé de modifier le fond.
    En respectant le schéma:
    1: boucle tant que x<y (account balance<cash in)
    2: ouvrir msg box
    3: rappeler input box x(cash in)

    Mais cela ne marche pas. Cela se répète sans fin.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    Option Explicit
     
    Sub compute_tax_crypto()
     
      Worksheets("Sheet1").Activate
      Dim number_of_session As Single
      Dim cash_in As Single
      Dim profit_or_loss As Single
      Dim account_balance As Single
      Dim cash_out As Single
      Dim end_session_profit_or_loss_before_tax As Single
      Dim tax_percentage As Single
      tax_percentage = 30
      Dim end_session_profit_or_loss_after_tax As Single
      Dim i As Integer
      i = 1
      Dim irow As Single
     
      number_of_session = InputBox("How many sessions did you trade?")
      Range("A2").Value = number_of_session
     
     
      Do Until i >= number_of_session
        Cells(1, 8) = i
        i = i + 1
     
     
          cash_in = InputBox("How many cash-in?")
          Range("B2").Value = cash_in
          profit_or_loss = InputBox("How many profit or loss?")
          Range("C2").Value = profit_or_loss
          account_balance = cash_in + profit_or_loss
          Range("D2").Value = account_balance
          cash_out = InputBox("How many cash out?")
              While account_balance < cash_out
                    MsgBox ("Error! Put a cash out inferior to account balance")
                    InputBox ("How many cash out?")
                    If cash_out <= account_balance Then break
     
          Range("E2").Value = cash_out
          end_session_profit_or_loss_before_tax = cash_out - (cash_in * (cash_out / account_balance))
          Range("F2").Value = end_session_profit_or_loss_before_tax
              If Range("F2").Value <= 0 Then
                 Range("G2").Value = 0
                 MsgBox "The end session profit or loss after tax is : " & 0
                 Else
                 end_session_profit_or_loss_after_tax = (tax_percentage / 100) * end_session_profit_or_loss_before_tax
                 Range("G2").Value = end_session_profit_or_loss_after_tax
                 MsgBox "The end session profit or loss after tax is : " & end_session_profit_or_loss_after_tax
                 End If
     
      Loop

  5. #5
    Membre chevronné Avatar de mfoxy
    Homme Profil pro
    Automation VBA
    Inscrit en
    Février 2018
    Messages
    752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Automation VBA
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2018
    Messages : 752
    Points : 1 971
    Points
    1 971
    Par défaut
    Re,

    Suis sur mobile et ne saurais donc pas poster un code testé, pour ton cas précis.

    Un petit exemple, tout de même, pour te mettre sur la route.

    On va tester un code postal :

    Condition :
    Tant que le code postal ne sera pas entièrement numeric et comportera bien 4 chiffres (Belgique)

    Colle le code ci-dessous dans un New module pour test

    À toi ensuite de modifier pour correspondre à ton besoin, puis tester, et ensuite ajouter à ta procédure


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Sub InputBoxDemo()
        Dim postcode As String
        Do
           postcode = VBA.InputBox("please enter into the postcode(4 digits)", "my InputBoxDemo")
        Loop Until VBA.Len(postcode) = 4 And VBA.IsNumeric(postcode)
        MsgBox "Le CP est : " & postcode
    End Sub

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Assistant aux utilisateurs
    Inscrit en
    Novembre 2019
    Messages
    35
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Assistant aux utilisateurs
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2019
    Messages : 35
    Points : 33
    Points
    33
    Par défaut
    Merci, j'ai réussi à presque faire ce que je voulais, il ne manque plus qu'à afficher un msg box d'erreur quand le cash_out >account_balance!
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    Option Explicit
     
    Sub compute_tax_crypto()
     
      Worksheets("Sheet1").Activate
      Dim number_of_session As Single
      Dim cash_in As Single
      Dim profit_or_loss As Single
      Dim account_balance As Single
      Dim cash_out As Single
      Dim end_session_profit_or_loss_before_tax As Single
      Dim tax_percentage As Single
      tax_percentage = 30
      Dim end_session_profit_or_loss_after_tax As Single
      Dim i As Integer
      i = 1
      Dim irow As Single
     
      number_of_session = InputBox("How many sessions did you trade?")
      Range("A2").Value = number_of_session
     
     
      Do Until i >= number_of_session
        Cells(1, 8) = i
        i = i + 1
     
     
          cash_in = InputBox("How many cash-in?")
          Range("B2").Value = cash_in
          profit_or_loss = InputBox("How many profit or loss?")
          Range("C2").Value = profit_or_loss
          account_balance = cash_in + profit_or_loss
          Range("D2").Value = account_balance
          Do
            cash_out = VBA.InputBox("How many cash out?")
          Loop Until cash_out <= account_balance
          MsgBox "The amount of cash out is under amount of account balance, great!"
     
     
     
          Range("E2").Value = cash_out
          end_session_profit_or_loss_before_tax = cash_out - (cash_in * (cash_out / account_balance))
          Range("F2").Value = end_session_profit_or_loss_before_tax
              If Range("F2").Value <= 0 Then
                 Range("G2").Value = 0
                 MsgBox "The end session profit or loss after tax is : " & 0
                 Else
                 end_session_profit_or_loss_after_tax = (tax_percentage / 100) * end_session_profit_or_loss_before_tax
                 Range("G2").Value = end_session_profit_or_loss_after_tax
                 MsgBox "The end session profit or loss after tax is : " & end_session_profit_or_loss_after_tax
                 End If
     
      Loop
     
     
     
     
    End Sub

  7. #7
    Membre chevronné Avatar de mfoxy
    Homme Profil pro
    Automation VBA
    Inscrit en
    Février 2018
    Messages
    752
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Belgique

    Informations professionnelles :
    Activité : Automation VBA
    Secteur : High Tech - Opérateur de télécommunications

    Informations forums :
    Inscription : Février 2018
    Messages : 752
    Points : 1 971
    Points
    1 971
    Par défaut
    Re,

    Il te suffit d'ajouter ton MsgBox et condition dans ta boucle ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
      Do
            cash_out = VBA.InputBox("How many cash out?")
            If cash_out > account_balance then MsgBox "ton message" 
          Loop Until cash_out <= account_balance

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AC-2013] Une zone de texte qui m'affiche le nom d'utilisateur d'access
    Par othmane.badri dans le forum IHM
    Réponses: 3
    Dernier message: 29/03/2019, 09h23
  2. Réponses: 3
    Dernier message: 13/10/2017, 12h06
  3. Download dialog box qui ne s'affiche pas
    Par Gaaaga dans le forum ActionScript 3
    Réponses: 1
    Dernier message: 03/06/2009, 11h18
  4. texte d'un input qui s'affiche quand on le renseigne
    Par identifiant_bidon dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 18/10/2007, 17h28
  5. [VBA-E] Saisie d'une date dans une Input Box
    Par nicobox dans le forum Macros et VBA Excel
    Réponses: 15
    Dernier message: 31/05/2006, 20h51

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo