
Originally Posted by
romperstomper
1. Which line gives you the error?
2. Why post a picture, rather than a sample workbook? It would also help if you explained why you think the sort won't do what you want, as well as what you actually do want.

Thanks romperstomper for replying.
1. I was able to solve the first problem of the error: Statement invalid outside type block just a few minutes back. I had forgotten to add Dim infront of myRange
2. I need help with the second part that is sort the rows in each column according to color such that the Red colored cells are on the top and the blue colored on the bottom for each column.
I pasted a link of the picture of the worksheet to give an example of the format of the data I am dealing with. This time I attach the worksheet to this post for convinience of the readers.
below is also the edited code that doesnot throw the Statement invalid outside type block error
Sub Macro2()
'
' Macro2 Macro
'
' define the col range
For Each currentHeaderColumn In Worksheets("Sheet1").Range("D1", Selection.End(xlEnd))
Debug.Print "Currently processing Column:" & vbTab & currentHeaderColumn.Column
Dim r As Range
Dim ca As String
ca = Cells(2, currentHeaderColumn.Column).Address
Set r = Range(ca).End(xlDown)
Debug.Print "Total number of rows:" & vbTab & r.Row
' my $ @ %
Dim myRange As Range ' this was the line that gave the errror before
Set myRange = Worksheets("Sheet1").Range(ca, Cells(2, r.Row).Address)
For Each currentCellInCurrentColRange In myRange
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add(Range(myRange), _
xlSortOnCellColor, xlAscending, , xlSortNormal).SortOnValue.Color = RGB(255, 0 _
, 0)
Debug.Print currentCellInCurrentColRange.Value
Next currentCellInCurrentColRange
Next currentHeaderColumn
End Sub
Bookmarks