I am trying to sort a number of columns in my worksheet based on the Cell Color. The columns have variable length of rows.
I have two questions:
1. When I run my current code in debug mode then I get the error: "Statement invalid outside type block". I am not sure which parantheses I should remove to get rid of the error.
2.Ssecondly I think my current code is not going to do the sort that I would like it to do. Can anyone please suggest what changes I should make in my code to get it working.
Below I have pasted a sample of my sheet as a link:
http://picasaweb.google.com/manishab...14238835510658
![]()
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 myRange As Range 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
Bookmarks