VBA gestion erreur

Bonjour,

dans le cadre du code suivant;

With [T_Staff].ListObject
    debut = .ListColumns("Id_Project").Range.Find(what:=Id_Project, LookIn:=xlValues, LookAt:=xlWhole).Row
    fin = WorksheetFunction.CountIf(.DataBodyRange, Id_Project)
    Set Maplageinit = .Range.Rows(debut).Resize(fin)
End With

excel me génère une erreur de type erreur exécution 91 quand aucun résultat n'est détecté

Aussi ma question est: comment géré au mieux cette erreur afin de l'éviter?

merci à vous.

bonjour,

une façon de faire

With [T_Staff].ListObject
    set debut = .ListColumns("Id_Project").Range.Find(what:=Id_Project, LookIn:=xlValues, LookAt:=xlWhole)
    if debut is nothing then
       msgbox " project " & id_project & " non trouvé" 
   else
       fin = WorksheetFunction.CountIf(.DataBodyRange, Id_Project)
        Set Maplageinit = .Range.Rows(debut.row).Resize(fin)
     'plus le reste du traitement si id_project trouvé
  end if
End With

Bonjour,

Par exemple :

Dim d As Range
With [T_Staff].ListObject
    Set d = .ListColumns("Id_Project").Range.Find(what:=Id_Project, LookIn:=xlValues, LookAt:=xlWhole)
    fin = WorksheetFunction.CountIf(.DataBodyRange, Id_Project)
    If Not d Is Nothing Then
      Set Maplageinit = .Range.Rows(d.Row).Resize(fin)
    Else
        ' ici traiter l'erreur
    End If
End With

EDIT : Grillé par h2so4

merci à vous mais si je fais cela, cela m'indique objet requis...

merci à vous mais si je fais cela, cela m'indique objet requis...

avec laquelle des 2 solutions ?

bonjour,

mon dernier post répondait à la première réponse.

néanmoins le probleme est similaire avec le nouveau code

Je vous joins un fichier demo

si valeur cherchée est un id connu comme 32 alors cela fonctionne mais si valeur exemple 30 pour un projet non présent alors cela ne marche pas

7staff.xlsm (36.46 Ko)

Tu n'as pas appliqué la modification dans ton exemple !!!!

Si tu l'appliques correctement elle fonctionne :

Private Sub UserForm_Activate()
Dim ColVisu(), LargeurCol(), Rng As Range
Dim Valeur_Cherchee, Debut As Long, Fin As Long, Debut2 As Long, Fin2 As Long, Maplage As Range
Dim d As Range
Valeur_Cherchee = 30
Valeur_Cherchee2 = "PP"

With [T_Staff].ListObject
    Set d = .ListColumns("Id_Project").Range.Find(what:=Valeur_Cherchee, LookIn:=xlValues, LookAt:=xlWhole)
    Fin = WorksheetFunction.CountIf(.DataBodyRange, Valeur_Cherchee)
    If Not d Is Nothing Then
        Set Maplage = .Range.Rows(d.Row).Resize(Fin)
        Debut = d.Row
        Set d = Maplage.Find(what:=Valeur_Cherchee2, LookIn:=xlValues, LookAt:=xlWhole)
        Fin2 = WorksheetFunction.CountIf(Maplage.Columns(5), Valeur_Cherchee2)
        If Not d Is Nothing Then
            Set Rng = Maplage.Rows(d.Row - Debut + 1).Resize(Fin2)
            ColVisu = Array(2, 3, 4, 5, 6)
            LargeurCol = Array(60, 60, 60, 30, 30)
            List_PP.ColumnCount = UBound(ColVisu) + 1
            List_PP.ColumnWidths = Join(LargeurCol, ";")
            List_PP.List = Application.Index(Rng, Evaluate("Row(1:" & Rng.Rows.Count & ")"), ColVisu)
        Else
            ' traiter l'erreur absence Valeur_Cherchee2
        End If
    Else
        ' traiter l'erreur absence Valeur_Cherchee
    End If
End With

End Sub

merci à vous pour votre retour.

en effet cela fonctionne bien et je vous en remercie.

Le fait qu'effectivement je n'avais pas appliqué If Not d Is Nothing Then aux deux endroits comme dans votre codage..

Rechercher des sujets similaires à "vba gestion erreur"