Saturday, August 20, 2011

How to Sort by Cell Color in Excel 2003


1. Press 'Alt and 'F11' together from the Excel worksheet you want to sort. This opens the Visual Basic Editor (VBE).
2. Click 'Insert' and then click 'Module.'
3. Cut and paste the following code into the blank window:Sub SortByColor()On Error GoTo SortByColor_ErrDim sRangeAddress As StringDim sStartCell As StringDim sEndCell As StringDim rngSort As RangeDim rng As RangeApplication.ScreenUpdating = FalsesStartCell = InputBox('Enter the cell address of the ' _'top cell in the range to be sorted by color' _Chr(13) 'i.e. 'A1'', 'Enter Cell Address')If sStartCell > '' ThensEndCell = Range(sStartCell).End(xlDown).AddressRange(sStartCell).EntireColumn.InsertSet rngSort = Range(sStartCell, sEndCell)For Each rng In rngSortrng.Value = rng.Offset(0, 1).Interior.ColorIndexNextRange(sStartCell).Sort Key1:=Range(sStartCell), _Order1:=xlAscending, Header:=xlNo, _Orientation:=xlTopToBottomRange(sStartCell).EntireColumn.DeleteEnd IfSortByColor_Exit:Application.ScreenUpdating = TrueSet rngSort = NothingExit SubSortByColor_Err:MsgBox Err.Number ': ' Err.Description, _vbOKOnly, 'SortByColor'Resume SortByColor_ExitEnd Sub
4. Press 'F5' to run the macro. The macro will ask you the beginning of the range you want to sort by color: enter the top-left cell in the range.

Blogger news