1.copy the data IN SHEET 1 as it is in sheet 3 for comparing expected resllt of col. E and result of macro in col E
2.in sheet 1 delete cells in column E. DO NOT DELETE COLUMN e
this is the source data
3.in sheet 1 a helper column in F that is F2 is 1,F3 is 2 etc till F22
4.copy sheet 1 as desgined to sheet 2 repeat sheet2 so that source data is preserved
the file "breadwiner 130908" is attached as per 1,2,3,4 above
the macro "testone" is in the module
if you run the macro the result will be sheet (expected result column E)
there appears to be some error in your manual ranking of the middle portion that is rows 8 to 14. check once again whether the macro result is correct.
the macro is also appended below for reference
Sub testone()
Dim so As Range, calltype As Range, unq1 As Range, unq2 As Range
Dim cunq1 As Range, cunq2 As Range, r As Range, j As Integer, k As Integer
Dim filt As Range
Dim crm As Range, result As Range
Application.ScreenUpdating = False
Worksheets("sheet1").Cells.Clear
Worksheets("sheet2").Cells.Copy Worksheets("sheet1").Range("A1")
Worksheets("sheet1").Activate
Set r = Range("a1").CurrentRegion
r.Sort key1:=Range("B1"), key2:=Range("C1"), Header:=xlYes
'=============
Set so = Range(Range("B1"), Range("B1").End(xlDown))
Set calltype = so.Offset(0, 1)
Set unq1 = Range("A1").End(xlDown).Offset(5, 0)
Set unq2 = unq1.Offset(0, 1)
so.AdvancedFilter xlFilterCopy, , unq1, True
calltype.AdvancedFilter xlFilterCopy, , unq2, True
Set unq1 = Range(unq1.Offset(1, 0), unq1.End(xlDown))
Set unq2 = Range(unq2.Offset(1, 0), unq2.End(xlDown))
For Each cunq1 In unq1
ActiveSheet.AutoFilterMode = False
r.AutoFilter 2, cunq1
For Each cunq2 In unq2
r.AutoFilter 3, cunq2
Set filt = r.Offset(1, 0).Resize(r.Rows.Count - 1).SpecialCells(xlCellTypeVisible)
Set crm = filt.Columns("A:A")
'msgbox crm.Address
Set result = filt.Columns("E:E")
'msgbox result.Address
j = filt.Rows.Count
For k = 1 To j
result.Cells(k, 1) = WorksheetFunction.Rank(crm.Cells(k, 1), crm, 1) - 1
Next k
Next cunq2
ActiveSheet.AutoFilterMode = False
Next cunq1
ActiveSheet.AutoFilterMode = False
r.Sort key1:=Range("F1"), Header:=xlYes
MsgBox "macro over"
Application.ScreenUpdating = True
End Sub
Bookmarks