Custom Sort in VBA

lopka

New Member
Joined
Feb 7, 2011
Messages
4
Dear all,

I have troubles with sorting.. :-(

Actually what I need is to sort by row... The row is supposed to be picked up from the drop-down list. Let say in C8..

These factors I have in rows 91:97.

I make from these rows graphs by selecting and filtering in drop-down list..
But I have to have the graphs sorted - descending..
I try to modify some code, but it doesn´t work..

HTML:
Range("C8").Select
Selection.Copy
ActiveSheet.Range("$A$90:$S$97").AutoFilter Field:=1, Criteria1:=Selection

    
ActiveWorkbook.Worksheets("Branch selection").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Branch selection").Sort.SortFields.Add Key:=Range( _
"A90:S97"), SortOn:=xlSortOnValues, Order:=xlDescending, CustomOrder:=xlmyVar1, Orientation:=xlLeftToRight, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Branch selection").Sort
        .SetRange Range("A90:S97")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
   End With

I´m pretty unhappy because I really try hard..

Could you please anyone help? Thank you very much!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Can you give an example of actual data, before sorting, and what you'd like it to look like after sorting?
 
Upvote 0
For your example file, try this:
Code:
Private Sub CommandButton1_Click()

Dim myVar1 As String

myVar1 = Range("D2")

ActiveSheet.Range("$A$5:$S$8").AutoFilter
ActiveWorkbook.Worksheets("Example").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Example").Sort.SortFields.Add _
Key:=Range("B5:S8").Offset(Application.Match(myVar1, Range("A6:A8"), 0)).Resize(1) _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Example").Sort
        .SetRange Range("B5:S8")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlLeftToRight
        .SortMethod = xlPinYin
        .Apply
   End With
ActiveSheet.Range("$A$5:$S$8").AutoFilter Field:=1, Criteria1:=myVar1

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,480
Members
452,915
Latest member
hannnahheileen

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