Problème Modification Macro "Enregistrer"

Résolu/Fermé
Mavericku Messages postés 5 Date d'inscription mardi 21 juillet 2015 Statut Membre Dernière intervention 10 janvier 2017 - 21 juil. 2015 à 13:56
Frenchie83 Messages postés 2240 Date d'inscription lundi 6 mai 2013 Statut Membre Dernière intervention 11 août 2023 - 22 juil. 2015 à 03:53
Bonjour à tous,

premier post ici j'espère que vous serez en mesure de m'aider. Comme indiqué dans mon profil, je suis un utilisateur d'Excel qui se considère intermédiaire/avancé. J'ai déjà jouer avec le VB6 il y a bien longtemps de cela, donc je suis en mesure de comprendre la logique de programmation.

Voici donc mon problème. J'ai un fichier Excel qui contient les colonnes suivantes (Celle ci sont fixes et ne changeront jamais)

A - Date B - Type C - Number D- Description E- Debit F- Credit

J'ai procéder à un test, en enregistrant une Macro lorsque je procédais à ce que je voulais que le fichier accomplisse. J'ai donc obtenu le résultat suivant :

Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Add Key:=Range( _
"C2:C388"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("20-7-2015").Sort
.SetRange Range("A1:F388")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A2").Select
ActiveWindow.SmallScroll Down:=105
Range("A2:C111").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.Delete Shift:=xlUp
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Add Key:=Range( _
"B2:B278"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("20-7-2015").Sort
.SetRange Range("A2:F278")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A2").Select
ActiveWindow.SmallScroll Down:=87
Range("A2:B95").Select
Range(Selection, Selection.End(xlToRight)).Select
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Add Key:=Range( _
"A2:A95"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("20-7-2015").Sort
.SetRange Range("A2:F95")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Cells.Select
Selection.Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(5, 6), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
Selection.Subtotal GroupBy:=2, Function:=xlCount, TotalList:=Array(6), _
Replace:=False, PageBreaks:=False, SummaryBelowData:=True
Range("E11").Select
Sheets("20-7-2015").Select
Sheets("20-7-2015").Name = "B"

Par contre, j'ai quelques éléments qui doivent être variable pour obtenir le résultat désiré. Les colonnes resteront toujours les mêmes mais je peux avoir plus ou moins de ligne dépendant des semaines.

J'apprécierais si vous pouviez m'assister dans la modification du code pour y insérez ces variables. Par exemple, au début de la macro :

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Add Key:=Range( _
"C2:C388"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal

Je sais que ces lignes vont sélectionner toutes mes données qui sont en continu (elle le sont toutes) :

Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select

Par contre sur ces lignes, le tri ne s'effectue que sur les lignes 2 à 388 ? Donc je dois changer C388 pour une variable choisissant la dernière ligne de mon fichier ?

ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Add Key:=Range( _
"C2:C388"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal

Comme indiqué plus haut je commence en VBA et je sais ce que je veux obtenir. Le chemin pour y parvenir m'est inconnu par contre.

Merci à tous pour votre aide !
A voir également:

1 réponse

Frenchie83 Messages postés 2240 Date d'inscription lundi 6 mai 2013 Statut Membre Dernière intervention 11 août 2023 337
22 juil. 2015 à 03:53
Bonjour
Remplacez la première partie de votre macro
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Add Key:=Range( _
    "C2:C388"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("20-7-2015").Sort
        .SetRange Range("A1:F388")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


par
         DerLig = [A100000].End(xlUp).Row
    ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("20-7-2015").Sort.SortFields.Add Key:=Range( _
    "C2:C" & DerLig), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
    xlSortNormal
    With ActiveWorkbook.Worksheets("20-7-2015").Sort
        .SetRange Range("A1:F" & DerLig)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Appliquer la même méthode pour la suite
Cdlt
0