IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

faire un retour en arrière après tri sur Excel


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Août 2010
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Août 2010
    Messages : 23
    Par défaut faire un retour en arrière après tri sur Excel
    Bonjour à tous,
    Voici mon problème : je dois faire en sorte que le « retour en arrière » soit possible après avoir fait un tri croissant ou décroissant de données sur Excel (ces données se trouvant dans un onglet nommé « workload »), je veux dire par là, faire une macro qui réaffiche les données telles qu’elles étaient juste avant d’avoir fait le tri.

    J’ai donc pensé à faire un userform similaire à la fenêtre qui s’ouvre lorsqu’on va dans données --> trier, et lorsque l’utilisateur valide son choix en cliquant sur le bouton « ok » de ce userform, la macro associée à ce bouton :

    - copie les données « initiales » (celles qu’on a avant de faire le tri) de l’onglet « workload » et les colle dans un onglet caché que j’ai nommé « copie »
    - effectue le tri dans l’onglet « workload » selon le choix de l’utilisateur (choix de la colonne, tri croissant ou décroissant)
    j’ai ensuite fait un bouton « retour arrière » qui, lorsque l’utilisateur clique dessus, la macro associée copie les données qui ont été collées dans l’onglet « copie » et les colle dans l’onglet « workload », pour pouvoir revenir aux données initiales, celles qu’on avait juste avant le tri.
    Ma question est la suivante : existe-t-il un moyen de faire un retour en arrière moins « bricolé » que celui que j’ai fait ? si non, voici le code que j’ai obtenu qui est associé au bouton « ok » de la userform, code qui marche très bien mais que je peux sûrement améliorer (je le trouve trop bricolé) en remplaçant les « order1:=xlAscending » et « order1:=xlDescending » par des « order1:=variable de type string » mais ça ne fonctionne pas quand je remplace par une variable, je cherche donc un moyen de simplifier ce code…

    (CboCol1, CboCol2 et CboCol3 sont les noms des combobox de la userform similaire à la fenêtre que l’on obtient en allant dans données---> trier, celles où l’utilisateur choisit les colonnes à trier ; FrCol1.OptCol1Asc, etc… font référence aux boutons d’option grâce auxquels l’utilisateur détermine s’il veut un tri « croissant » ou bien « décroissant »)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    Private Sub CmdLineSortOk_Click()
    Dim column_index1 As String
    Dim column_index2 As String
    Dim column_index3 As String
    Dim number As Integer
    Dim last_line As Integer
    Dim i As Integer
     
    Sheets("Copie").Visible = True
    Sheets("Copie").Select
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Clear
    Sheets("Workload").Select
    Range("A10").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Sheets("Copie").Select
    Range("A1").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveWindow.SelectedSheets.Visible = False
     
    Sheets("Workload").Select
     
     
    last_line = Range("B65536").End(xlUp).Row
    Rows("10:" & last_line).Select
     
     
    If CboCol1.Value <> "" And CboCol2.Value = "" And CboCol3.Value = "" Then number = 1
    If CboCol1.Value <> "" And CboCol2.Value <> "" And CboCol3.Value = "" Then number = 2
    If CboCol1.Value <> "" And CboCol2.Value <> "" And CboCol3.Value <> "" Then number = 3
     
     
    If number = 1 Then
        If CboCol1.Value = "Date" Then column_index1 = "A10"
        If CboCol1.Value = "Program" Then column_index1 = "B10"
        If CboCol1.Value = "BU" Then column_index1 = "C10"
        If CboCol1.Value = "Phasis" Then column_index1 = "D10"
        If CboCol1.Value = "Type of program" Then column_index1 = "E10"
        If CboCol1.Value = "a" Then column_index1 = "F10"
        If CboCol1.Value = "b" Then column_index1 = "G10"
        If CboCol1.Value = "Project" Then column_index1 = "H10"
        If CboCol1.Value = "Activity" Then column_index1 = "I10"
        If CboCol1.Value = "Working Modes" Then column_index1 = "J10"
        If CboCol1.Value = "Design maturity" Then column_index1 = "K10"
        If CboCol1.Value = "Pro ex" Then column_index1 = "L10"
        If CboCol1.Value = "Pro el" Then column_index1 = "M10"
        If CboCol1.Value = "Internal Pro" Then column_index1 = "N10"
        If CboCol1.Value = "Probability" Then column_index1 = "O10"
     
        If FrCol1.OptCol1Asc.Value = True Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
        Else
        Selection.Sort Key1:=Range(column_index1), order1:=xlDescending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
        End If
    End If
     
     
     
    If number = 2 Then
        If CboCol1.Value = "Date" Then column_index1 = "A10"
        If CboCol1.Value = "Program" Then column_index1 = "B10"
        If CboCol1.Value = "BU" Then column_index1 = "C10"
        If CboCol1.Value = "Phasis" Then column_index1 = "D10"
        If CboCol1.Value = "Type of program" Then column_index1 = "E10"
        If CboCol1.Value = "a" Then column_index1 = "F10"
        If CboCol1.Value = "b" Then column_index1 = "G10"
        If CboCol1.Value = "Project" Then column_index1 = "H10"
        If CboCol1.Value = "Activity" Then column_index1 = "I10"
        If CboCol1.Value = "Working Modes" Then column_index1 = "J10"
        If CboCol1.Value = "Design maturity" Then column_index1 = "K10"
        If CboCol1.Value = "Pro ex" Then column_index1 = "L10"
        If CboCol1.Value = "Pro el" Then column_index1 = "M10"
        If CboCol1.Value = "Internal Pro" Then column_index1 = "N10"
        If CboCol1.Value = "Probability" Then column_index1 = "O10"
     
        If CboCol2.Value = "Date" Then column_index2 = "A10"
        If CboCol2.Value = "Program" Then column_index2 = "B10"
        If CboCol2.Value = "BU" Then column_index2 = "C10"
        If CboCol2.Value = "Phasis" Then column_index2 = "D10"
        If CboCol2.Value = "Type of program" Then column_index2 = "E10"
        If CboCol2.Value = "a" Then column_index2 = "F10"
        If CboCol2.Value = "b" Then column_index2 = "G10"
        If CboCol2.Value = "Project" Then column_index2 = "H10"
        If CboCol2.Value = "Activity" Then column_index2 = "I10"
        If CboCol2.Value = "Working Modes" Then column_index2 = "J10"
        If CboCol2.Value = "Design maturity" Then column_index2 = "K10"
        If CboCol2.Value = "Pro ex" Then column_index2 = "L10"
        If CboCol2.Value = "Pro el" Then column_index2 = "M10"
        If CboCol2.Value = "Internal Pro" Then column_index2 = "N10"
        If CboCol2.Value = "Probability" Then column_index2 = "O10"
     
        If FrCol1.OptCol1Asc.Value = True And FrCol2.OptCol2Asc.Value = True Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlAscending, Key2:=Range(column_index2 _
            ), order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = True And FrCol2.OptCol2Asc.Value = False Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlAscending, Key2:=Range(column_index2 _
            ), order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = False And FrCol2.OptCol2Asc.Value = True Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlDescending, Key2:=Range(column_index2 _
            ), order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = False And FrCol2.OptCol2Asc.Value = False Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlDescending, Key2:=Range(column_index2 _
            ), order2:=xlDescending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
        End If
    End If
     
     
     
    If number = 3 Then
        If CboCol1.Value = "Date" Then column_index1 = "A10"
        If CboCol1.Value = "Program" Then column_index1 = "B10"
        If CboCol1.Value = "BU" Then column_index1 = "C10"
        If CboCol1.Value = "Phasis" Then column_index1 = "D10"
        If CboCol1.Value = "Type of program" Then column_index1 = "E10"
        If CboCol1.Value = "a" Then column_index1 = "F10"
        If CboCol1.Value = "b" Then column_index1 = "G10"
        If CboCol1.Value = "Project" Then column_index1 = "H10"
        If CboCol1.Value = "Activity" Then column_index1 = "I10"
        If CboCol1.Value = "Working Modes" Then column_index1 = "J10"
        If CboCol1.Value = "Design maturity" Then column_index1 = "K10"
        If CboCol1.Value = "Pro ex" Then column_index1 = "L10"
        If CboCol1.Value = "Pro el" Then column_index1 = "M10"
        If CboCol1.Value = "Internal Pro" Then column_index1 = "N10"
        If CboCol1.Value = "Probability" Then column_index1 = "O10"
     
        If CboCol2.Value = "Date" Then column_index2 = "A10"
        If CboCol2.Value = "Program" Then column_index2 = "B10"
        If CboCol2.Value = "BU" Then column_index2 = "C10"
        If CboCol2.Value = "Phasis" Then column_index2 = "D10"
        If CboCol2.Value = "Type of program" Then column_index2 = "E10"
        If CboCol2.Value = "a" Then column_index2 = "F10"
        If CboCol2.Value = "b" Then column_index2 = "G10"
        If CboCol2.Value = "Project" Then column_index2 = "H10"
        If CboCol2.Value = "Activity" Then column_index2 = "I10"
        If CboCol2.Value = "Working Modes" Then column_index2 = "J10"
        If CboCol2.Value = "Design maturity" Then column_index2 = "K10"
        If CboCol2.Value = "Pro ex" Then column_index2 = "L10"
        If CboCol2.Value = "Pro el" Then column_index2 = "M10"
        If CboCol2.Value = "Internal Pro" Then column_index2 = "N10"
        If CboCol2.Value = "Probability" Then column_index2 = "O10"
     
        If CboCol3.Value = "Date" Then column_index3 = "A10"
        If CboCol3.Value = "Program" Then column_index3 = "B10"
        If CboCol3.Value = "BU" Then column_index3 = "C10"
        If CboCol3.Value = "Phasis" Then column_index3 = "D10"
        If CboCol3.Value = "Type of program" Then column_index3 = "E10"
        If CboCol3.Value = "a" Then column_index3 = "F10"
        If CboCol3.Value = "b" Then column_index3 = "G10"
        If CboCol3.Value = "Project" Then column_index3 = "H10"
        If CboCol3.Value = "Activity" Then column_index3 = "I10"
        If CboCol3.Value = "Working Modes" Then column_index3 = "J10"
        If CboCol3.Value = "Design maturity" Then column_index3 = "K10"
        If CboCol3.Value = "Pro ex" Then column_index3 = "L10"
        If CboCol3.Value = "Pro el" Then column_index3 = "M10"
        If CboCol3.Value = "Internal Pro" Then column_index3 = "N10"
        If CboCol3.Value = "Probability" Then column_index3 = "O10"
     
        If FrCol1.OptCol1Asc.Value = True And FrCol2.OptCol2Asc.Value = True And FrCol3.OptCol3Asc.Value = True Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlAscending, Key2:=Range(column_index2 _
            ), order2:=xlAscending, Key3:=Range(column_index3), order3:=xlAscending, Header _
            :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
            , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = True And FrCol2.OptCol2Asc.Value = True And FrCol3.OptCol3Asc.Value = False Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlAscending, Key2:=Range(column_index2 _
            ), order2:=xlAscending, Key3:=Range(column_index3), order3:=xlDescending, Header _
            :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
            , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = True And FrCol2.OptCol2Asc.Value = False And FrCol3.OptCol3Asc.Value = False Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlAscending, Key2:=Range(column_index2 _
            ), order2:=xlDescending, Key3:=Range(column_index3), order3:=xlDescending, Header _
            :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
            , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = False And FrCol2.OptCol2Asc.Value = False And FrCol3.OptCol3Asc.Value = False Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlDescending, Key2:=Range(column_index2 _
            ), order2:=xlDescending, Key3:=Range(column_index3), order3:=xlDescending, Header _
            :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
            , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = True And FrCol2.OptCol2Asc.Value = False And FrCol3.OptCol3Asc.Value = True Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlAscending, Key2:=Range(column_index2 _
            ), order2:=xlDescending, Key3:=Range(column_index3), order3:=xlAscending, Header _
            :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
            , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal
        End If
        If FrCol1.OptCol1Asc.Value = False And FrCol2.OptCol2Asc.Value = False And FrCol3.OptCol3Asc.Value = True Then
        Selection.Sort Key1:=Range(column_index1), order1:=xlDescending, Key2:=Range(column_index2 _
            ), order2:=xlDescending, Key3:=Range(column_index3), order3:=xlAscending, Header _
            :=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom _
            , DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:= _
            xlSortNormal
        End If
    End If
    End Sub

    Désolé si c’est un peu long à lire mais j’ai voulu détailler clairement (par contre je ne peux pas uploader de fichier pr des raisons de confidentialité).

    D’avance merci à ceux qui me répondront.

  2. #2
    Expert confirmé

    Profil pro
    Inscrit en
    Mai 2005
    Messages
    3 419
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2005
    Messages : 3 419
    Par défaut
    on peut procéder ainsi
    on ajoute aux données une colonne ordre
    on y loge la fonction =ligne()-1
    on copie la colonne puis collage spécial valeur
    on peut alors effectuer tout tri y compris sur la colonne ordre ce qui redonne l'ordre originel

  3. #3
    Membre averti
    Inscrit en
    Août 2010
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Août 2010
    Messages : 23
    Par défaut
    pas con du tout, j'y avais pas pensé

    je vais appliquer ta méthode, merci beaucoup pr ta réponse et bonne journée


  4. #4
    Membre averti
    Inscrit en
    Août 2010
    Messages
    23
    Détails du profil
    Informations forums :
    Inscription : Août 2010
    Messages : 23
    Par défaut
    pour ceux qui sont confrontés au problème de remplacer "order1:=xlAscending " par quelque chose comme "order1:=variable" :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    Dim sens As Integer
    sens = 1
    Range("HE10:HE3000").Select
    Selection.Sort Key1:=Range("HE10"), order1:=sens, Header:=xlGuess _
    , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    si sens = 1 le tri sera croissant
    si sens = 2 le tri sera décroissant

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Retour en arrière après chaque espace
    Par Fluch dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 17/03/2014, 13h38
  2. Retour en arrière après chaque espace
    Par deadly_muffin dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 23/09/2011, 09h12
  3. Réponses: 4
    Dernier message: 25/08/2010, 13h07
  4. Aide pour faire un tri sur Excel
    Par legolas51 dans le forum Excel
    Réponses: 4
    Dernier message: 05/09/2008, 16h23
  5. Javascript comment faire le retour en arrière
    Par bozykely3 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 11/07/2007, 16h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo