+ Reply to Thread
Results 1 to 11 of 11

Sort by colour

Hybrid View

josros Sort by colour 05-13-2022, 02:15 PM
6StringJazzer Re: Sort by colour 05-13-2022, 03:32 PM
nimrod1313 Re: Sort by colour 05-13-2022, 05:03 PM
josros Re: Sort by colour 05-13-2022, 06:12 PM
Artik Re: Sort by colour 05-13-2022, 06:25 PM
josros Re: Sort by colour 05-13-2022, 07:01 PM
6StringJazzer Re: Sort by colour 05-13-2022, 09:56 PM
Artik Re: Sort by colour 05-13-2022, 07:13 PM
nimrod1313 Re: Sort by colour 05-14-2022, 04:07 AM
kvsrinivasamurthy Re: Sort by colour 05-14-2022, 09:25 AM
josros Re: Sort by colour 05-16-2022, 12:52 PM
  1. #1
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    OFFICE 365
    Posts
    184

    Sort by colour

    Hi,

    I have the code below that I used Macro Recorder the problem having is sorting the headers as well the data Start at cell "B14" and "B12" and "B13" are headers.

    how to modify it that:

    1. Sort but not including headers
    2. Cells that are blank no colors to be at the bottom. (attaching sample of file)

    here is the code:

    Sub sort_bycolours()
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Clear
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, _
     255, 0)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(146, _
     208, 80)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(112, 48 _
     , 160)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(31, 73 _
     , 125)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(204, _
     153, 0)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(192, 0 _
     , 0)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(148, _
     138, 84)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(218, _
     150, 148)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(141, _
     180, 226)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(38, 38 _
     , 38)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(217, _
     217, 217)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(252, _
     213, 180)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add(Range("B16:B55"), _
     xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(128, _
     128, 128)
     ActiveWorkbook.Worksheets("STATEMENT").Sort.SortFields.Add2 Key:=Range( _
     "B16:B55"), SortOn:=xlSortOnCellColor, Order:=xlAscending, DataOption:= _
     xlSortNormal
     With ActiveWorkbook.Worksheets("STATEMENT").Sort
     .SetRange Range("STMTRNG")'("B14").CurrentRegion
     .Header = xlNo
     .MatchCase = False
     .Orientation = xlTopToBottom
     .SortMethod = xlPinYin
     .Apply
     End With
    End Sub

    thank you,
    Attached Files Attached Files
    Last edited by 6StringJazzer; 05-13-2022 at 03:26 PM. Reason: Moved from Excel - New Users/Basics

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Sort by colour

    Change this
     .Header = xlNo
    to this
     .Header = xlYes
    to indicate that your data has headers that not to be sorted.

    For no color on the bottom change xlAscending to xlDescending
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Sort by colour

    Building on 6StringJazzer keen observations

    Sub sort_bycolours()
        Dim sortRange As Range
        Dim targSheet As Worksheet
        Const sortOrder As Integer = xlDescending
        
        Set targSheet = ActiveWorkbook.Worksheets("STATEMENT")
        Set sortRange = targSheet.Range("B16:B55")
        
        With targSheet.Sort
            .SortFields.Clear
     
            With .SortFields.Add(sortRange, xlSortOnCellColor, sortOrder, , xlSortNormal).SortOnValue
                .Color = RGB(255, 255, 0)
                .Color = RGB(146, 208, 80)
                .Color = RGB(112, 48, 160)
                .Color = RGB(31, 73, 125)
                .Color = RGB(204, 153, 0)
                .Color = RGB(192, 0, 0)
                .Color = RGB(148, 138, 84)
                .Color = RGB(218, 150, 148)
                .Color = RGB(141, 180, 226)
                .Color = RGB(38, 38, 38)
                .Color = RGB(217, 217, 217)
                .Color = RGB(252, 213, 180)
                .Color = RGB(128, 128, 128)
            End With
            
            .SortFields.Add2 key:=sortRange, SortOn:=xlSortOnCellColor, Order:=sortOrder, DataOption:=xlSortNormal
            .SetRange targSheet.Range("B14").CurrentRegion
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Last edited by nimrod1313; 05-13-2022 at 05:10 PM.

  4. #4
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    OFFICE 365
    Posts
    184

    Re: Sort by colour

    Thank you.

    I tried but getting this error:


    Run-time error '1004':

    The sort reference is not valid. Make sure that it's within the data you want to sort, and the first sort by box isn't the same or blank and highlight this line:

    .Apply
    Thanks again.

  5. #5
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,542

    Re: Sort by colour

    Are you still using the MSO 2007 version?

    Artik

  6. #6
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    OFFICE 365
    Posts
    184

    Re: Sort by colour

    Microsoft office 365 (office 2016)

    Thanks.

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: Sort by colour

    Quote Originally Posted by josros View Post
    Microsoft office 365 (office 2016)
    Those are two different versions. Are you using both?

    Please update your member profile to reflect what version you are actually using.

  8. #8
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,542

    Re: Sort by colour

    On the test data presented, no error is reported at my site.

    Artik

  9. #9
    Valued Forum Contributor
    Join Date
    05-03-2022
    Location
    Halifax,Canada
    MS-Off Ver
    365
    Posts
    326

    Re: Sort by colour

    Hello JosRos:
    One reason Error will occur is if the code is attempting to reference a range that it cannot access .
    With your code you have it referencing the "ActiveWorkBook".
    If you have a second workbook open and its the activeWorkBook when you run the code , then you will get this error. (at least I did when I tried this scenario)
    I changed the following line , in this scenario, and the problem went away.

    I changed
    Set targSheet = ActiveWorkbook.Worksheets("STATEMENT")
    to ...
    Set targSheet = ThisWorkbook.Worksheets("STATEMENT")

    So now you are referencing sheets in workbook that contains the macro vs the activeWorkBook .

    NOTE: This is one reason to clean code up. As you notice I only needed to make a single change to code, to test this scenario. In your code you'd have to make updates in over a dozen places,
    and missing any single place you'd have only caused more problems. (i.e. the DRY coding principle aka Dont Repeat Yourself)

    WARNING:
    Once you get this code working, lines you may want to reconsider, if your data is dynamic in size, would be: Set sortRange = targSheet.Range("B16:B55") as well as targSheet.Range("B14").CurrentRegion.
    BUT only need to consider if the range of rows and columns is dynamic in size and/or position.


    Sub sort_bycolours()
        Dim sortRange As Range
        Dim targSheet As Worksheet
        Const sortOrder As Integer = xlDescending
        
        ' Set targSheet = ActiveWorkbook.Worksheets("STATEMENT") << OLD STATEMENT
        Set targSheet = ThisWorkbook.Worksheets("STATEMENT")
        Set sortRange = targSheet.Range("B16:B55")
        
        With targSheet.Sort
            .SortFields.Clear
     
            With .SortFields.Add(sortRange, xlSortOnCellColor, sortOrder, , xlSortNormal).SortOnValue
                .Color = RGB(255, 255, 0)
                .Color = RGB(146, 208, 80)
                .Color = RGB(112, 48, 160)
                .Color = RGB(31, 73, 125)
                .Color = RGB(204, 153, 0)
                .Color = RGB(192, 0, 0)
                .Color = RGB(148, 138, 84)
                .Color = RGB(218, 150, 148)
                .Color = RGB(141, 180, 226)
                .Color = RGB(38, 38, 38)
                .Color = RGB(217, 217, 217)
                .Color = RGB(252, 213, 180)
                .Color = RGB(128, 128, 128)
            End With
            
            .SortFields.Add2 Key:=sortRange, SortOn:=xlSortOnCellColor, Order:=sortOrder, DataOption:=xlSortNormal
            .SetRange targSheet.Range("B14").CurrentRegion
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    Last edited by nimrod1313; 05-14-2022 at 04:47 AM.

  10. #10
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: Sort by colour

    Try this.
    Change this
    Set sortRange = targSheet.Range("B16:B55")
    as
    Set sortRange = targSheet.Range("B14")
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  11. #11
    Forum Contributor
    Join Date
    11-05-2012
    Location
    Canada
    MS-Off Ver
    OFFICE 365
    Posts
    184

    Re: Sort by colour

    Thank you all for your help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] sort table on colour and than on value
    By MaartenRo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-30-2020, 04:11 AM
  2. How to Sort by Colour
    By sunplum in forum Excel General
    Replies: 1
    Last Post: 01-23-2013, 06:57 PM
  3. can i sort rows by colour?
    By nickymac in forum Excel General
    Replies: 4
    Last Post: 09-30-2008, 02:09 PM
  4. [SOLVED] Sort on infill colour
    By Lewej in forum Excel General
    Replies: 3
    Last Post: 08-15-2006, 10:25 AM
  5. Excel should let me sort by colour
    By kbatey25 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-09-2005, 06:34 AM
  6. [SOLVED] Sort by colour
    By Mark Fox in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-07-2005, 03:05 PM
  7. [SOLVED] sort by colour
    By Gers in forum Excel General
    Replies: 1
    Last Post: 04-01-2005, 10:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1