Erreur dans code VBA

Bonjour,

Je débute en programmation et j'ai fait un exercice tiré d'un bouquin.

Et si je vous écrit c'est que cela ne fonctionne pas

Voilà ce que j'ai tapé :

Sub obtenirPrix()

Dim Pièce As Variant

Dim Prix As Double

Pièce = InputBox("No de pièce SVP")

Prix = WorksheetFunction.VLookup(Pièce, Range("ListePrix"), 2, False)

MsgBox "La pièce" & Pièce & "coûte" & Prix & "€"

End Sub

Dans la colonne A j'ai la liste des pièces

Dans la colonne B le Prix

Le message N° de pièce SVP apparaît bien mais après bug...

Qui peut me dire ce qui ne va pas SVP ?

Merci

Edit Dan : modifié titre du sujet

Bonjour

A tester.

Option Explicit
Public Sub obtenirPrix()
Dim Pièce
Dim Prix As Variant
    Pièce = InputBox("No de pièce SVP", "Recherche prix")
    If Pièce = "" Then Exit Sub
    On Error Resume Next
    Prix = Application.VLookup(Pièce, Range("ListePrix"), 2, False)
    If IsError(Prix) Then
        MsgBox "La référence n'existe pas"
    Else
        MsgBox "La pièce " & Pièce & " coûte " & Prix & " €"
    End If
End Sub

Merci grandement Jean Eric,

Cela marche après cette petite modification :

Public Sub obtenirPrix()

Dim Pièce

Dim Prix As Variant

Pièce = InputBox("No de pièce SVP", "Recherche prix")

If Pièce = "" Then Exit Sub

On Error Resume Next

Prix = Application.VLookup(Pièce, Range("a:b"), 2, False)

If IsError(Prix) Then

MsgBox "La référence n'existe pas"

Else

MsgBox "La pièce " & Pièce & " coûte " & Prix & " €"

End If

End Sub

Merci et à une prochaine fois

Rechercher des sujets similaires à "erreur code vba"