L'indice n'appartient pas à la selection

Bonjour,

Je bloque (comme d'hab ... ) sur une erreur ... erreur d'execution 9 sur cette ligne de code :

 If Cells(2, i).Value = ThisWorkbook.Worksheets("closing").Range("A1") Then

Je ne comprends pas pourquoi ... vu que je suis une merde c'est normal ...

Pourriez-vous m'aider svp. Merci à vous.

Sub maj()

Dim nomfichier1 As String
Dim chemin1 As String
Dim wk1 As Workbook
Dim i As Byte
Dim dercolonne As Byte

Application.AskToUpdateLinks = False
Application.DisplayAlerts = False

nomfichier1 = "Oscar 2018.xlsm" 'le fichier que je veux ouvrir
chemin1 = "R:\11 - Pilotage TU\00 Oscar\New OSCAR\" 'le chemin où il se trouve

Set wk1 = Workbooks.Open(chemin1 & nomfichier1)

Sheets("closing").Range("A2").Select
dercolonne = Cells(2, Cells.Columns.Count).End(xlToLeft).Column

'Sheets("closing").Range("A2").Select

    For i = 1 To dercolonne

        If Cells(2, i).Value = ThisWorkbook.Worksheets("closing").Range("A1") Then

            Cells(2, i).Activate

        End If

    Next i

    Range(ActiveCell, ActiveCell.End(xlDown)).Copy
    Workbooks("OSCAR_2.xlsm").Worksheets("Feuil1").Range("B2").PasteSpecial Paste:=xlPasteValues
    wk1.Close False

Application.AskToUpdateLinks = True
Application.DisplayAlerts = True

End Sub

Bonsoir,

La feuille "closing" n'appartient pas pas au classeur où s'exécute le code (ThisWorkBook) mais au classeur actif (ActiveWorkbook), c'est à dire celui qui vient d'être ouvert "Oscar 2018.xlsm".

ceci sera donc mieux :

If Cells(2, i).Value = ActiveWorkbook.Worksheets("closing").Range("A1") Then

ou plus simplement

If Cells(2, i).Value = Sheets("closing").Range("A1") Then

Au top !! Merci cela est totalement logique en + ...

Merci pour l'explication

Rechercher des sujets similaires à "indice appartient pas selection"