简体   繁体   中英

Sort on font color using vba

I am trying to implement a button that, when pressed sorts an array first alphabetically, then based on font color. The column that I am using to sort has 3 possible values (enrolled, waitlisted, and cancelled). The font color for 'cancelled' is grey. I want to get enrolled at the top of the list, then waitlisted, then cancelled at the bottom. Shouldn't be that difficult, but I can't get the code to work. Here's the code I wrote. Many thanks!

Private Sub btnSort_Click()
Dim SortArray As Range
Dim SortColumn As Range


Set SortArray = Range("A3").CurrentRegion
Set SortColumn = Range(Range("A3").End(xlToRight), Range("A3").End(xlToRight).End(xlDown))

SortArray.Sort Key1:=SortColumn, Header:=xlYes

With SortArray.Sort
    .SortFields.Clear
    .SortFields.Add Key:=SortColumn
    .xlSortOnFontColor
    .SortOnValue.Color = RGB(192, 192, 192)
    .SortOrder = xlAscending
    .Header = xlYes
    .Apply
End With

Since there are only 3 values, we use a helper column and then assign values to it. We then sort and then finally delete the helper column.

Let's say, your data looks like this

在此处输入图片说明

Try this code. I have commneted the code so you should not have a problem in understanding it.

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, lCol As Long
    Dim rng As Range
    Dim ColName As String

    '~~> Change this to the relevant sheet
    Set ws = Sheet1

    With ws
        '~~> Insert a helper column in Col A
        .Columns(1).Insert Shift:=xlToRight
        .Cells(1, 1).Value = "TmpHeader"

        '~~> Get Last Row and last Column
        '~~> I am assuming that headers are in row 1
        lRow = .Range("B" & .Rows.Count).End(xlUp).Row
        lCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        ColName = Split(Cells(, lCol).Address, "$")(1)

        '~~> Insert the formula in Col A
        .Range("A2:A" & lRow).Formula = "=IF(RC[1]=""enrolled"",1,IF(RC[1]=""waitlisted"",2,3))"

        '~~> Set your range
        Set rng = .Range("A1:" & ColName & lRow)

        '~~> Sort it
        rng.Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

        '~~> Delete the helper column
        .Columns(1).Delete
    End With
End Sub

When you run the above code, it inserts a helper column and then inserts a formula =IF(B2="enrolled",1,IF(B2="waitlisted",2,3)) What this basically does is assigns a value of 1,2 and 3 based on the value whether it is enrolled, waitlisted or cancelled.

在此处输入图片说明

Once the formula is inserted, we sort on Col A in ascending order and then finally delete the helper column.

在此处输入图片说明

Figured it out:

ActiveSheet.Range("A3").CurrentRegion.Sort Key1:=Range("I3"), Header:=xlYes

ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add(Range("I3"), _
xlSortOnFontColor, xlDescending, , _
xlSortNormal).SortOnValue.Color = RGB(192, 192, 192)

With ActiveSheet.Sort
   .SetRange Range("A3").CurrentRegion
   .Header = xlYes
   .MatchCase = False
   .Orientation = xlTopToBottom
   .SortMethod = xlPinYin
   .Apply
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM