当有重复值时如何返回数组中每个双精度的等级?

问题描述 投票:0回答:1

如果我的输入数组为(10,10,20,20,30,30,40,40,50,50),我想返回一个简单的代码片段(1,2,3,4,5, 6,7,8,9,10)。我正在尝试实现ABC分类,目前正在使用此代码:

    For i = 1 To noA 'noA number of items should be classified as "A"
        'return the item number with the i'th largest number in arrTembABC
        intTemp = Application.Match(WorksheetFunction.Large(arrTempABC, i), arrTempABC, True)
        'assign the category "A" to 'intTemp' item number
        SKUinfo(intTemp, 12) = "A"
    Next i

    'some printing code

上面的代码的问题是,当有重复的值时,它总是返回第一个实例的位置。具有相同值的其他任何项目编号都不会分配类别。

因此,对于上面讨论的数组(请参见“ avg值”),该代码仅对每个重复值的第一个实例进行分类,其余均为空白(请参见“ ABC_CategCry”)]

arrays excel vba ranking
1个回答
0
投票

我求助于对保存值的数组进行排序,同时将它们的位置保存在单独的数组中。我在下面发布整个子项目,该子项目基于数量或价值(=平均数量*成本)进行ABC分类。我试图以一种有益的方式发表评论,但对任何需要澄清的问题或对如何提高清晰度的评论表示赞赏。

Sub ABCclass(ByRef swtcABCclass As Integer, ByRef prcA As Double, ByRef prcB As Double, _
    ByRef prcC As Double)
    Dim arrTempABC() As Double, i As Integer, j As Integer, intTemp As Integer, tempABC As Double
    Dim noA As Integer, noB As Integer, noC As Integer, arrTempTSno() As Integer

    ReDim arrTempABC(1 To tsTot)
    ReDim arrTempTSno(1 To tsTot)


    'populate the array that holds the values by which we classify the SKUs
    If swtcABCclass = 1 Then 'ABC based on volume*value
        For i = 1 To tsTot
            arrTempABC(i) = SKUinfo(i, 11) * SKUinfo(i, 5) 'average monthly volume*value (cost)
            arrTempTSno(i) = i 'just hold the position (ascending number of the timeseries/SKU)
        Next i
    ElseIf swtcABCclass = 2 Then 'ABC based on volume
        For i = 1 To tsTot
            arrTempABC(i) = SKUinfo(i, 11) ' average monthly volume
            arrTempTSno(i) = i
        Next i
    End If

    'find the limits of each class (in terms of percentages of SKUbase)
    noA = WorksheetFunction.RoundDown(tsTot * prcA, 0) 'first prcA% of the tsTot (number of timeseries or SKUs) are in class A
    noB = noA + WorksheetFunction.RoundDown(tsTot * prcB, 0)
    noC = tsTot

    'sort arrTempABC while saving the positions in a seperate array
    For i = 2 To tsTot
        tempABC = arrTempABC(i)
        intTemp = arrTempTSno(i)
        For j = i - 1 To 1 Step -1
            If arrTempABC(j) >= tempABC Then Exit For
            arrTempABC(j + 1) = arrTempABC(j)
            arrTempTSno(j + 1) = arrTempTSno(j)
        Next
        arrTempABC(j + 1) = tempABC
        arrTempTSno(j + 1) = intTemp
    Next

    'now that i have the sorted positions, i can just assign the categories
    For i = 1 To noA 'noa number of items should be classified as "A"
        SKUinfo(arrTempTSno(i), 12) = "A"
    Next i
    For i = noA + 1 To noB 'nob - (noa +1) number of items should be classified as "B"
        SKUinfo(arrTempTSno(i), 12) = "B"
    Next i
    For i = noB + 1 To noC 'noc - (nob +1) number of items should be classified as "C"
        SKUinfo(arrTempTSno(i), 12) = "C"
    Next i

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.