Utilisation de la fonction IF

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

Shirazedi

XLDnaute Nouveau
Bonjour a tous ! Je reviens une fois de plus sur ce formidable forum pour vous demander un conseil 🙂

J'ai cree un macro ayant pour objectif d'afficher une boite de dialogue selon le caractere (chiffres, lettres) rentree dans une dite cellule. Dans mon macro j'ai inscrit plusieurs conditions a l'aide de "If" et tout fonctionne.

Cependant lorsque la cellule est vide, le message "There is no value in the cell" doit s'afficher. Cela fonctionne mais ensuite, une seconde boite de dialogue apparait affichant "Your entry ... is not valid number !"

Je n'arrive pas a resoudre ce leger soucis et c'est pourquoi je vous demande de l'aide. Voici mon macro :

Sub Variables()

If IsEmpty(Range("G7")) Then
MsgBox "There is no value in the cell"
End If

If IsNumeric(Range("G7")) Then

Dim Nom As String, Prenom As String, Age As Integer, Numero As Integer
Numero = Range("G7") + 1

If Numero >= 2 And Numero <= 32 Then
Nom = Cells(Numero, 2)
Prenom = Cells(Numero, 3)
Age = Cells(Numero, 4)
MsgBox Nom & " " & Prenom & " has " & Age & " years"

Else
MsgBox "Your entry " & Range("G7") & " is not a valid number !"
Range("G7").ClearContents
End If

Else
MsgBox "Your entry " & Range("G7") & " is not valid ! You have to enter a number !"
Range("G7").ClearContents
End If

End Sub
 
Bonjour
IsEmpty(Range("G7")) est toujours False vu que TypeName(Range("G7")) = "Range" et jamais "Empty".
IsEmpty(Range("G7").Value) sera True si la cellule est vide.
Enfin je crois…
N'ayant pas de classeur joint je ne peux pas vérifier.
À votre place je ferais un Select Case VarType(Range("G7").Value)
avec des: Case vbEmpty: … Case vbDouble: … Case vbString: … Case vbError: … End Select
 
Dernière édition:
Bonjour tous, j'arrive à la bourre.
A voir si cela conviens ,vu que c'est fait je mets....
Bruno
VB:
Sub Variables()
Dim Nom As String, Prenom As String, Age As Integer, Numero As Integer
If [G7] = "" Then MsgBox "There is no value in the cell": Exit Sub

If Not IsNumeric([G7]) Or [G7] < 1 Or [G7] > 30 Then
MsgBox "Your entry " & [G7] & " is not a valid number !"
Range("G7").ClearContents
Exit Sub
End If

Numero = [G7] + 1
Nom = Cells(Numero, 2)
Prenom = Cells(Numero, 3)
Age = Cells(Numero, 4)
MsgBox Nom & " " & Prenom & " has " & Age & " years"
End Sub
 
Bonjour Shirazedi, Bernard, [Edit] Bruno,
IsEmpty(Range("G7")) est toujours False vu que TypeName(Range("G7")) = "Range" et jamais "Empty".
Qu'est-ce qu'il t'arrive Bernard, un coup de mou ??? Suffit de tester...
Code:
Sub Variables()
Dim Numero As Long, Nom As String, Prenom As String, Age As Integer
Numero = Val(CStr(Range("G7"))) + 1
If IsEmpty(Range("G7")) Then
    MsgBox "There is no value in the cell"
ElseIf IsNumeric(Range("G7")) And Numero >= 2 And Numero <= 32 Then
    Nom = CStr(Cells(Numero, 2))
    Prenom = CStr(Cells(Numero, 3))
    Age = Val(CStr(Cells(Numero, 4)))
    MsgBox Nom & " " & Prenom & " has " & Age & " years"
Else
    MsgBox "Your entry " & CStr(Range("G7")) & " is not a valid number !"
    Range("G7").ClearContents
End If
End Sub
Les CStr permettent d'éviter un bug s'il y a des valeurs d'erreur dans les cellules (c'est classique).

A+
 
Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
4
Affichages
223
  • Question Question
Microsoft 365 worksheet_change
Réponses
29
Affichages
479
Réponses
2
Affichages
153
Réponses
4
Affichages
177
  • Question Question
Microsoft 365 Probléme VBA
Réponses
8
Affichages
318
Retour