Gestion d'erreur

Claudy

XLDnaute Accro
Bonjour,
dans ma macro ci dessous, associée à un bouton 'Valider' dans un userform, la procédure fonctionne bien , mais lorsque j'introduit un mauvais numéro dans mon textbox4, la derniere ligne de mon tableau (Range('D64000').Offset.End(xlUp)(2)) se remplit avec des erreurs....#N/A
Ce que je souhaiterais, c'est tout simplement quitter sans rien remplir avec éventuellement un Msgbox 'La facture n'existe pas!' et laisser la derniere ligne vide!
Merci d'avance,
Claudy

Private Sub CommandButton1_Click()

On Error Resume Next
'Facture mod 113 client
Range('B64000').Offset.End(xlUp)(2) = TextBox4.Value
'date facture
Range('C64000').Offset.End(xlUp)(2) = CDate(Application.VLookup(CDbl(TextBox4.Value), Range('Factures'), 4, False))
'N° CCP
Range('D64000').Offset.End(xlUp)(2) = 'CCP ' & TextBox2.Value & ' ' & TextBox3.Value
'Nom
Range('E64000').Offset.End(xlUp)(2) = Application.VLookup(CDbl(TextBox4.Value), Range('Factures'), 2, False)
'Montant
Range('F64000').Offset.End(xlUp)(2) = Application.VLookup(CDbl(TextBox4.Value), Range('Factures'), 3, False)

Unload UserForm3

End Sub
 

CBernardT

XLDnaute Barbatruc
Bonjour Claudy et le forum,

je te propose ces modifications de ton code :

Private Sub CommandButton1_Click()
Dim Lig As Integer

Lig = Range('B64000').End(xlUp).Row + 1
'Facture mod 113 client
If IsError(Application.VLookup(CDbl(TextBox4.Value), Range('Factures'), 2, False)) Then MsgBox 'La facture n'existe pas !': Exit Sub
Range('B' & Lig) = TextBox4.Value
'date facture
Range('C' & Lig) = CDate(Application.VLookup(CDbl(TextBox4.Value), Range('Factures'), 4, False))
'N° CCP
Range('D' & Lig) = 'CCP ' & TextBox2.Value & ' ' & TextBox3.Value
'Nom
Range('E' & Lig) = Application.VLookup(CDbl(TextBox4.Value), Range('Factures'), 2, False)
'Montant
Range('F' & Lig) = Application.VLookup(CDbl(TextBox4.Value), Range('Factures'), 3, False)

Unload UserForm3
End Sub

Cordialement

Bernard
 

Discussions similaires

Statistiques des forums

Discussions
312 571
Messages
2 089 804
Membres
104 276
dernier inscrit
helenevellocet