Auto sort sheet while excluding specific rows

butlerkd52

New Member
Joined
Jun 27, 2017
Messages
3
Hi, first time posting! I'm attempting to auto sort sheet 2(col A, then col B) alphabetically that is pre-populated based on criteria selected from sheet 1 while ignoring specific keywords(Select) in sheet 2 and pushing those rows to the bottom of the list. I have been able to sort the data A-Z, but I cannot figure out the exclusion part of it.

What would work best, formula or macro?

Thanks in advance for your help!


The first table represents the raw data while the second table represents what I want the list to be sorted to. Col A sorted A-Z, then Col B sorted A-Z, all while ignoring everything that starts with "Select" and moves them to the bottom of the list.

Header 1Header 2
VegetableCarrot
Select Fruit or VegetableSelect Fruit or Vegetable
FruitPineapple
FruitApple
VegetableOnion
Select Fruit or VegetableSelect Fruit or Vegetable

<tbody>
</tbody>

Header 1Header 2
FruitApple
FruitPineapple
VegetableCarrot
VegetableOnion
Select Fruit or VegetableSelect Fruit or Vegetable
Select Fruit or VegetableSelect Fruit or Vegetable

<tbody>
</tbody>
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Ok, this one should work. It's not the most elegant solution and I wouldn't recommend it with really large data sets, but it should do it.

Code:
Sub cSort(r As Range)
Dim AR()
AR = r.Value
Dim tmp As String
Dim sCnt As Long
For i = 1 To UBound(AR)
    If InStr(AR(i, 1), "Select") > 0 Then
        sCnt = sCnt + 1
    End If
Next i
For i = 1 To UBound(AR)
    For j = i To UBound(AR)
        If InStr(AR(i, 1), "Select") > 0 Then
            tmp = AR(i, 1)
            AR(i, 1) = AR(j, 1)
            AR(j, 1) = tmp
        End If
    Next j
Next i
For i = 1 To UBound(AR) - sCnt
    For j = i To UBound(AR) - sCnt
        If AR(i, 1) > AR(j, 1) Then
            tmp = AR(i, 1)
            AR(i, 1) = AR(j, 1)
            AR(j, 1) = tmp
        End If
    Next j
Next i
r.Value = AR
End Sub
Sub test()
cSort Range("A2:A7")
End Sub
 
Upvote 0
Here's another way:
Code:
Sub a1011678a()
Dim ra As Long, rb As Long, rr As Long
rr = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
Range(Cells(1, "A"), Cells(rr, "B")).Sort key1:=Range("A1"), order1:=xlAscending, key2:=Range("B1"), order2:=xlAscending, _
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

ra = Range("A:A").Find("Select*", SearchOrder:=xlByRows, SearchDirection:=xlNext).row
rb = Range("A:A").Find("Select*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row

Range(Cells(ra, "A"), Cells(rb, "B")).Cut Cells(rr + 1, "A")
Range(Cells(rb + 1, "A"), Cells(rr + rb - ra + 1, "B")).Cut Cells(ra, "A")

End Sub
 
Upvote 0
Good posts... Here's another way too:

Code:
Sub SpecialSort()
Dim rng As Range


Set rng = [A1].CurrentRegion


rng.Replace What:="select", Replacement:="ZZZZZZ", LookAt:=xlPart, _
        SearchOrder:=xlByRows
rng.Sort Key1:=[A1], Order1:=xlAscending, Key2:=[B1], Order2:=xlAscending, Header:=xlYes
rng.Replace What:="ZZZZZZ", Replacement:="Select", LookAt:=xlPart, _
        SearchOrder:=xlByRows
End Sub
 
Upvote 0
Good posts... Here's another way too:

Code:
Sub SpecialSort()
Dim rng As Range


Set rng = [A1].CurrentRegion


rng.Replace What:="select", Replacement:="ZZZZZZ", LookAt:=xlPart, _
        SearchOrder:=xlByRows
rng.Sort Key1:=[A1], Order1:=xlAscending, Key2:=[B1], Order2:=xlAscending, Header:=xlYes
rng.Replace What:="ZZZZZZ", Replacement:="Select", LookAt:=xlPart, _
        SearchOrder:=xlByRows
End Sub



Hi CalcSux78, I am now in need of an another header row to my data set and I am unable to get you code to work as expected. I've tried a few variations of changing the rng, Key's, and Header with no luck. How would you sort the same dataset, but with 2 header rows?

Thanks!
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,825
Members
449,190
Latest member
rscraig11

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