naturally_data
New Member
- Joined
- Apr 1, 2020
- Messages
- 9
- Office Version
- 2016
- Platform
- Windows
Hi,
Hoping for some help please. I need to be able to sort by row based on the font color of the cell content so that it is sorted left to right by color.
As in the example below, I'd like to sort the cell with red font to the left. I'd like each row to be sorted separately. I've used reecorded macro excel but that has not proven useful.
Regardless of where the data is located I want to be able to select the range I would like to be sorted. The most I've gotten is the VBA shown below. It is not working for me.
Thank you very much in advance for your kind help.
Sub sort_rows_left_to_right_by_color()
Dim wks As Worksheet
Dim rng As Range
Dim i As Long
Set wks = ActiveSheet
Set rng = Application.InputBox("Select range with the mouse", Type:=8)
If Not rng Is Nothing Then
With rng
For i = 1 To .Rows.Count
With .Rows(i)
With wks.Sort
With .SortFields
.Clear
.Add(Key:=.Rows(i).Range("A1")),xlSortOnFontColor, xlAscending, xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
End With
.Sort.Header = xlYes
.Sort.Orientation = xlLeftToRight
.Sort.Apply
End With
Next
End With
End Sub
Hoping for some help please. I need to be able to sort by row based on the font color of the cell content so that it is sorted left to right by color.
As in the example below, I'd like to sort the cell with red font to the left. I'd like each row to be sorted separately. I've used reecorded macro excel but that has not proven useful.
Regardless of where the data is located I want to be able to select the range I would like to be sorted. The most I've gotten is the VBA shown below. It is not working for me.
Thank you very much in advance for your kind help.
Sub sort_rows_left_to_right_by_color()
Dim wks As Worksheet
Dim rng As Range
Dim i As Long
Set wks = ActiveSheet
Set rng = Application.InputBox("Select range with the mouse", Type:=8)
If Not rng Is Nothing Then
With rng
For i = 1 To .Rows.Count
With .Rows(i)
With wks.Sort
With .SortFields
.Clear
.Add(Key:=.Rows(i).Range("A1")),xlSortOnFontColor, xlAscending, xlSortNormal).SortOnValue.Color = RGB(255, 0, 0)
End With
.Sort.Header = xlYes
.Sort.Orientation = xlLeftToRight
.Sort.Apply
End With
Next
End With
End Sub
#1 Column A | #2 Column B | #3 Column C | #4 Column D | #5 Column E | #6 Column F |
1 | 2 | 3 | 4 | 5 | 6 |
10 | 11 | 19 | 67 | 99 | 76 |
00 | 00 | 23 | 32 | 55 | 78 |