Sorting VBA

Jamm_027

Board Regular
Joined
Sep 10, 2007
Messages
249
I recorded a macro that sorts exactly like I need. Now I want this Macro to work no matter what Sheet within the workbook I am using. Not just the sheet named sheet1. I realize I could change the sheet name within the code but I dont want to repeat that over and over. And I know there is a way to clean this code up?????

Code:
 Range("C6:H300").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("H6:H300"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D6:D300"), _
       SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
       .SetRange Range("C6:H300")
      .Header = xlGuess
     .MatchCase = False
    .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("C3").Select
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
To sort the active worksheet,
Code:
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("H6"), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("D6"), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
        .Sort.SetRange .Range("C6:H300")
 
        With .Sort            
            .Header = [COLOR=red]xlGuess[/COLOR]
            .MatchCase = False
            .Orientation = xlTopToBottom
            .Apply
        End With
    End With

You should change xlGuess to xlYes or xlNo according to whether your data has headers.
 
Upvote 0
Thanks SHG,

I am getting a run time error 450: Wrong number of arguments or invalid property assignment

then when I debug it has the row highlighted:

.SetRange .Range("C6:H300")
 
Upvote 0
Code:
[COLOR=red].Sort[/COLOR].SetRange .Range("C6:H300")
I just ran the code copied directly from the post - try again.
 
Upvote 0
I am getting a run time error 450 still saying wrong number of arguments???


Code:
Sub Sort_Tasks()

Application.ScreenUpdating = False
    
    With ActiveSheet
        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=.Range("H6"), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=.Range("D6"), _
                             SortOn:=xlSortOnValues, _
                             Order:=xlAscending, _
                             DataOption:=xlSortNormal
        With .Sort
            .SetRange.Range ("C6:H300")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .Apply
        End With
    End With

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,604
Messages
6,179,857
Members
452,948
Latest member
UsmanAli786

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