在指令中使用变量的内容

时间:2019-02-13 16:31:58

标签: excel vba

我解释一个Y问题: 这就是调用,diMatSE(i)可以具有不同的值,例如本示例中的PRS02PRS03

Call findCaMaterialsAndSumWeights(caMaterials, caMaterialsW, caMat, caMatW, diMatSE(i), diMatNotSE(i), i, posCaMaterialsTaken)

这里是数组的定义:

PRS02 = Array("201010", "207201", "213004", "210110")
PRS03 = Array("201010", "207201", "213004")

这是摘要子:

Private Sub findCaMaterialsAndSumWeights(caMaterials As Variant, caMaterialsW As Variant, caMat As Variant, caMatW As Variant, diMatSE As Variant, diMatNotSE As Variant, y As Variant, posCaMaterialsTaken As Variant)
    Select Case diMatSE
        Case "PRS-02"
             For i = LBound(PRS02) To UBound(PRS02)
                Call posInTheArrayIgnoringPos(caMaterials, PRS02(i), posInArray, posCaMaterialsTaken)

                If posInArray <> 0 Then 'If found one CA material that is a component from a Diko SE
                    numFound = numFound + 1
                    posCaMaterialsTaken(posInArray) = "x"
                    If caMatW(y) = "" Then
                        caMatW(y) = 0
                    End If
                    caMatW(y) = caMatW(y) + caMaterialsW(posInArray)
                    If numFound = UBound(PRS02) + 1 Then 'If all Diko SE materials are found in Diko materials
                        caMat(y) = "PRS-02"
                        For x = LBound(posCaMaterialsTaken) To UBound(posCaMaterialsTaken)
                            If posCaMaterialsTaken(x) = "x" Then 'Saving CA materials positions that compound a Diko SE
                                posCaMaterialsTaken(x) = 1
                                numFound = numFound - 1
                                If numFound = 0 Then
                                    Exit For
                                End If
                            End If
                        Next x
                    End If
                   ...
                Else 'Not found one SE material
                End If
            Next i
         Case "PRS-03"  
            (same code as PRS-02 case but PRS03 instead PRS02)
        Case "PRS-04"
            (same code as PRS-02 case but PRS04 instead PRS02)
        ...
        Case else

现在我有几种情况,对于不同的值重复执行代码。

1 个答案:

答案 0 :(得分:0)

您的Y问题解决方案:

您可以使用字典来收集所有PRS-XX阵列。

Option Explicit

Dim PRS As Object

Sub Test()
    Set PRS = CreateObject("Scripting.Dictionary")
    'fill dictionary
    PRS.Add "PRS-02", Array("201010", "207201", "213004", "210110")
    PRS.Add "PRS-03", Array("201010", "207201", "213004")

    'call it
    findCaMaterialsAndSumWeights caMaterials, caMaterialsW, caMat, caMatW, diMatSE(i), diMatNotSE(i), i, posCaMaterialsTaken
End Sub

然后可以像PRS("PRS-02")一样使用它来获取数组Array("201010", "207201", "213004", "210110")。甚至是PRS("PRS-02")(1),例如直接访问数组的项1。如果现在像diMatSE = "PRS-02"一样使用变量PRS(diMatSE),它将根据您的变量值获取正确的数组。

因此,您只有一次代码,并且可以根据需要向字典中添加任意数量的PRS-xx,而无需再次触摸此过程。

Private Sub findCaMaterialsAndSumWeights(caMaterials As Variant, caMaterialsW As Variant, caMat As Variant, caMatW As Variant, diMatSE As Variant, diMatNotSE As Variant, y As Variant, posCaMaterialsTaken As Variant)
     For i = LBound(PRS(diMatSE)) To UBound(PRS(diMatSE))
        Call posInTheArrayIgnoringPos(caMaterials, PRS(diMatSE)(i), posInArray, posCaMaterialsTaken)

        If posInArray <> 0 Then 'If found one CA material that is a component from a Diko SE
            numFound = numFound + 1
            posCaMaterialsTaken(posInArray) = "x"
            If caMatW(y) = "" Then
                caMatW(y) = 0
            End If
            caMatW(y) = caMatW(y) + caMaterialsW(posInArray)
            If numFound = UBound(PRS(diMatSE)) + 1 Then 'If all Diko SE materials are found in Diko materials
                caMat(y) = diMatSE
                For x = LBound(posCaMaterialsTaken) To UBound(posCaMaterialsTaken)
                    If posCaMaterialsTaken(x) = "x" Then 'Saving CA materials positions that compound a Diko SE
                        posCaMaterialsTaken(x) = 1
                        numFound = numFound - 1
                        If numFound = 0 Then
                            Exit For
                        End If
                    End If
                Next x
            End If
           '...
        Else 'Not found one SE material
        End If
    Next i
End Sub