No cache version.

Caching disabled. Default setting for this page:enabled (code LNG204)
If the display is too slow, you can disable the user mode to view the cached version.

vba MsgBox :les boîtes de dialogues

Les boîtes de messages, ou boîtes de dialogue, permettent non seulement d'afficher de l'information à l'écran, mais aussi de susciter une réaction chez l'utilisateur du programme.

La procédure en cours est stoppée tant que l'utilisateur n'a pas cliqué sur un des boutons de la boîte.

Syntaxe des MsgBox en vba

Variable = MsgBox("Message" [, Type] [, "Titre de la fenêtre"])

variable : variable de type entier (integer) retournée par le type bouton cliqué.

Message : chaîne de caractères qui sera affichée dans la boîte de dialogue.

Type : constante qui définit les boutons et icônes à afficher.

Titre : chaîne de caractères qui sera affichée dans la barre de titre de la boîte de dialogue.

Constantes des MsgBox en vba



Constantes icône 
Type | Valeur | Icône | Utilisation |
vbCritical | 16 | vbCritical | Erreur fatale |
vbQuestion | 32 | vbQuestion | Question |
vbQuestion | 48 | vbQuestion | Information |
vbExclamation | 64 | vbExclamation | Remarque |


Constantes boutons 
Type | Valeur | Boutons |
vbOKOnly | 0 | |
vbOKCancel | 1 | |
vbCancelRetryIgnore | 2 | |
vbYesNoCancel | 3 | |
vbYesNo | 4 | |
vbRetryCancel | 5 | |


Constantes bouton par défaut 
Type | Valeur | Bouton par défaut |
vbDefaultButton | 10 | premier bouton |
BrolPseudoTable-const-1 | 256 | deuxième bouton |
BrolPseudoTable-const-1 | 512 | troisième bouton |


Constantes boutons 
Type | Valeur | Boutons |
vbOk | 1 | OK |
vbCancel | 2 | Annuler |
vbAbort | 3 | Abandonner |
vbRetry | 4 | Réessayer |
vbIgnore | 5 | Ignorer |
vbYes | 6 | Oui |
vbNo | 7 | Non |

Exemples de MsgBox en vba

Message simple :

  1. Public Sub test()
  2. MsgBox "hello world"
  3. End Sub


  1. <input type="button" value="Tester le script" name="BtnHW" />
  2. <script language="VBScript">
  3. <!--
  4. Sub BtnHW_OnClick
  5. MsgBox "hello world"
  6. End Sub
  7. -->
  8. </script>


Message avec icône de remarque et titre :

  1. Public Sub test()
  2. MsgBox "hello world", vbExclamation, "infobrol"
  3. End Sub


  1. <input type="button" value="Tester le script" name="BtnHello" />
  2. <script language="VBScript">
  3. <!--
  4. Sub BtnHello_OnClick
  5. MsgBox "hello world", vbExclamation, "infobrol"
  6. End Sub
  7. -->
  8. </script>


  1. Private Sub Button_Supprime_Click ( )
  2. Dim Sup
  3. Sup = MsgBox("Veuillez confirmer",vbYesNo + 256 + vbCritical, "Confirmation")
  4. If Sup = vbYes Then
  5. MsgBox "Action confirmée"
  6. else
  7. MsgBox "Action annulée"
  8. End If
  9. End Sub


  1. <input type="button" value="Tester le script" name="BtnConfirm" />
  2. <script language="VBScript">
  3. <!--
  4. Sub BtnConfirm_OnClick
  5. Dim Sup
  6. Sup = MsgBox("Veuillez confirmer",vbYesNo + 256 + vbCritical, "Confirmation")
  7. If Sup = vbYes Then
  8. MsgBox "Action confirmée"
  9. else
  10. MsgBox "Action annulée"
  11. End If
  12. End Sub
  13. -->
  14. </script>


Impossible d'exécuter les codes VBScript

les boutons de tests des scripts VBA n'apparaissent pas car soit votre navigateur ne permet pas d'exécuter des scripts vba/vbs, soit ces fonctionnalités sont désactivées pour des raisons de sécurité.

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 09/06/2004, last modified the 17/03/2020
Source of the printed document:https://www.gaudry.be/en/vba-msgbox.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.