Looking for VBA code to efficiently Sort of Multiple Columns

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
133
Office Version
  1. 2019
Platform
  1. Windows
I originally used the following to code to successfully perform my sort.

Range("g7:S1826").Select
ActiveWorkbook.Worksheets("TeamSelection").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("TeamSelection").Sort.SortFields.Add Key:=Range( _
"k7:k1826"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
ActiveWorkbook.Worksheets("TeamSelection").Sort.SortFields.Add Key:=Range( _
"R7:R1826"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("TeamSelection").Sort
.SetRange Range("g7:S1826")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With


However, I am looking to make my code more efficient and so I am trying to use VBA coding.
But, when I attempted to perform a sort using the following VBA code I got a "Run-time error '1004': To do this, all the merged cells need to be the same size". I don't understand why.

With Sheets("TeamSelection")
.
.
.
Set sortRange = Range("G7:S1826")​
Set keyCell = Range("K7")​
Set keyCell2 = Range("R7")
sortRange.Sort Key1:keyCell, Order1:=xlAscending, Key2:=keyCell2, Order2:=xlAscending, Header:=xlYes
.
.
.
End With


Any assistance would be appreciated.

Thank you,
Don
 
I have a follow-up question if you don't mind... this is related directly to the code discussed above.

In one section of my code I was able to use the code above and (after your suggestions) it worked perfectly. Now... I'm trying to replace code in another section of my program with similar code as above and have come up with another error. The error is...
"sort method of range class failed error 1004"

I'm trying to replace the following code ....
Code:
Range("g7:S501").Select
    ActiveWorkbook.Worksheets("TeamSelection2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("TeamSelection2").Sort.SortFields.Add Key:=Range( _
        "k7:k501"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    ActiveWorkbook.Worksheets("TeamSelection2").Sort.SortFields.Add Key:=Range( _
        "R7:R501"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("TeamSelection2").Sort
        .SetRange Range("g7:S501")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

with this code ...
Code:
With Sheets("TeamSelection2")
        .Range("C7:F501").Copy
        .Range("G7").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
    End With

Any thoughts? It seems like it should be a very clean replacement but I'm obviously missing something.

Thanks,
Don
 
Last edited by a moderator:
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I not sure I understand.
The code you are trying to use doesn't do any sorting, therefore you should not get that error.

Also when posting code, lease use code tags (The # icon in the reply window).
 
Upvote 0
My error sorry... I pasted the wrong code. The code I meant to include and the code I'm trying to use is...
With Sheets("TeamSelection2")
Set sortRange = Range("G7:S501")
Set keyCell = Range("K7")
Set keyCell2 = Range("R7")
sortRange.Sort Key1:=keyCell, Order1:=xlAscending, Key2:=keyCell2, Order2:=xlAscending, Header:=xlYes
End With
 
Upvote 0
You haven't properly qualified the ranges.
Try
Code:
With Sheets("TeamSelection2")
   Set SortRange = .Range("G7:S501")
   Set Keycell = .Range("K7")
   Set Keycell2 = .Range("R7")
   SortRange.Sort Key1:=Keycell, Order1:=xlAscending, Key2:=Keycell2, Order2:=xlAscending, header:=xlYes
End With
 
Upvote 0
That was it! You've been a big help. Thanks again!

Don
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

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