Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Comment utiliser SpecialCells(xlLastCell) avec la commande "Sort" ?

6 réponses
Avatar
Gouap
Bonjour à tous,

Avec l'enregistreur de macro d'Excel j'obtients le code suivant qui
fonctionne très bien mais qui ne tri que les ligne 1 à 32. Ce que je cherche
à faire c'est à modifier ce code pour qu'il tri jusqu'à la ligne
correspondant à SpecialCells(xlLastCell) sans devoir mettre ("A1: B65536")
mais toutes mes tentatives sont regetées par Excel.

ActiveCell.Offset(0, -2).Columns("A:B").EntireColumn.Select
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Add Key:= _
ActiveCell.Offset(0, 1).Range("A1:A32"), SortOn:=xlSortOnValues,
Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Consolidation").Sort
.SetRange ActiveCell.Range("A1:B32")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Pouvez vous m'aider à trouver la bonne syntaxe ?

@+
Philippe

6 réponses

Avatar
Tatanka
Bonjour Gouap,

Ces deux instructions peuvent-elles te venir en aide ?

j = Range("B2").SpecialCells(xlLastCell).Row
Set ici = Range("A1:B" & j)

Serge


"Gouap" a écrit dans le message de news:
Bonjour à tous,

Avec l'enregistreur de macro d'Excel j'obtients le code suivant qui fonctionne très bien mais qui ne tri que les ligne 1 à 32. Ce
que je cherche à faire c'est à modifier ce code pour qu'il tri jusqu'à la ligne correspondant à SpecialCells(xlLastCell) sans
devoir mettre ("A1: B65536") mais toutes mes tentatives sont regetées par Excel.

ActiveCell.Offset(0, -2).Columns("A:B").EntireColumn.Select
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Add Key:= _
ActiveCell.Offset(0, 1).Range("A1:A32"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Consolidation").Sort
.SetRange ActiveCell.Range("A1:B32")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Pouvez vous m'aider à trouver la bonne syntaxe ?

@+
Philippe




Avatar
Gouap
Merci de ton aide.

En tenant compte de ton code, j'ai simplifié et obtenu ça qui semble
fonctionner:

Sheets("Consolidation").Select

With ActiveWorkbook.Worksheets("Consolidation").Sort
.SetRange Range("A1:B" & Range("B2").SpecialCells(xlLastCell).Row)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

@+
Philippe

"Tatanka" a écrit dans le message de news:
e%
Bonjour Gouap,

Ces deux instructions peuvent-elles te venir en aide ?

j = Range("B2").SpecialCells(xlLastCell).Row
Set ici = Range("A1:B" & j)

Serge


"Gouap" a écrit dans le message de news:

Bonjour à tous,

Avec l'enregistreur de macro d'Excel j'obtients le code suivant qui
fonctionne très bien mais qui ne tri que les ligne 1 à 32. Ce que je
cherche à faire c'est à modifier ce code pour qu'il tri jusqu'à la ligne
correspondant à SpecialCells(xlLastCell) sans devoir mettre ("A1:
B65536") mais toutes mes tentatives sont regetées par Excel.

ActiveCell.Offset(0, -2).Columns("A:B").EntireColumn.Select
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Add Key:= _
ActiveCell.Offset(0, 1).Range("A1:A32"), SortOn:=xlSortOnValues,
Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Consolidation").Sort
.SetRange ActiveCell.Range("A1:B32")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Pouvez vous m'aider à trouver la bonne syntaxe ?

@+
Philippe








Avatar
FFO
Salut à toi

Mets ceci :

Range("A1:A32")

Comme ceci

Range("A1","A" & Range("A1").SpecialCells(xlLastCell).Row)

Idem pour

Range("A1:B32")

à mettre comme ceci :

Range("A1","B" & Range("A1").SpecialCells(xlLastCell).Row)

Celà devrait faire

Dis moi !!!!!
Avatar
Tatanka
> j = Range("B2").SpecialCells(xlLastCell).Row
Set ici = Range("A1:B" & j)


Je voulais écrire B1 au lieu de B2

Même principe :
j = Range("A1").SpecialCells(xlLastCell).Row
Set ici = Range("A1:A" & j)
...

Serge


"Tatanka" a écrit dans le message de news: e%
Bonjour Gouap,

Ces deux instructions peuvent-elles te venir en aide ?

j = Range("B2").SpecialCells(xlLastCell).Row
Set ici = Range("A1:B" & j)

Serge


"Gouap" a écrit dans le message de news:
Bonjour à tous,

Avec l'enregistreur de macro d'Excel j'obtients le code suivant qui fonctionne très bien mais qui ne tri que les ligne 1 à 32. Ce
que je cherche à faire c'est à modifier ce code pour qu'il tri jusqu'à la ligne correspondant à SpecialCells(xlLastCell) sans
devoir mettre ("A1: B65536") mais toutes mes tentatives sont regetées par Excel.

ActiveCell.Offset(0, -2).Columns("A:B").EntireColumn.Select
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Consolidation").Sort.SortFields.Add Key:= _
ActiveCell.Offset(0, 1).Range("A1:A32"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Consolidation").Sort
.SetRange ActiveCell.Range("A1:B32")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Pouvez vous m'aider à trouver la bonne syntaxe ?

@+
Philippe








Avatar
Caetera
"Gouap" a écrit dans le message de news:


correspondant à SpecialCells(xlLastCell)



*************
Conseil :
Ne JAMAIS utiliser la méthode "SpecialCells(xlLastCell)"

Etc
Avatar
Gouap
Merci de ta proposition, c'est ce que je fais maintenant et effectivement ça
fonctionne !
@+
Philippe

"FFO" a écrit dans le message de news:

Salut à toi

Mets ceci :

Range("A1:A32")

Comme ceci

Range("A1","A" & Range("A1").SpecialCells(xlLastCell).Row)

Idem pour

Range("A1:B32")

à mettre comme ceci :

Range("A1","B" & Range("A1").SpecialCells(xlLastCell).Row)

Celà devrait faire

Dis moi !!!!!