Passer à un nouvel onglet si methode "find" ne retourne rien

yuplop

XLDnaute Nouveau
Bonjour,

Tout est dans le titre en fait.
Je dispose d'un classeur qui doit faire une recherche dans un second (qui dispose de 3 onglets).
Je parcours la première colonne pour trouver le nom du client. Une fois le nom du client trouvé, je fait une recherche dans la seconde colonne pour trouver le nom de la facture associée.
Pour l'instant, ça fonctionne sur mon premier onglet:
Code:
dateref = ThisWorkbook.Worksheets("Clients").Range("B17")
Workbooks.Open "C:\Users\XXXX\Desktop\Ent\TABLEAUX AVANCEMENT PAIEMENT.xlsm"
y = Year(dateref)

Set an = ActiveWorkbook.Worksheets("Prévisionnel " & y)
Set ann = ActiveWorkbook.Worksheets("Prévisionnel " & y + 1)
Set annn = ActiveWorkbook.Worksheets("Prévisionnel " & y + 2)

With an.Range("A1:A5000")
    Set c = .Find(Copie(4), LookIn:=xlValues)
    col = c.Row
End With

Set MaPlage = Cells(col, 1)
plop = MaPlage.MergeArea.Cells(MaPlage.MergeArea.Cells.Count).Row

With an.Range(Cells(col, 2), Cells(plop, 2))
    Set b = .Find(Copie(5), LookIn:=xlValues)
    coll = b.Row
End With

Sur le code ci-dessus, je récupère le n° de ligne de la facture voulu du client

Je voudrais faire en sorte que si il ne trouve pas la facture correspondante dans le premier onglet, il passe au second (nommé "ann") et au troisième (nommé "annn") si on ne retrouve pas la facture dans le second.

J'ai tenté:
Code:
dateref = ThisWorkbook.Worksheets("Clients").Range("B17")
Workbooks.Open "C:\Users\XXXX\Desktop\Ent\TABLEAUX AVANCEMENT PAIEMENT.xlsm"
y = Year(dateref)

Set an = ActiveWorkbook.Worksheets("Prévisionnel " & y)
Set ann = ActiveWorkbook.Worksheets("Prévisionnel " & y + 1)
Set annn = ActiveWorkbook.Worksheets("Prévisionnel " & y + 2)

With an.Range("A1:A5000")
    Set c = .Find(Copie(4), LookIn:=xlValues)
    col = c.Row
End With

Set MaPlage = Cells(col, 1)
plop = MaPlage.MergeArea.Cells(MaPlage.MergeArea.Cells.Count).Row

With an.Range(Cells(col, 2), Cells(plop, 2))
    Set b = .Find(Copie(5), LookIn:=xlValues)
    If b Is Nothing Then

        With ann.Range("A1:A5000")
            Set c = .Find(Copie(4), LookIn:=xlValues)
            col = c.Row
        End With
        MsgBox col
        Set MaPlage = Cells(col, 1)
        plop = MaPlage.MergeArea.Cells(MaPlage.MergeArea.Cells.Count).Row

        With ann.Range(Cells(col, 2), Cells(plop, 2))
            Set b = .Find(Copie(5), LookIn:=xlValues)
            If b Is Nothing Then

                With annn.Range("A1:A5000")
                    Set c = .Find(Copie(4), LookIn:=xlValues)
                    col = c.Row
                End With

                Set MaPlage = Cells(col, 1)
                plop = MaPlage.MergeArea.Cells(MaPlage.MergeArea.Cells.Count).Row

                With annn.Range(Cells(col, 2), Cells(plop, 2))
                    Set c = .Find(Copie(5), LookIn:=xlValues)
                    coll = c.Row
                End With
            Else: coll = b.Row
            End If
        End With
    Else: coll = b.Row
    End If
End With


yuplop
Membre habitué

Messages : 53
Inscription : 16 Mars 2017
Version Excel : 2007
 

Statistiques des forums

Discussions
312 229
Messages
2 086 426
Membres
103 206
dernier inscrit
diambote