实现功能:
销售数据在全局排名、在相同子类(可以是区域、分类)再对销售进行各自排名
k列(11)为销售数据,A列为子类,全局排名数据存放到第15列,子类排名存放到第14列
可以对A、K列设为变量再赋值,提高代码移植的便利性
Sub SalesSeqence()
'
' seqence Macro
' 宏由 mike 录制
'
Dim RowN As Integer
Dim i As Integer
Dim colqA as integer,colqK as integer '2个参照数据列数
Dim colA as integer '全局排名填充列
Dim colB as integer '子类排名填充列
colqA=1
colqK=11
colA=15
colB=14
'获取数据行数,减少后面的循环次数
For i = 1 To 20000
If Cells(i, colqK).Value <> "" Then
RowN = i
End If
Next i
'子类不全时填充全--数据特殊格式可以略过
For i = 3 To RowN
If Cells(i, colqK).Value <> "" And Cells(i, colqA) = "" Then
Cells(i,colqA) = Cells(i - 1, colqA)
End If
Next i
'---------------------进行全局排名
Columns("K:K").Select
Range("A1:O" & RowN).Sort Key1:=Range("K1"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
SortMethod:=xlPinYin, DataOption1:=xlSortNormal
'第一遍遍历
Cells(2, colA).Value = 1
For i = 3 To RowN
If Cells(i, colqK).Value <> "" Then
Cells(i, colA).Value = Cells(i - 1, colA).Value + 1
End If
Next i
'第二遍合并相同名次
For i = 3 To RowN
If Cells(i, colqK).Value <> "" And Cells(i,colqK).Value = Cells(i - 1,colqK).Value Then
Cells(i, colA).Value = Cells(i - 1, colA).Value
End If
Next i
'----------------在子类进行排名
Columns("k:k").Select
Range("A1:O" & RowN).Sort Key1:=Range("k1"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
SortMethod:=xlPinYin, DataOption1:=xlSortNormal
Columns("A:A").Select
Range("A1:O" & RowN).Sort Key1:=Range("A1"), Order1:=xlDescending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
SortMethod:=xlPinYin, DataOption1:=xlSortNormal
Cells(2, colB).Value = 1
For i = 3 To RowN
If Cells(i, colqK).Value <> "" And Cells(i, colqA).Value = Cells(i - 1,colqA).Value Then
Cells(i, colB).Value = Cells(i - 1, colB).Value + 1
ElseIf Cells(i, colqK).Value <> "" And Cells(i, colqA).Value <> Cells(i - 1, colqA).Value Then
Cells(i, colB).Value = 1
End If
Next i
For i = 3 To RowN
If Cells(i, colqK).Value <> "" And Cells(i, colqA).Value = Cells(i - 1, colqA).Value And Cells(i, colqK).Value = Cells(i - 1, colqK).Value Then
Cells(i, colB).Value = Cells(i - 1, colB).Value
End If
Next i
End Sub