zone709

Well-known Member
Joined
Mar 1, 2016
Messages
2,079
Office Version
  1. 365
Platform
  1. Windows
Hi this works fine right, but I'm trying to change the range as my sheet extended a little. So I don't have to keep changing it I want to change range to

C4:C B4:B & B4:AH

if I change it all it breaks at each spot range above. I think there is something small I'm missing and cant figure it out. Thanks


Code:
Sub Sorting1224()

Dim keyRange As Variant
Dim sortNum As Long

keyRange = Array("Office", "Field Intern", "Trainer", "Supervisor", "Assistant", "Safety", "Super", "Super - Local 15D", "Assistant Field Engineer")
Application.AddCustomList ListArray:=keyRange
sortNum = Application.CustomListCount
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("[COLOR=#b22222]C4:C200[/COLOR]"), _
    SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:=sortNum, DataOption:=xlSortNormal

 ActiveWorkbook.Worksheets("Data").Sort.SortFields.Add Key:=Range("[COLOR=#b22222]B4:B200[/COLOR]"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Data").Sort
        .SetRange Range("[COLOR=#b22222]B4:AH200[/COLOR]")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Range("B4").Select
End Sub
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
How about ...

Code:
Sub Sorting1224()
  Dim nList         As Long
  Dim wks           As Worksheet

  Application.AddCustomList ListArray:=Array("Office", "Field Intern", "Trainer", "Supervisor", "Assistant", "Safety", "Super", "Super - Local 15D", "Assistant Field Engineer")
  nList = Application.CustomListCount

  Set wks = Worksheets("Data")

  With wks.Sort
    .SortFields.Clear
    .SortFields.Add Key:=wks.Range("C4"), CustomOrder:=nList
    .SortFields.Add Key:=wks.Range("B4")
    .SetRange wks.Range("B4", wks.Cells(Rows.Count, "AH"))
    .Header = xlYes  ' (or xlNo; why guess?)
    .MatchCase = False
    .Orientation = xlTopToBottom
    .Apply
  End With

  Application.DeleteCustomList nList
End Sub
 
Upvote 0
Hi ok this works great. I don't know why the guess part. I think when I first recorded part of this awhile back it was added. I didn't add it at all. Not sure and didn't really look into it. I do have header on sheet from row 1 to 3 why I'm starting at all 4's
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,435
Members
448,898
Latest member
dukenia71

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