Sort condition required

dlee83

New Member
Joined
Mar 11, 2022
Messages
17
Office Version
  1. 365
Platform
  1. Windows
Hello,

Any help is appreciated.

I have a simple sort function sub that works for a set order using custom sort. Fine

Range("V3:AE52").Select
ActiveWorkbook.ActiveSheet.Sort.SortFields.clear
ActiveWorkbook.ActiveSheet.Sort.SortFields.Add2 key:=Range( _
"x2:x52"), SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
Application.CustomListCount, _
DataOption:=xlSortNormal
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range("v2:ae52")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("a1").Select

I can add the customer sort order after CustomOrder:= which works, or add an application with the array, which is what's above.
Now what I want to do is determine the sort order depending on the value of cell "v3"

Just for simplicity on a solution:
If cell value is "A" - Sort order needs to be "B", "C", "D", "E", "F", "A"
If cell value is "B" - Sort order needs to be "C", "D", "E", "F", "A", "B"
If cell value is "C" - Sort order needs to be "D", "E", "F", "A", "B", "A"
Etc etc.

I've tried adding If's to.....

Application.AddCustomList _
ListArray:=Array("B", "C", "D", "E", "F", "A")
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
How about
VBA Code:
   Dim Srt As Variant
   
   Select Case Range("V3")
      Case "A": Srt = Array("B", "C", "D", "E", "F", "A")
      Case "B": Srt = Array("C", "D", "E", "F", "A", "B")
   End Select
   With ActiveSheet.Sort
      .SortFields.Clear
      .SortFields.Add2 Range("X2"), xlSortOnValues, xlAscending, Join(Srt, ","), xlSortNormal
      .SetRange Range("v2:ae52")
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
   End With
There is no need to add anything to the customsort list
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,839
Messages
6,121,887
Members
449,057
Latest member
Moo4247

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