How Do I Do A Custom Sort

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I want to sort a file by a particular column first. I need it to be sorted with a particular word first but that word may be anywhere within the cell.

Obviously the normal sort would do it by the first letter of the first word in the cell either A-Z or Z-A. But I need it to look for a particular word in the cell and have that first. Is it possible?
 
What about the sheet name?
The code loops until it finds an empty cell, are there values in the cells?
See highlighted below for edits.
Rich (BB code):
   Set rng = Sheets("Sheet1").Range("A2")
   Do Until rng = ""
      For i = LBound(arr) To UBound(arr)
         If InStr(1, rng.Value, Trim(arr(i)), vbTextCompare) Then
            Sheets("Sheet1").Range("AL" & rng.Row).Value = i + 1
         End If
      Next
      Set rng = rng.Offset(1, 0)
   Loop
 
Upvote 0

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
It is called sheet1, and there are no empty cells in either column C or AD
 
Upvote 0
Peter's code place a formula over a range of cells. If you want to place a different formula in each cell the only way I can think of doing so is to loop through the data.

I have changed the column references in my working sheet and it seems to work at my end. I have also added a sort routine.
Here is a link to my worksheet.
https://docs.google.com/file/d/0B5BFC6zPG_P0QzhzclVQX0RpeE0/edit?usp=sharing

The code will go into a standard module.
Rich (BB code):
Sub Main()
   ProcessDummyList
   SortData
End Sub




Private Sub ProcessDummyList()
   Dim rng As Range
   Dim myList As String
   Dim i As Long
   Dim arr As Variant
   
   myList = InputBox("Enter a list separated by commas (,)")
   If myList = "" Then
      MsgBox "Nothing entered!"
      Exit Sub
   End If
   
   arr = Split(myList, ",")
   
   Set rng = Sheets("Sheet1").Range("C2")
   Do Until rng = ""
      For i = LBound(arr) To UBound(arr)
         If InStr(1, rng.Value, Trim(arr(i)), vbTextCompare) Then
            Sheets("Sheet1").Range("AD" & rng.Row).Value = i + 1
         End If
      Next
      Set rng = rng.Offset(1, 0)
   Loop


   Set rng = Nothing
End Sub


Private Sub SortData()
   Sheets("Sheet1").UsedRange.Select
   Selection.Sort Key1:=Range("AD1"), Order1:=xlAscending, Key2:=Range("C1") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom
End Sub
 
Upvote 0
I have looked at your sample sheet and it has nothing in AD? Thats where the words will be that need sorting like coils, leads etc.. Column C will just be a normal A-Z sort then it sorts column AD by the words.
 
Upvote 0
This is sample data to show you how the code works. Did you run the code? Does it do what you want wrt "Leads" and "Coils"?

If so, then you need to edit the code to meet your needs.
Press F8 to step through the code.
The column you are going to loop through is the column containing the text "Leads", "Coils", etc.
Edit the column in this line if necessary;
Rich (BB code):
Set rng = Sheets("Sheet1").Range("C2")

The dummy sort variable is output to an empty column.
Edit this line if necessary:
Rich (BB code):
Sheets("Sheet1").Range("AD" & rng.Row).Value = i + 1

Edit the order of the SortData() procedure if necessary:
NB: in this example, AD contains the dummy sort variables.
Rich (BB code):
Private Sub SortData()
   Sheets("Sheet1").UsedRange.Select
   Selection.Sort _
      Key1:=Range("AD1"), Order1:=xlAscending, _
      Key2:=Range("C1"), Order2:=xlAscending, _
      Header:=xlGuess, _
      OrderCustom:=1, _
      MatchCase:=False, _
      Orientation:=xlTopToBottom
End Sub
 
Upvote 0
This is really confusing me now. Whats all this dummy data? All columns have data in so I dont have any dummy data. The sort needs to do column C first A-Z this is a must. Then it needs to sort column AD, the the code needs to ask me 'what order do you want the words sorted' in the box. I appreciate all you are doing but I have told you the columns, so I shouldn't have to edit any of the code which is confusing me.

Thanks.
 
Upvote 0
Did you even attempt to follow the instructions in post #35?

I have made the adjustments you outlined in your last post. It is working at my end.
Make a copy of your workbook and try running the code.
Rich (BB code):
Option Explicit


Sub Main()
   ProcessDummyList
   SortData
End Sub


Private Sub ProcessDummyList()
   Dim rng As Range
   Dim myList As String
   Dim i As Long
   Dim arr As Variant
   
   myList = InputBox("Enter a list separated by commas (,)")
   If myList = "" Then
      MsgBox "Nothing entered!"
      Exit Sub
   End If
   
   arr = Split(myList, ",")
   
   Set rng = Sheets("Sheet1").Range("AD2")
   Do Until rng = ""
      For i = LBound(arr) To UBound(arr)
         If InStr(1, rng.Value, Trim(arr(i)), vbTextCompare) Then
            Sheets("Sheet1").Range("AL" & rng.Row).Value = i + 1
         End If
      Next
      Set rng = rng.Offset(1, 0)
   Loop


   Set rng = Nothing
End Sub


Private Sub SortData()
   Sheets("Sheet1").UsedRange.Select
   Selection.Sort _
      Key1:=Range("C1"), Order1:=xlAscending, _
      Key2:=Range("AD1"), Order2:=xlAscending, _
      Header:=xlGuess, _
      OrderCustom:=1, _
      MatchCase:=False, _
      Orientation:=xlTopToBottom
End Sub
 
Upvote 0
I cant get it to work, column AD wont go in the order I enter the words in the box and also the headers disappear. I don't want to take up anymore of your time. Thanks anyway.
 
Upvote 0
Final thoughts:
Not sorting in the correct order?
Try reversing the order you enter the values into the InputBox.


Header Disappearing - In the SortData() procedure, change highlighted to xlYes.
Rich (BB code):
Header:=xlGuess, _
 
Upvote 0

Forum statistics

Threads
1,215,267
Messages
6,123,964
Members
449,137
Latest member
yeti1016

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