Macro excel, erreur 1004 suite à un tri

Bonjour,

J'ai un bouton de commande sur un feuille d'un classeur X, et lorsque je clique dessus je souhaite (entre autrre) qu'il me trie des données sur une feuille d'un classeur Y. J'ai obtenue le code en faisant enregistrer une macro, mais lorsque je copie le code et que je le colle dans le code du bouton de commande la fonction tri ne marche plus!! Je sais que ça marcherais avec une bouton de formulaire mais je ne veux pas utiliser de bouton de formulaire pour la raison suivante: mon classeur X contient une 50aine de feuille, et dans chaque feuille j'aurais ce même bouton de commande avec seul changement le nom de la feuille... Et si je crée un bouton de formulaire je devrais avoir autant de macro que de feuille et je risque donc d'etre un peu perdue! Donc voila mon bout de code

Sheets("Données filtrées").Range("A1:C65536").Select
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("C2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
DataOption2:=xlSortNormal

Pour information, lorsque j'en arrive à ce niveau de la macro, je suis bien dans le classeur qui m'intéresse et dans la feuille qui m'intéresse. Il me sélectionne bien mes colonnes, mais ensuite me sort une erreur 1004 avec la ligne "selection.sort etc" surlignée en jaune. Le code marche lorsqu'il est pris en dehors de mon bouton de commande...

Je crois que je travaille sous excel 2002 (je suis au boulot, je sais pas trop comment vérifier)

Merci d'avance!!!
Configuration: Windows 2000 / Firefox 3.0.13

3 réponses

  1. Bonjour,

    J'ai un doute, mais essayez d'effacer "Sheets("Données filtrées")." de la macro comme ceci :
    Range("A1:C65536").Select
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("C2") _
    , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
    False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
    DataOption2:=xlSortNormal

    Si elle fonctionne toujours, elle devrait fonctionner partout !

    Cordialement,
    Migou

    PS : J'aurais bien essayé, mais je n'ai plus Excel
    0
    1. Ca marche pas non plus :'(
      0
      1. Pour information voici ma macro complète, si ça peut aider.

        Alors voila la macro toute seule qui marche bien:
        Sub MAJtop10ref()
        '
        ' MAJtop10ref Macro
        ' Macro enregistrée le 26/02/2010 par XXX
        '

        '
        Windows("Données.xls").Activate
        Sheets("Données").Select
        Columns("B:B").Select
        Range("B:B,K:K,AS:AS").Select
        Range("AS1").Activate
        Selection.Copy
        Sheets("Données filtrées").Select
        Range("A1").Select
        ActiveSheet.Paste
        Sheets("Données").Select
        Application.CutCopyMode = False
        Range("B:B,K:K,AT:AT").Select
        Selection.Copy
        Sheets("Données filtrées").Select
        Range("D1").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Range("H2").Select
        ActiveCell.FormulaR1C1 = "=[indicateur.xls]Honda!R4C2"
        Columns("A:C").Select
        Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("C2") _
        , Order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
        DataOption2:=xlSortNormal
        Columns("D:F").Select
        Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, Key2:=Range("F2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
        DataOption2:=xlSortNormal
        Windows("indicateur.xls").Activate
        Windows("Données.xls").Activate
        Range("A1").Select
        Range("A1:F46864").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        Range("I1:I2"), Unique:=False
        Range("B4984:C4993").Select
        Selection.Copy
        Windows("indicateur.xls").Activate
        Range("C23").Select
        ActiveSheet.Paste
        Windows("Données.xls").Activate
        Range("E4984:F4993").Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("indicateur.xls").Activate
        Range("E23").Select
        ActiveSheet.Paste
        ActiveWindow.SmallScroll Down:=9
        Range("C23:D32").Select
        Application.CutCopyMode = False
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        Range("E23:F32").Select
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .Weight = xlThick
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .Weight = xlThin
        .ColorIndex = xlAutomatic
        End With
        Windows("Données.xls").Activate
        Sheets("Données filtrées").Select
        ActiveSheet.ShowAllData

        Et la macro bidouillée pour mon bouton de commande, qui bug au niveau du tri (je ne copie que le début)

        Private Sub CommandButton2_Click()
        Windows("Données.xls").Activate
        Sheets("Données").Select
        Sheets("Données").Range("B:B,K:K,AS:AS").Select
        Selection.Copy
        Sheets("Données filtrées").Select
        Sheets("Données filtrées").Range("A1").Select
        ActiveSheet.Paste
        Sheets("Données").Select
        Application.CutCopyMode = False

        Sheets("Données").Range("B:B,K:K,AT:AT").Select
        Selection.Copy
        Sheets("Données filtrées").Select
        Sheets("Données filtrées").Range("D1").Select
        ActiveSheet.Paste
        Application.CutCopyMode = False
        Sheets("Données filtrées").Range("H2").Select
        ActiveCell.FormulaR1C1 = "=[indicateur.xls]Honda!R4C2"
        Sheets("Données filtrées").Range("A1:C65536").Select
        MsgBox Selection.Address
        Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range("C2") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
        DataOption2:=xlSortNormal

        Pour rappel, je ne veux pas utiliser de bouton de formulaire!
        0