How to sort 2 columns in order?

danbates

Active Member
Joined
Oct 8, 2017
Messages
377
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Please can someone help me?

I am trying to sort 2 columns in order. Column F has values in generated by formulas and column E has a coloured cells.

What I would like is column F to sort in ascending order and column E to sort with it so the values before it sorts are still next the the right coloured cells after it has sorted.

Thanks
Dan
 

Attachments

  • Capture.PNG
    Capture.PNG
    3.3 KB · Views: 7

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If you sort with both columns selected then it might work. It would definitely work if you didn't have formulas in the second column, but some formulas don't play well with sorting.
 
Upvote 0
Hi,
yeah I've tried that but it doesn't give me the correct order. It just swaps the green and yellow cells.

I have just tried copying the numbers and then pasting them in the same location as numbers and that works.

Don't you think there is any way around this without converting the formulas to actual numbers?
 
Upvote 0
Don't you think there is any way around this without converting the formulas to actual numbers?
I think that it is unlikely, but without knowing what the formulas are, or where they are pulling the values from it is impossible to say.
 
Upvote 0
This works for simple formulas in the Col F cells provided you are happy adding text of the same colour to the Col E cells (or I guess it could be deleted after):
VBA Code:
Sub SortColour()
    Dim n As Integer
    Dim c As Integer
    Dim d As Object
    Set d = CreateObject("Scripting.Dictionary")

    For n = 2 To 5
        Cells(n, 5) = Cells(n, 6).Value
        c = Range("E" & Trim(Str(n))).Interior.ColorIndex
        Range("E" & Trim(Str(n))).Font.ColorIndex = c
        d.Add Key:=Range("E" & Trim(Str(n))).Value, Item:=Range("F" & Trim(Str(n))).Formula
    Next n
    Sheet1.Sort.SortFields.Clear
    Sheet1.Sort.SortFields.Add2 Key:=Range("E2:E5") _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("E2:F5")
        .Header = xlGuess
        .Apply
    End With
    For n = 2 To 5
        Range("F" & Trim(Str(n))).Formula = d(Cells(n, 5).Value)
    Next n

End Sub
Note it's only for the E2:F5 range, but could obviously be extended. Note too it relies on the values being unique.
Input:
Book2
EFGH
2253
3341
443-1
5154
Sheet1
Cell Formulas
RangeFormula
F2:F5F2=G2-H2

Output:
Book2
EFGH
21153
32241
4333-1
54454
Sheet1
Cell Formulas
RangeFormula
F2F2=G5-H5
F3:F5F3=G2-H2
 
Upvote 0
Hi,

I've managed to sort it luckily. The problem I had is that I hadn't made all my cell references in the formulas obsolute.

Thank you very much for your help and suggestions, I do appreciate you helping me.

Thanks

Dan
 
Upvote 0

Forum statistics

Threads
1,214,970
Messages
6,122,514
Members
449,088
Latest member
RandomExceller01

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