Excel2010 Macro tri sur feuille active

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

Usine à gaz

XLDnaute Barbatruc
Bonjour à tous,

Toujours aussi nul en VBA (j'avance quand même un peu).

L'écriture (automatique) de la macro me donne ça :

Rows("2:10000").Select
ActiveWorkbook.Worksheets("Smatr").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Smatr").Sort.SortFields.Add Key:=Range("D2 :10000"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(Smatr).Sort
.SetRange Range("A2 :10000")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Smatr est le nom de l'une des feuilles dans mon classeur et je souhaite le changer pour que la macro s'exécute sur n'importe quelle feuille « active » de mon classeur.

Un grand merci pour votre aide.
 
Re : Excel2010 Macro tri sur feuille active

Bonjour,

remplace :

Code:
With ActiveWorkbook.Worksheets(Smatr).Sort

par :
Code:
With ActiveWorkbook.ActiveSheet.Sort

bonne journée
@+

Edition :applique la même chose pour les 2 lignes précédentes....
 
Dernière édition:
Re : Excel2010 Macro tri sur feuille active

Bonjour,
Merci pour ta réponse mais je n'y arrive pas et voilà ce qu'il me donne :

Rows("4:1003").Select
With ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
With ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("JW4:JW1003") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortTextAsNumbers
With With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A4:KE1003")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Call TriA

Range("A4").Select
End Sub
Sub TriA()
'
' TriA Macro
'

'
Range("A4:JN1003").Select
Range("JN1003").Activate
With ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
With ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("A4:A1003"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("A4:JN1003")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Range("A4").Select
End Sub
Sub TriOF()
'
' TriOF Macro
'

'
With ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
With ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("JP4:JP1003") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("JP4:JZ1003")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

Merci pour ton aide,
Lionel,
 
Re : Excel2010 Macro tri sur feuille active

Re,

regarde ceci, plages de cellules à adapter ("key" et "setrange")

Code:
    ActiveWorkbook.ActiveSheet.Sort.SortFields.Clear
    ActiveWorkbook.ActiveSheet.Sort.SortFields.Add Key:=Range("D1:D11") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.ActiveSheet.Sort
        .SetRange Range("A1:H11")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Re : Excel2010 Macro tri sur feuille active

Re,

on pourrait également le coder ainsi :
Code:
    With ActiveWorkbook.ActiveSheet.Sort
        With .SortFields
            .Clear
            .Add Key:=Range("D1:D11") _
                , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        End With
        .SetRange Range("A1:H11")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

  • Question Question
XL 2021 listbox
Réponses
18
Affichages
739
Réponses
17
Affichages
1 K
Réponses
4
Affichages
732
B
  • Question Question
Réponses
2
Affichages
776
Benjy51190
B
Réponses
6
Affichages
1 K
Réponses
11
Affichages
968
Réponses
1
Affichages
932
Retour