Macro running on every sheet

KarlP

New Member
Joined
Jun 21, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
How can I make this macro run in any worksheet. For now it runs only if the sheet name is "Sheet1 (2)"
I am a beginner and very happy to have created this macro. Now I would like it to work all the time

VBA Code:
Sub TricolonneC()
'
' TricolonneC Macro
'
' Touche de raccourci du clavier: Ctrl+t
'
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    ActiveWorkbook.Worksheets("Sheet1 (2)").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1 (2)").Sort.SortFields.Add2 Key:=ActiveCell. _
        Offset(0, 1).Range("A1:A5"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1 (2)").Sort
        .SetRange ActiveCell.Range("A1:B5")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Last edited by a moderator:

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi & welcome to MrExcel.
How about
VBA Code:
Sub TricolonneC()
'
' TricolonneC Macro
'
' Touche de raccourci du clavier: Ctrl+t
'
   Range(Selection, Selection.End(xlDown)).Select
   Range(Selection, Selection.End(xlToRight)).Select
   With ActiveSheet.Sort
      .SortFields.clear
      .SortFields.Add2 Key:=ActiveCell.Offset(0, 1).Range("A1:A5"), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
      .SetRange ActiveCell.Range("A1:B5")
      .Header = xlGuess
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End Sub
 
Upvote 0
Thanks it's now working on any sheet but it sort only the five first line even if the macro selected many more lines
I've upload an image. You can see that the selection is from B96 to C115 but the sorting occured only from 96 to 100
I guess it's about range that is fix A1:B5. How do we make it variable so it sort all of the selected cells
 

Attachments

  • 2021-06-21_135921.jpg
    2021-06-21_135921.jpg
    85.3 KB · Views: 3
Upvote 0
Ok, how about
VBA Code:
Sub TricolonneC()
'
' TricolonneC Macro
'
' Touche de raccourci du clavier: Ctrl+t
'
   Range(Selection, Selection.End(xlDown)).Select
   Range(Selection, Selection.End(xlToRight)).Select
   With ActiveSheet.Sort
      .SortFields.clear
      .SortFields.Add2 Key:=ActiveCell.Offset(0, 1), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
      .SetRange Selection
      .Header = xlGuess
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
End Sub
 
Upvote 0
Solution
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top