Utilisation de la fonction IF

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
 

Dranreb

XLDnaute Barbatruc
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:

youky(BJ)

XLDnaute Barbatruc
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
 

job75

XLDnaute Barbatruc
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:

Discussions similaires

Statistiques des forums

Discussions
312 158
Messages
2 085 821
Membres
102 992
dernier inscrit
KOSTIC