Slight Amendment To Code

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,783
Office Version
  1. 365
Platform
  1. Windows
I did the code below by recording and want a bit of it amended please. Where it says worksheets 'New' I would like that changed to the active sheet and where it says the range C2:C1262, A1:AY1262 etc I would like that changed to the used range of rows and columns as it will change each sheet I use it on.

Thanks.

Code:
Range("A1").Select
    ActiveWorkbook.Worksheets("New").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("New").Sort.SortFields.Add2 key:=Range("C2:C1262"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("New").Sort.SortFields.Add2 key:=Range("A2:A1262"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("New").Sort
        .SetRange Range("A1:AY1262")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If you are happy to use Row 1 to determine the last column and Column A to determine the last row, then this might work for you.

VBA Code:
Sub SortActiveSheet

    Dim wb As Workbook
    Dim wsAct As Worksheet
    Dim rng As Range
    Dim lr As Long, lc As Long
    
    Set wb = ActiveWorkbook
    Set wsAct = wb.ActiveSheet
    
    With wsAct
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(1, "A"), .Cells(lr, lc))
    End With
    
    wsAct.Range("A1").Select
    wsAct.Sort.SortFields.Clear
    wsAct.Sort.SortFields.Add2 Key:=rng.Cells(1, 3), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    wsAct.Sort.SortFields.Add2 Key:=rng.Cells(1, 1), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With wsAct.Sort
        .SetRange rng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
End Sub
 
Upvote 1
Solution
If you are happy to use Row 1 to determine the last column and Column A to determine the last row, then this might work for you.

VBA Code:
Sub SortActiveSheet

    Dim wb As Workbook
    Dim wsAct As Worksheet
    Dim rng As Range
    Dim lr As Long, lc As Long
   
    Set wb = ActiveWorkbook
    Set wsAct = wb.ActiveSheet
   
    With wsAct
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(1, "A"), .Cells(lr, lc))
    End With
   
    wsAct.Range("A1").Select
    wsAct.Sort.SortFields.Clear
    wsAct.Sort.SortFields.Add2 Key:=rng.Cells(1, 3), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    wsAct.Sort.SortFields.Add2 Key:=rng.Cells(1, 1), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With wsAct.Sort
        .SetRange rng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   
End Sub
Thanks. I meant to say this bit of recorded code is added to other code at the bottom so I just need that bit changed if possible?
 
Upvote 0
I don't understand what you are saying.
That code will work whether you put it at bottom of your existing code or if in your existing code you just use Call SortActiveSheet.
 
Upvote 0
If you are happy to use Row 1 to determine the last column and Column A to determine the last row, then this might work for you.

VBA Code:
Sub SortActiveSheet

    Dim wb As Workbook
    Dim wsAct As Worksheet
    Dim rng As Range
    Dim lr As Long, lc As Long
   
    Set wb = ActiveWorkbook
    Set wsAct = wb.ActiveSheet
   
    With wsAct
        lr = .Cells(Rows.Count, "A").End(xlUp).Row
        lc = .Cells(1, Columns.Count).End(xlToLeft).Column
        Set rng = .Range(.Cells(1, "A"), .Cells(lr, lc))
    End With
   
    wsAct.Range("A1").Select
    wsAct.Sort.SortFields.Clear
    wsAct.Sort.SortFields.Add2 Key:=rng.Cells(1, 3), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    wsAct.Sort.SortFields.Add2 Key:=rng.Cells(1, 1), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With wsAct.Sort
        .SetRange rng
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   
End Sub
Thanks that worked, I got a little confused as I had other dims in my existing code. Much obliged squire.
 
Upvote 0
No problem. If you want to show me your code, I can see if I can use your existing variables.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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