VBA / InputBox

baissaoui Messages postés 598 Date d'inscription   Statut Webmaster Dernière intervention   -  

<style>@media(max-device-width: 767px) { .ccmPlayerTextTitle {display: none;} } #ccmPlayer-686806 .ccmPlayerPlaceHolder {position: absolute; width: 100%; height: 100%; flex-direction: column; justify-content: center; display: flex; text-align: center; ; color:#474a4f;} .ccmPLayerLoaderBox {box-sizing: border-box; height: 59px; width: 59px; margin: 5px auto;} .ccmPlayerTextDefault {top: -10px; position: relative;} .ccmPlayerTextDefault:before {content: "Chargement de votre vidéo";} #ccmPlayer-686806 .ccmPLayerLoader{color:#fff;position:relative;box-sizing:border-box;left:-9999px;top:-9999px;width:0;height:0;overflow:hidden;}#ccmPlayer-686806 .ccmPLayerLoader:after,#ccmPlayer-686806 .ccmPLayerLoader:before{box-sizing:border-box;display:none}#ccmPlayer-686806 .ccmPLayerLoader.is-active{width:100%;height:100%;left:0;top:0;position:relative;}#ccmPlayer-686806 .ccmPLayerLoader.is-active:after,#ccmPlayer-686806 .ccmPLayerLoader.is-active:before{display:block}@keyframes rotation{0%{transform:rotate(0)}to{transform:rotate(359deg)}}@keyframes blink{0%{opacity:.5}to{opacity:1}}#ccmPlayer-686806 .ccmPLayerLoader[data-text]:before{position:relative;left:0;top:50%;color:currentColor;font-family:Helvetica,Arial,sans-serif;text-align:center;width:100%;font-size:14px}#ccmPlayer-686806 .ccmPLayerLoader[data-text=""]:before{content:"Loading"}#ccmPlayer-686806 .ccmPLayerLoader[data-text]:not([data-text=""]):before{content:attr(data-text)}#ccmPlayer-686806 .ccmPLayerLoader[data-text][data-blink]:before{animation:blink 1s linear infinite alternate}#ccmPlayer-686806 .ccmPLayerLoader-default[data-text]:before{top:calc(50% - 63px)}#ccmPlayer-686806 .ccmPLayerLoader-default:after{content:"";position:relative;width:48px;height:48px;border:8px solid #474a4f;border-left-color:transparent;border-radius:50%;top:calc(50% - 24px);left:calc(50% - 24px);animation:rotation 1s linear infinite}#ccmPlayer-686806 .ccmPLayerLoader-default[data-half]:after{border-right-color:transparent}#ccmPlayer-686806 .ccmPLayerLoader-default[data-inverse]:after{animation-direction:reverse} .ccmPlayer#ccmPlayer-686806 {position:absolute;width:100%;height:100%; background-color:#dcdcdc; background-size: cover; flex-direction: column; justify-content: center; display: flex; text-align: center; font-family: Roboto, sans-serif; font-size: 20px; font-weight: bold; line-height: 26px; color: #474a4f;}</style>
"FAQ : VBA / InputBox"
<script type="application/ld+json">{"@context":"http:\/\/schema.org","@type":"VideoObject","name":"FAQ : VBA \/ InputBox","description":"FAQ : VBA \/ InputBox","thumbnailUrl":"https:\/\/media.ccmbg.com\/tc\/9757252658\/686806\/1613548499","uploadDate":"2021-02-17T08:54:59+01:00","contentUrl":"https:\/\/media.ccmbg.com\/vc\/1613548499\/9757252658\/686806.mp4","duration":"PT1M51S","embedUrl":"https:\/\/media.ccmbg.com\/media\/?format=embed&rid=686806&rkey=9757252658&site=ccm&startMode=0"}</script>

Dim Nom As String
Nom = InputBox("Saisie de votre NOM : ")

Dim Nom As String
Nom = InputBox("Saisie de votre NOM : ", "NOM")

Dim Nom As String
Nom = InputBox("Saisie de votre NOM : ", "NOM", "DUPOND")

Dim Nom As String
Nom = InputBox("Saisie de votre NOM : ", "NOM", "DUPOND", 100, 100)

Dim Nom As String
Nom = InputBox("Saisie de votre NOM : ", "NOM", , , , "c:Help.chm", 0)

Dim Nom As String
Nom = InputBox("Saisie de votre NOM : ", "NOM", "DURAND", 200, 200, "DEMO.HLP", 10)

Dim Nombre As Integer 
Dim Message As String, Titre As String, ParDefaut As String, PosX As Integer, PosY As Integer, Aide As String, NumAide As Long
Message = "Entrez un nombre entier : "
Titre = "Saisie numérique"
ParDefaut = "13"
PosX = 250
PosY = 360
Aide = "c:Help.chm"
NumAide = 3152
Nombre = InputBox(Message, Titre, ParDefaut, PosX, PosY, Aide, NumAide)

MsgBox Nombre * 2

Dim Nombre As Integer ' Ici on sait que l'on demande un Integer
Nombre = InputBox("Entrez un nombre entier : ", "Saisie numérique")

Dim Nombre As Integer
'En cas d'erreur, on va à l'étiquette MauvaisType
On Error GoTo MauvaisType 
Nombre = InputBox("Entrez un nombre entier : ", "Saisie numérique")

'Ici le traitement souhaité, par exemple
Select Case Nombre
    Case Is >= 18: MsgBox "Mention très bien"
    Case Is >= 16: MsgBox "Mention bien"
    Case Is >= 14: MsgBox "Mention passable"
    Case Is >= 10: MsgBox "Pas de mention"
    Case Is >= 8: MsgBox "Mention pas bien"
    Case Is >= 6: MsgBox "Mention pas bien du tout"
    Case Else: MsgBox "Mention très nul"
End Select
Exit Sub 'Sortie à ne pas oublier pour ne pas traiter MauvaisType

MauvaisType:
MsgBox "Vous n'avez pas saisi un nombre entier", vbCritical

InputBox("Entrez un nombre entier : ", "Saisie numérique")

Dim Nom As String
Nom = InputBox("Entrer votre NOM :", "Saisie NOM")
Do While Len(Nom) = 0
    MsgBox "Cette donnée est obligatoire"
    Nom = InputBox("Entrer votre NOM :", "Saisie NOM")
Loop
MsgBox "Votre NOM est :" & Nom

Dim Nom As String, Cpt As Integer
Cpt = 1
Nom = InputBox("Entrer votre NOM :", "Saisie NOM")
Do While Len(Nom) = 0
    Cpt = Cpt + 1
    If Cpt = 4 Then GoTo TropBetePourContinuer
    MsgBox "Cette donnée est obligatoire. Plus que " & 4 - Cpt & " essais."
    Nom = InputBox("Entrer votre NOM :", "Saisie NOM")
Loop
MsgBox "Votre NOM est :" & Nom
Exit Sub
TropBetePourContinuer:
MsgBox "Veuillez arrêter l'informatique."

Valeur Signification
0 Formule
1 Nombre
2 Texte (Chaîne)
4 Valeur logique (True ou False)
8 Référence de cellule, sous la forme d'un objet Range
16 Valeur d'erreur telle que #n/a
64 Matrice de valeurs
 

Dim Nombre As Integer
Nombre = Application.InputBox("Entrez un nombre entier : ", "Saisie numérique", Type:=1)

Dim Plage As Range
      Set Plage = Application.InputBox("Sélectionnez une plage", "Sélection de cellules", Type:=8)
      MsgBox ("La plage que vous avez sélectionné est : " & Plage.Address)

Dim MonTab()As String
Dim Plage As Range
Dim i As Integer

Set Plage = Application.InputBox("Sélectionnez une plage", "Sélection de cellules", Type:=8)
MonTab = Split(Plage.Address, ",")
For i = 0 To UBound(MonTab)
    MsgBox MonTab(i)
Next i

Cet exemple utilise une méthode InputBox pour permettre à l'utilisateur de sélectionner une plage à transmettre à la fonction « MaFonction » définie par l'utilisateur, qui multiplie trois valeurs d'une plage et renvoie le résultat.
Sub Cbm_Value_Select()
   Dim rng As Range
   
   Set rng = Application.InputBox("Range:", Type:=8)
   If rng.Cells.Count <> 3 Then
     MsgBox "Length, width and height are needed -" & _
         vbLf & "please select three cells!"
      Exit Sub
   End If
   
   'Appel MyFunction
   ActiveCell.Value = MyFunction(rng)
End Sub


Function MyFunction(rng As Range) As Double
   MyFunction = rng(1) * rng(2) * rng(3)
End Function

  • ne doit comporter qu'une ligne ou une colonne. A éviter : Range("A1:B12")...
  • ne doit pas comporter de cellule vide ==> message d'abandon

  • clic sur OK sans saisie ==> relance l'inputbox
  • clic sur annuler ==> retourne le message d'annulation
  • mauvaise saisie ==> relance l'inputbox

  • Wsh (Worksheet): Feuille ou sont situées les données permettant la vérification de la saisie
  • rngValues (String) : l'adresse du Range ou sont situées les données (soit sur 1 ligne, soit sur 1 colonne)
  • ForceMajuscules (Boolean) : si True transforme les saisies en Majuscules
  • strMessage (String / Optional) : le message d'abandon par l'utilisateur

Private Function Funct_MyInputBox(Wsh As Worksheet, rngValues As String, Optional ForceMajuscules As Boolean, Optional strMessage As String) As String
'Fonction utilisant l'inputbox.
    'clic sur OK sans saisie ==> relance l'inputbox
    'clic sur annuler ==> retourne le message d'annulation
    'mauvaise saisie ==> relance l'inputbox
'PARAMETRES :
    'Wsh (Worksheet): Feuille ou sont situées les données permettant la vérification de la saisie
    'rngValues (String) : l'adresse du Range ou sont situées les données (soit sur 1 ligne, soit sur 1 colonne)
    'ForceMajuscules (Boolean) : si True transforme les saisies en Majuscules
    'strMessage (String / Optional) : le message d'abandon par l'utilisateur
'ATTENTION : utilise la fonction Funct_Dimension

Dim bon As Boolean, entree As String, MyArray As Variant, i As Long, Dimens As Byte, Valeur As String

    MyArray = Wsh.Range(rngValues).Value
    If UBound(MyArray, 1) > 1 And UBound(MyArray, 2) > 1 Then
        MsgBox "Plage non valide (toléré : 1 ligne ou 1 colonne)"
        Funct_MyInputBox = strMessage: Exit Function
    End If
    Dimens = Funct_Dimension(MyArray)
    Do While Not bon
        entree = InputBox("Enter your Country code", "Country code")
        If StrPtr(entree) = 0 Then Funct_MyInputBox = strMessage: Exit Function
        If entree <> "" Then
            entree = IIf(ForceMajuscules, UCase(entree), entree)
            If Dimens = 1 Then
                For i = LBound(MyArray, Dimens) To UBound(MyArray, Dimens)
                    Valeur = IIf(ForceMajuscules, UCase(MyArray(i, 1)), MyArray(i, 1))
                    If Valeur = "" Then Funct_MyInputBox = strMessage: Exit Function
                    If Valeur = entree Then
                        bon = True: Exit For
                    End If
                Next i
            Else
                For i = LBound(MyArray, Dimens) To UBound(MyArray, Dimens)
                    Valeur = IIf(ForceMajuscules, UCase(MyArray(1, i)), MyArray(1, i))
                    If Valeur = "" Then Funct_MyInputBox = strMessage: Exit Function
                    If Valeur = entree Then
                        bon = True: Exit For
                    End If
                Next i
            End If
        End If
    Loop
    Funct_MyInputBox = IIf(ForceMajuscules, UCase(entree), entree)
End Function

Private Function Funct_Dimension(Arr As Variant) As Byte
'retourne la dimension la plus grande de l'Array Arr
'si pas de "plus grande" dimension retourne 1 par défaut
    Select Case True
        Case UBound(Arr, 1) > UBound(Arr, 2)
            Funct_Dimension = 1
        Case UBound(Arr, 2) > UBound(Arr, 1)
            Funct_Dimension = 2
        Case Else
            Funct_Dimension = 1
    End Select
End Function

Sub AppelInputBox()
Dim Feuille As Worksheet, MyString As String

    Const strRANGE As String = "B1:F1"                          'A ADAPTER
    Const MSG_ANNULE As String = "Annulation utilisateur..."    'A ADAPTER
    Set Feuille = Worksheets("Feuil1")                          'A ADAPTER
    
    MyString = Funct_MyInputBox(Feuille, strRANGE, True, MSG_ANNULE)
    If MyString = MSG_ANNULE Then
        MsgBox MSG_ANNULE: Exit Sub
    Else
        MsgBox MyString
    End If
End Sub