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 :

Tri personnalisé qui se lance tout seul


Sujet :

Macros et VBA Excel

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 21
    Par défaut Tri personnalisé qui se lance tout seul
    Bonjour,

    Je vous écris à propos d'un comportement d'excel que je ne comprends pas avec une de mes macros.

    En gros à la base je veux trier une colonne d'un TCD à l'aide d'une liste personnalisée.

    La création du TCD c'est ok. De même pour l'ajout de la liste personnalisée.
    Seulement le tri est effectué qu'au deuxième lancement de la macro, chose que je ne comprends pas.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Application.AddCustomList ListArray:=Array("port libre", "port pseudo libre", "port architecturé", "port réduit", "abattage", "essouchage", "recépage", "dévitalisation")
        Dim n As Integer
        n = Application.GetCustomListNum(Array("port libre", "port pseudo libre", "port architecturé", "port réduit", "abattage", "essouchage", "recépage", "dévitalisation")) + 1
    '1 added to give true position of desired order in custom list
    Sheets("Travailbis").Select
    Selection.Range("A5").Select 'début de ma colonne
    Selection.Sort Order1:=xlAscending, Header:=xlGuess, _
    Type:=xlSortLabels, OrderCustom:=n, Orientation:=xlTopToBottom
    Autre particularité que je ne comprends pas, si je désactive la commande de tri, le tri s'effectue quand même, c'est à dire avec le code suivant:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    Application.AddCustomList ListArray:=Array("port libre", "port pseudo libre", "port architecturé", "port réduit", "abattage", "essouchage", "recépage", "dévitalisation")
        Dim n As Integer
        'n = Application.GetCustomListNum(Array("port libre", "port pseudo libre", "port architecturé", "port réduit", "abattage", "essouchage", "recépage", "dévitalisation")) + 1
    '1 added to give true position of desired order in custom list
    Sheets("Travailbis").Select
     
    Selection.Range("A5").Select
    'Selection.Sort Order1:=xlAscending, Header:=xlGuess, '_
    'Type:=xlSortLabels, OrderCustom:=n, Orientation:=xlTopToBottom
    Avez-vous une idée? le seul fait de sélectionner le début de la colonne de mon tcd en ayant ajouté une liste personnalisée avant ferait que le tri s'effectue automatiquement?

    Je vous remercie d'avance.

  2. #2
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Par défaut
    Où places-tu ton code ?
    Commence par vérifier si une macro ds l'événement Workbook_Open ou Worksheet_Change ne lance pas ce code ou une macro qui appelle une macro qui appelle...

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 21
    Par défaut
    Merci Ouskel'nor de ta réponse.

    Qu'entends-tu par
    Où places-tu ton code ?
    ?

    Je ne suis pas sûr de répondre à ta question mais le bout de code qui effectue le trie se trouve dans un sub. je n'appelle ce bout de macro qu'une seule fois.

    je veux bien mettre tout mon code en ligne mais ça va faire long.

    Sinon comment dois-je faire pour vérifier
    si une macro ds l'événement Workbook_Open ou Worksheet_Change ne lance pas ce code
    ?

    Merci encore.

  4. #4
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Par défaut
    Est-ce toi qui a démarré ce projet ? Parce que si c'est quelqu'un d'autre, il peut avoir mis un code qui lance ta macro dan ThisWorkbook ou dans la feuille de code de l'une de feuilles de calculs -> Tu trouves tout ça dans l'éditeur VBA -> deux clics sur Thisworkbook ou sur le nom de chaque feuille.
    A tout hasard...

  5. #5
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 21
    Par défaut
    Oui c'est moi qui ai initié ce projet.

    Ma macro fonctionne mais en la lançant deux fois de suite donc ce n'est pas si grave ça. Mais j'avoue que je n'aime pas ne pas comprendre. Surtout que je ne vois pas où peut bien se cacher l'erreur.Et donc le fonctionnement d'Excel m'étonne en l'occurrence.

    Le fait que je mette tout mon code vous intéresse-t-il?

  6. #6
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Par défaut
    Oui, surtout si ton code, pour une raison ou une autre, fait appel au système, ce qui pourrait expliquer des choses

  7. #7
    Membre averti
    Profil pro
    Inscrit en
    Août 2008
    Messages
    21
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2008
    Messages : 21
    Par défaut
    Bonjour, je mets en ligne mon code.
    J'ai mis l'ensemble des modules de ma macro.

    Pour expliquer le contexte, cette macro sert à créer des TCD à partir d'une feuille de données et ensuite de les mettre en forme (bordures, police...).

    Le Sub TCDexportation() gère l'ensemble de la macro.
    Le Sub TCD crée les TCD (il y en a de deux types, palette et travaux)
    Les TCD palette sont ensuite travaillés par le sub palette. Les TCD travaux par le sub travaux.
    Une fois un TCD finalisé, il est envoyé sur une autre feuille avec la fonction exportation (plus compliquée qu'un simple envoi car tous les tableaux d'un même type seront disposés sur une même feuille en respectant une certaine disposition).

    J'ai mis en rouge les parties du code qui posent problème à mon avis, c'est à dire les codes de tris personnalisés.

    Merci d'avance de votre aide. et merci pour vos réponses précédentes.


    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
    Private plage As Range
    Private final1(1 To 1, 1 To 3)
    Private final2(1 To 1, 1 To 3)
    Private hauteur As Integer
    Private largeur As Integer
    Private numerocolonne As Integer
    Private itération As Integer
    Private nligneplantes As Integer
    Private larg1palette
    Private larg2palette
    Private larg1travaux
    Private larg2travaux
     
    Option Explicit
     
    Sub TCDexportation()
     
    'Désactivation du rafraichissement de l'écran pour ne pas ralentir la macro
    Application.ScreenUpdating = False
     
    'Création des feuilles de travail
     
    'Désactivation de l'affichage des alertes lors de la suppression de feuilles
    Application.DisplayAlerts = False
    Sheets("Exportation1").Delete
    Sheets("Exportation2").Delete
    'Réactivation de l'affichage des alertes
    Application.DisplayAlerts = True
     
    Sheets.Add.Name = "Exportation1"
    Sheets.Add.Name = "Exportation2"
    Sheets.Add.Name = "Travail"
    Sheets.Add.Name = "Travailbis"
    Sheets.Add.Name = "Travailter"
    Sheets.Add.Name = "Travail2"
    Sheets.Add.Name = "Travail3"
    Sheets.Add.Name = "Travail4"
    Sheets.Add.Name = "Travail5"
    Sheets.Add.Name = "Travail6"
     
     
    'calcul du nb de Noms de plantes
    Worksheets("Nomsplantes").Activate
    [A1].Select
    Selection.CurrentRegion.Select
    nligneplantes = Application.Selection.Rows.count
     
    'Création du TCD qui est automatiquement copié sur Travail3 (voir fonction TCD)
     
    TCD "TCD1", 2, "(Tous)", "(Tous)", 1
     'Ajoue de la valeur (tous) à site et station
      Cells(1, 1).Select
    Selection.EntireRow.Insert
    Cells(1, 1) = "(Tous)"
    Cells(1, 2) = "(Tous)"
     
    'les cellules site vides à cause de la présence de plusieurs stations pour un site, sont remplie avec le nom du site correspondant
    Dim nbstation As Integer
    nbstation = Selection.CurrentRegion.Rows.count
        Dim i As Integer
        For i = 1 To nbstation
            If IsEmpty(Cells(i, 1)) Then
               Cells(i, 1).Value = Cells(i - 1, 1)
            End If
        Next
     
     'Initialisation du tableau final2
    Dim debut(1 To 1, 1 To 3)
    debut(1, 1) = 1
    debut(1, 2) = 1
    debut(1, 3) = 0
     
     numerocolonne = 1
    'Début de la boucle de création en chaîne des TCD pour chaque site et station
     
    For i = 1 To nbstation
      itération = i
    'Création des TCD palette et travaux complets (station = site = (tous))
      If i = 1 Then
     
         Dim site As String
         site = Worksheets("Travail4").Cells(i, 1).Text
         Dim station As String
         station = Worksheets("Travail4").Cells(i, 2).Text
     
        'Création du TCD de palette végétale
         TCD "TCD1", 0, (site), (station), 1
        'Application du style au tableau obtenu
         palette
        'Exportation du tableau finalisé
         exportation debut(1, 1), debut(1, 2), site, station, 0, i
     
        'De même pour les travaux
         TCD "TCD1", 1, (site), (station), 1
         travaux
         exportation debut(1, 1), debut(1, 2), site, station, 1, i
     
      'Création des TCD palette et travaux site par site et station par station
      Else
     
         site = Worksheets("Travail4").Cells(i, 1).Text
         station = Worksheets("Travail4").Cells(i, 2).Text
     
         TCD "TCD1", 0, (site), (station), 2
         palette
         exportation final1(1, 1), final1(1, 2), site, station, 0, i
     
     
         TCD "TCD1", 1, (site), (station), 2
         travaux
         exportation final2(1, 1), final2(1, 2), site, station, 1, i
     
    End If
     
    numerocolonne = numerocolonne + largeur + 5 - 1
    Next
     
    'Désactivation de l'affichage des alertes lors de la suppression de feuilles
    Application.DisplayAlerts = False
    Sheets("Travail").Delete
    Sheets("Travail2").Delete
    Sheets("Travail3").Delete
    Sheets("Travail4").Delete
    Sheets("Travail5").Delete
    Sheets("Travail6").Delete
    Sheets("Travailbis").Delete
    Sheets("Travailter").Delete
    'Réactivation de l'affichage des fenêtres d'alerte
    Application.DisplayAlerts = True
    'Réactivation du rafraichissement de l'écran
    Application.ScreenUpdating = True
     
    End Sub
    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
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    Sub TCD(Nom As String, a As Single, site As String, station As String, ind As Integer)
    
    ' Cette fonction créé un TCD à partir des paramètres d'entrée suivants:
    'nom du TCD à créer
    'Site: nom du site à prendre en compte
    'Station: nom de la station à prendre en compte
    'ind: valeur d'itération de TCD
    
    
    'Nettoyage des feuilles servant juste de stockage ponctuel de travail de toutes données mais conserve les largeurs de colonnes
    
    Worksheets("Travail2").Cells.Clear
    Worksheets("Travail3").Cells.Clear
    Worksheets("Travail5").Cells.Clear
    Worksheets("Travail6").Cells.Clear
    
    'Activation de la feuille contenant la plage de données
    Sheets("BD").Activate
    
    'dimension de la plage contenant les données
    Dim DataR As Long
    Dim DataC As Integer
    Dim Source As String
    
    'Selection de la plage de données
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
        
    'Définition de la plage de données source pour le TCD
    DataR = Selection.CurrentRegion.Rows.count
    DataC = Selection.CurrentRegion.Columns.count
    Source = "bd!R1C1:R" & CStr(DataR) & "C" & CStr(DataC)
    
    'Selection de la feuille et de la cellule de départ pour la création du TCD
    If a = 0 Then
       Sheets("Travail").Activate
       Selection.Range("A1").Select
    End If
    
    If a = 1 Then
       Sheets("Travailbis").Activate
       Selection.Range("A1").Select
    End If
    
    If a = 2 Then
       Sheets("Travailter").Activate
       Selection.Range("A1").Select
    End If
    
    If ind = 1 Then
    
    'Création du TCD
       ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
        Source).CreatePivotTable TableDestination:=Selection, TableName:=Nom
    
       ActiveSheet.PivotTables(Nom).SmallGrid = False
    
    'TCD pour la palette végétale
    
        If a = 0 Then
           ActiveSheet.PivotTables(Nom).RowGrand = True
           ActiveSheet.PivotTables(Nom).AddFields RowFields:= _
            Array("Type", "Essence", "Nom botanique"), PageFields:=Array("Site", "Station")
           ActiveSheet.PivotTables(Nom).PivotFields("Essence"). _
            Orientation = xlDataField
            
              'Suppression des sous-totaux inutiles
            ActiveSheet.PivotTables(Nom).PivotSelect _
            "Essence[All;Total]", xlDataAndLabel, True
            Selection.Delete
            
            
            'TCD pour les travaux
            
        ElseIf a = 1 Then
            ActiveSheet.PivotTables(Nom).RowGrand = True
            ActiveSheet.PivotTables(Nom).AddFields RowFields:= _
            Array("Type de travaux", "Travaux"), ColumnFields:="Urgence", PageFields:=Array("Site", "Station")
            ActiveSheet.PivotTables(Nom).PivotFields("Travaux"). _
            Orientation = xlDataField
            
             With ActiveSheet.PivotTables(Nom).PivotFields("Type de travaux")
             Dim i As Integer
                        For i = 1 To .PivotItems.count
                 If UCase(.PivotItems(i).Name) = UCase("(vide)") Then
                    .PivotItems(i).Visible = False
                 End If
               Next i
             End With
     
            With ActiveSheet.PivotTables(Nom).PivotFields("Travaux")
              For i = 1 To .PivotItems.count
                If UCase(.PivotItems(i).Name) = UCase("(vide)") Then
                   .PivotItems(i).Visible = False
                End If
              Next i
            End With
    
     
            With ActiveSheet.PivotTables(Nom).PivotFields("Urgence")
              For i = 1 To .PivotItems.count
                If UCase(.PivotItems(i).Name) = UCase("(vide)") Then
                   .PivotItems(i).Visible = False
                End If
              Next i
            End With
                     
             ActiveSheet.PivotTables(Nom).PivotFields("Urgence"). _
            ShowAllItems = True
            
            'TCD pour la liste sites/stations
        ElseIf a = 2 Then
            ActiveSheet.PivotTables(Nom).RowGrand = True
            ActiveSheet.PivotTables(Nom).AddFields RowFields:= _
            Array("Site", "Station")
            ActiveSheet.PivotTables(Nom).PivotFields("Station"). _
            Orientation = xlDataField
            ActiveSheet.PivotTables(Nom).PivotSelect _
            "Site[All;Total]", xlDataAndLabel, True
            Selection.Delete
        End If
    End If
      
    If a = 0 Or a = 1 Then
        ActiveSheet.PivotTables(Nom).PivotFields("Site").CurrentPage = site
        ActiveSheet.PivotTables(Nom).PivotFields("Station").CurrentPage = station
    End If
        
    If ind = 1 Then
        If a = 1 Then
        Application.AddCustomList ListArray:=Array("port libre", "port pseudo libre", "port architecturé", "port réduit", "abattage", "essouchage", "recépage", "dévitalisation")
        Dim n As Integer
        n = Application.GetCustomListNum(Array("port libre", "port pseudo libre", "port architecturé", "port réduit", "abattage", "essouchage", "recépage", "dévitalisation")) + 1
    '1 added to give true position of desired order in custom list
    Sheets("Travailbis").Select
    'ActiveSheet.Select
    'Selection.Worksheets("Travailbis").[A5].Select
    Selection.Range("A5").Select
    Selection.Sort Order1:=xlAscending, Header:=xlGuess, _
    Type:=xlSortLabels, OrderCustom:=n, Orientation:=xlTopToBottom
    
    
    Dim v As Integer
       v = 6
       Dim t As Integer
       For t = 0 To 7
       Dim texte As String
         texte = Worksheets("Tableaux").Cells(96, 11 + t).Text
         Dim u As Integer
              u = 0
              Dim x As Integer
                   x = 1
         While Not IsEmpty(Worksheets("Tableaux").Cells(96 + x, 11 + t))
             u = u + 1
             x = x + 1
         Wend
         
         If Not u = 0 Then
            Dim liste() As Variant
            ReDim Preserve liste(u - 1)   '(-1) car les arrays commencent à 0 et pas 1
          'remplir l'array
            For i = 0 To (u - 1)
               liste(i) = Sheets("Tableaux").Cells(97 + i, 11 + t).Value
            Next i
          
    
              Application.AddCustomList ListArray:=Array(liste)
              
           Dim m As Integer
                   m = Application.GetCustomListNum(Array(liste())) + 1
    
           
            Sheets("Travailbis").Activate
            'Range("B5").Select
            Dim w As Integer
                    w = v + u - 1
            
            Dim cellule As String
            Dim b As String
            cellule = b & v & ":" & b & w
            Range(cellule).Select
            v = w + 2
            
            
           Sheets("Travailbis").Activate
           'ActiveSheet.Select
           Range(cellule).Select
        Selection.Sort Order1:=xlAscending, Type:=xlSortLabels, OrderCustom:=m, _
            Orientation:=xlTopToBottom
         End If
       Next
    
     
        End If
        End If
           
     If a = 0 Then
    'Sélection du TCD nettoyé et collage sur Travail2
    Worksheets("Travail").Activate
    Range("A4").Select
      Selection.CurrentRegion.Select
      Selection.Copy
      Sheets("Travail2").Select
     Range("B2").Select
    ActiveSheet.Paste
    End If
     If a = 1 Then
    'Sélection du TCD nettoyé et collage sur Travail3
    Worksheets("Travailbis").Activate
    Range("A4").Select
      Selection.CurrentRegion.Select
      Selection.Copy
      Sheets("Travail3").Select
     Range("B2").Select
    ActiveSheet.Paste
    End If
      If a = 0 Or a = 1 Then
    'La première ligne inutile du TCD est éliminée
      Rows(2).Select
        Selection.Delete Shift:=xlUp
        Selection.CurrentRegion.Select
        Dim li As Integer
        li = Selection.CurrentRegion.Rows.count
        Dim co As Integer
        co = Selection.CurrentRegion.Columns.count
    
    Set plage = Range(Cells(2, "B"), Cells(li, co))
    End If
    
    If a = 2 Then
    'Sélection du TCD nettoyé et collage sur Travail2
    Worksheets("Travailter").Activate
    Selection.CurrentRegion.Select
    li = Selection.CurrentRegion.Rows.count
    co = Selection.CurrentRegion.Columns.count
     Range(Cells(3, "A"), Cells(li, co)).Select
    Selection.Copy
    
    
     Sheets("Travail4").Activate
     Range("A1").Select
    ActiveSheet.Paste
    
      
    'Les deux premières lignes inutiles du TCD sont éliminées
    Selection.CurrentRegion.Select
    li = Selection.CurrentRegion.Rows.count
    co = Selection.CurrentRegion.Columns.count
    Rows(li).Select
        Selection.Delete Shift:=xlUp
        Columns(co).Select
        Selection.Delete Shift:=xlLeft
        Selection.CurrentRegion.Select
    li = Selection.CurrentRegion.Rows.count
    co = Selection.CurrentRegion.Columns.count
    
    
      Set plage = Range(Cells(1, "A"), Cells(li, co))
    End If
         
    End Sub
    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
    Sub palette()
     
    'Copie de la plage de donnée sélectionnée et collage en haut à gauche sur la feuille Exportation
    Dim Travail2 As Sheets
     Sheets("Travail2").Activate
      [B2].Select
     plage.Copy [B2]
     
    'Travail de mise en forme du tableau obtenu sur la feuille exportation
     
    'Première étape, Supprimer les cellules inutiles, déplacer et renommer les mal disposées, supprimer première colonne
     
    Selection.CurrentRegion.Select
    Dim nligne As Integer
    nligne = Application.Selection.Rows.count
    Dim ncolonne As Integer
    ncolonne = Application.Selection.Columns.count
     
    Dim i As Integer
    For i = 2 To nligne + 1
     
    If Cells(i, 2).Value = "Total (vide)" Then
    Rows(i).Select
        Selection.Delete Shift:=xlUp
     End If
     
     Next
     
     Selection.CurrentRegion.Select
    nligne = Application.Selection.Rows.count
    ncolonne = Application.Selection.Columns.count
     
    For i = 2 To nligne + 1
     
    If Cells(i, 2).Value Like "Total*" Then
    Cells(i, 3).Value = Cells(i, 2).Value
    End If
     
    Next
     Columns("B").Select
     Selection.Delete Shift:=xlLeft
     'Style du tableau nettoyé des lignes et colonnes inutiles
     
     [B2].Select
     Selection.CurrentRegion.Select
    nligne = Application.Selection.Rows.count
    ncolonne = Application.Selection.Columns.count
     
    'Remplacement des noms botaniques partiels (genre espèce variété)
    'par les noms botaniques complets (genre espèce nom d'auteur variété)
    For i = 3 To nligne + 1
     
    Dim r As Integer
    For r = 2 To nligneplantes
    If Worksheets("Travail2").Cells(i, 2) = Worksheets("Nomsplantes").Cells(r, 2) Then
    Worksheets("Travail2").Cells(i, 3).Select
    Worksheets("Nomsplantes").Cells(r, 1).Copy Selection
    End If
    Next
    Next
     
     
    For i = 2 To nligne + 1
     'Style de la première ligne
     
     If i = 2 Then
     Union(Cells(i, 2), Cells(i, 3), Cells(i, ncolonne + 1)).Select
     With Selection.Cells.Font
     .Bold = True
     .Size = 8
     .Name = "Lucida Sans Unicode"
      End With
    With Selection
     .HorizontalAlignment = xlCenter
      End With
     
     With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThick
            .ColorIndex = xlAutomatic
            End With
     
     
    'Style des lignes de sous-totaux
     
    ElseIf Cells(i, 2).Value Like "Total*" Or Cells(i, 2).Value Like "Manque*" Or Cells(i, 2).Value Like "Souche*" Then
     
    If i = nligne + 1 Then
            Cells(i, 2).ClearContents
            Union(Cells(i, 2), Cells(i, 3)).Select
            With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Cells(i, 2).Value = "Total"
        End If
     
    Range(Cells(i, 2), Cells(i, ncolonne + 1)).Select
     
    With Selection.Font
    .Bold = True
    .Size = 8
    .Name = "Lucida Sans Unicode"
    End With
    With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThick
            .ColorIndex = xlAutomatic
            End With
     
            If Cells(i, 2).Value = "Total f" Then
            Cells(i, 2).Value = "Synthèse pour Feuillus"
            End If
     If Cells(i, 2).Value = "Total r" Then
            Cells(i, 2).Value = "Synthèse pour Résineux"
            End If
     
            If Cells(i, 2).Value = "Manque" Or Cells(i, 2).Value = "Souche" Then
     Cells(i, 3).ClearContents
              Cells(i, 2).Select
     With Selection.Borders(xlEdgeRight)
            .LineStyle = xlNone
                    End With
                    Cells(i, 3).Select
     With Selection.Borders(xlEdgeLeft)
            .LineStyle = xlNone
                    End With
    End If
          Cells(i, ncolonne + 1).Select
      With Selection
     .HorizontalAlignment = xlCenter
      End With
     
     ElseIf Not IsEmpty(Cells(i, 3)) Then
     Union(Cells(i, 2), Cells(i, ncolonne + 1)).Select
     With Selection.Cells.Font
     '.Size = 7.5
     .Size = 8
     .Name = "Lucida Sans Unicode"
     End With
     Cells(i, 3).Select
     With Selection.Cells.Font
      .Size = 8
     .Name = "Lucida Sans Unicode"
     End With
      Cells(i, ncolonne + 1).Select
      With Selection
     .HorizontalAlignment = xlCenter
      End With
     
     End If
     Next
     
    [B2].Select
        Selection.CurrentRegion.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 = xlThick
            .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
            .VerticalAlignment = xlCenter
        End With
     
        Dim li As Integer
            li = Selection.CurrentRegion.Rows.count
            Dim co As Integer
            co = Selection.CurrentRegion.Columns.count
     
    Set plage = Range(Cells(2, "B"), Cells(li + 1, co + 1))
    hauteur = nligne
    Sheets("Travail5").Select
     [B2].Select
     plage.Copy [B2]
     
     
    End Sub
    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
    Sub travaux()
     
    'Copie de la plage de donnée sélectionnée et collage en haut à gauche sur la feuille Exportation
    Dim Travail3 As Sheets
     Sheets("Travail3").Activate
      [B2].Select
     plage.Copy [B2]
     
    'Travail de mise en forme du tableau obtenu sur la feuille exportation
     
    '1).Première étape, Supprimer les cellules inutiles
     
    'Sélection de la plage de données et calcul du nb de lignes et de colonnes
    Selection.CurrentRegion.Select
    Dim nligne As Integer
    nligne = Application.Selection.Rows.count
    Dim ncolonne As Integer
    ncolonne = Application.Selection.Columns.count
     
     
    '2).Application du style choisi
    'Sélection de la nouvelle plage de données(allégée des lignes et colonnes "vides") et calcul du nb de lignes et de colonnes
     
     
    Selection.CurrentRegion.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 = xlThick
            .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
            .VerticalAlignment = xlCenter
        End With
        With Selection.Borders(xlInsideVertical)
        .LineStyle = xlThin
        .LineStyle = xlContinuous
        End With
     
    nligne = Application.Selection.Rows.count
    ncolonne = Application.Selection.Columns.count
     
    Dim i As Integer
    For i = 2 To nligne + 1
     
     
    'Style pour la première ligne du tableau
     
    If i = 2 Then
    Range(Cells(2, 2), Cells(i, ncolonne + 1)).Select
    With Selection.Font
    .Bold = True
    .Size = 8
    .Name = "Lucida Sans Unicode"
    End With
    With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThick
            .ColorIndex = xlAutomatic
            End With
            With Selection
     .HorizontalAlignment = xlCenter
      End With
     
     
    'Style pour les lignes de sous-totaux
     
    ElseIf Cells(i, 2).Value Like "Total*" Then
     
    If i = nligne + 1 Then
            Cells(i, 2).ClearContents
            Union(Cells(i, 2), Cells(i, 3)).Select
            With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Cells(i, 2).Value = "Total"
        Else
        Dim valeur As Variant
     
            valeur = Cells(i, 2)
     
        Cells(i, 2).ClearContents
            Union(Cells(i, 2), Cells(i, 3)).Select
            With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With
        Cells(i, 2).Value = valeur
        End If
     
    Range(Cells(i, 2), Cells(i, ncolonne + 1)).Select
     
    If i = nligne + 1 Then
    With Selection.Font
    .Bold = True
    .Size = 8
    .Name = "Lucida Sans Unicode"
    End With
    With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThick
            .ColorIndex = xlAutomatic
            End With
     
     
       Union(Cells(i, 2), Cells(i, 3)).Select
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
       Else
       With Selection.Font
    .Bold = True
    .Size = 8
    .Name = "Lucida Sans Unicode"
    End With
    With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThick
            .ColorIndex = xlAutomatic
            End With
     
            With Selection.Borders(xlEdgeTop)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
            End With
       Union(Cells(i, 2), Cells(i, 3)).Select
    Selection.Borders(xlInsideVertical).LineStyle = xlNone
    End If
     
    Else
    Range(Cells(i, 2), Cells(i, ncolonne + 1)).Select
    With Selection.Font
    .Bold = False
    .Size = 8
    .Name = "Lucida Sans Unicode"
    End With
    Range(Cells(i, 3), Cells(i, ncolonne + 1)).Select
    With Selection.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = xlAutomatic
            End With
     Cells(i, 2).Select
      valeur = Mid(Selection.Value, 2)
    valeur = UCase(Mid(Selection.Value, 1, 1)) & valeur
    Selection.Value = valeur
     
     End If
     
     Dim j As Integer
     For j = 4 To ncolonne + 1
     
    Cells(i, j).Select
     With Selection
     .HorizontalAlignment = xlCenter
      End With
     
    Next
     
    Next
    'Style de bordure de contour pour l'ensemble du tableau
     
    Selection.CurrentRegion.Select
     
       Dim li As Integer
            li = Selection.CurrentRegion.Rows.count
            Dim co As Integer
            co = Selection.CurrentRegion.Columns.count
     Set plage = Range(Cells(2, "B"), Cells(li + 1, co + 1))
     
    Sheets("Travail6").Select
     [B2].Select
     plage.Copy [B2]
     
     End Sub
    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
     
     Sub exportation(coli, coco, site, station, a, ind)
     
    Dim c As Integer
    c = a
     
    If a = 0 Then
    Sheets("Exportation1").Select
    Cells(coli, coco) = "Site"
    Cells(coli, coco + 1) = site
    Cells(coli + 1, coco) = "Station"
    Cells(coli + 1, coco + 1) = station
    Cells(coli + 3, coco).Select
     plage.Copy [Selection]
     
     If ind = 1 Then
    Columns(coco).AutoFit
    Columns(coco + 1).EntireColumn.AutoFit
    larg1palette = Columns(coco).ColumnWidth
    larg2palette = Columns(coco + 1).ColumnWidth
    End If
     
       Columns(coco).ColumnWidth = larg1palette
      Columns(coco + 1).ColumnWidth = larg2palette
     
     Dim li As Integer
    li = 1
    Dim co As Integer
    co = coco + 2 + 5
     
    Dim b As Integer
     b = 1
    End If
     
    If a = 1 Then
    Sheets("Exportation2").Select
    Cells(coli, coco) = "Site"
    Cells(coli, coco + 1) = site
    Cells(coli + 1, coco) = "Station"
    Cells(coli + 1, coco + 1) = station
    Cells(coli + 3, coco).Select
     plage.Copy [Selection]
     
     If ind = 1 Then
    Columns(coco).AutoFit
    Columns(coco + 1).EntireColumn.AutoFit
    larg1travaux = Columns(coco).ColumnWidth
    larg2travaux = Columns(coco + 1).ColumnWidth
    End If
     
    Columns(coco).ColumnWidth = larg1travaux
      Columns(coco + 1).ColumnWidth = larg2travaux
     
    li = 1
    co = coco + 5 + 5
    b = 0
    End If
     
    a = b
     
     If c = 0 Then
     final1(1, 1) = li
     final1(1, 2) = co
     final1(1, 3) = a
     
     Else
     final2(1, 1) = li
     final2(1, 2) = co
     final2(1, 3) = a
     
     End If
    End Sub

  8. #8
    Inactif  
    Avatar de ouskel'n'or
    Profil pro
    Inscrit en
    Février 2005
    Messages
    12 464
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2005
    Messages : 12 464
    Par défaut
    Ce n'est pas "Application.AddCustomList ListArray:=Array(....)
    mais
    Application.AddCustomList Array(..........)
    Bonne journée

  9. #9
    Membre Expert
    Avatar de JackOuYA
    Inscrit en
    Juin 2008
    Messages
    1 040
    Détails du profil
    Informations forums :
    Inscription : Juin 2008
    Messages : 1 040
    Par défaut
    Bonjour,

    pour que ton code soit plus clair et que tu sois sur d'agir sur le bon classeur, la bonne feuille.... il faudrai que tu supprime tous ces "Select, selection ,activate , ActiveCell, activeSheet...)

    par exemple pour ton tri tu peu remplacer :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Sheets("Travailbis").Select
    'ActiveSheet.Select
    'Selection.Worksheets("Travailbis").[A5].Select
    Selection.Range("A5").Select
    Selection.Sort Order1:=xlAscending, Header:=xlGuess, _
    Type:=xlSortLabels, OrderCustom:=n, Orientation:=xlTopToBottom
    par
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    Sheets("Travailbis").Range("A5").Sort Order1:=xlAscending, Header:=xlGuess, _
    Type:=xlSortLabels, OrderCustom:=n, Orientation:=xlTopToBottom

Discussions similaires

  1. Réponses: 3
    Dernier message: 28/07/2012, 15h26
  2. Réponses: 9
    Dernier message: 24/01/2012, 02h18
  3. Macro qui se lance toute seule, sans mon autorisation
    Par csempere dans le forum VBA Word
    Réponses: 5
    Dernier message: 06/06/2009, 09h10
  4. [webcam] qui se lance toute seule
    Par zodd dans le forum Sécurité
    Réponses: 7
    Dernier message: 20/02/2008, 10h22
  5. Un evenement qui se lance tout seul ?
    Par insane_80 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 15/03/2007, 17h17

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