excel - 在指令中使用变量的内容

标签 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

现在我有几个案例,代码针对不同的值重复。

最佳答案

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

关于excel - 在指令中使用变量的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675116/

相关文章:

excel - 如何获取Excel单元格的 "Name Box"名称?

excel - 如何从数组中填充列?

excel - 禁用 Microsoft Excel 的自动日期格式设置

excel - 数据验证 - Excel 中没有重复项的下拉列表

vba - 如何使用 VBA 导出 CSV?

arrays - 在 VBA、Excel 中自动创建不同的、可识别的集合

Excel密码和雕刻标签名称

sharepoint-2010 - Excel vba根据条件删除Excel中的重复列

macos - 在 OSX Excel 中使用 VBA 获取 HTTP 请求

excel - 查找范围内的最后一行