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

vba select case ?

2 réponses
Avatar
j-pascal
Bonjour,

Peut-on utiliser des "select case" (que je n'utilise jamais,
probablement à tort !?) pour optimiser le code suivant :

'-----
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim MaPlage As Variant

MaPlage = Range("A3:K" & Application.CountA(Range("G:G")) +
2).Address
'---
If Not Application.Intersect(Target, [a2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[a3] ', Order1:=xlAscending

If Not Application.Intersect(Target, [b2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[b3]

If Not Application.Intersect(Target, [c2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[c3]

If Not Application.Intersect(Target, [d2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[d3]

If Not Application.Intersect(Target, [e2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[e3]

If Not Application.Intersect(Target, [f2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[f3]

If Not Application.Intersect(Target, [g2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[g3]

If Not Application.Intersect(Target, [h2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[h3]

If Not Application.Intersect(Target, [i2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[i3]

If Not Application.Intersect(Target, [j2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[j3]

If Not Application.Intersect(Target, [k2]) Is Nothing Then
Range(MaPlage).Sort Key1:=[k3]

'---
If Intersect(Target, [a3:a2000]) Is Nothing Then Exit Sub
Res = Target
End Sub
'------

Merci d'avance pour vos lumières,

JP

PS : pouvez-vous me confirmer que "Order1:=xlAscending" est l'ordre par
défaut et qu'il n'y a aucune incidence à l'enlever (dès lors qu'on trie
en ordre croissant bien sûr !) ?

2 réponses

Avatar
FFO
Salut Pascal

Je te propose ceci

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim MaPlage As Variant
MaPlage = Range("A3:K" & Application.CountA(Range("G:G")) +
2).Address
For i = 1 to 11
If Not Application.Intersect(Target, Cells(2,i) Is Nothing Then
Range(MaPlage).Sort Key1:Îlls(3,i) ', Order1:=xlAscending
Next
'---
If Intersect(Target, [a3:a2000]) Is Nothing Then Exit Sub
Res = Target
End Sub

Celà fais la même chose mais en écriture plus simple

Je te confirme que si il n'est pas spécifié le paramètre "xlAscending" est
par défaut l'ordre croissant

Espérant avoir répondu à tes attentes
Dis moi !!!
Avatar
j-pascal
Bonsoir,

C'est génial ! Merci bcp !

(Je m'initierai aux "select case" une prochaine fois ;-) )

JP

Salut Pascal

Je te propose ceci

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim MaPlage As Variant
MaPlage = Range("A3:K" & Application.CountA(Range("G:G")) +
2).Address
For i = 1 to 11
If Not Application.Intersect(Target, Cells(2,i) Is Nothing Then
Range(MaPlage).Sort Key1:Îlls(3,i) ', Order1:=xlAscending
Next
'---
If Intersect(Target, [a3:a2000]) Is Nothing Then Exit Sub
Res = Target
End Sub

Celà fais la même chose mais en écriture plus simple

Je te confirme que si il n'est pas spécifié le paramètre "xlAscending" est
par défaut l'ordre croissant

Espérant avoir répondu à tes attentes
Dis moi !!!