繁体   English   中英

如何使用动态变量?

[英]How to use Dynamic Variables?

好吧,我做错了什么? 在网上找到了这个,尝试了一下,类型不匹配。 只是通过让变量定义自己来使代码更简洁,例如Y(10),Y(11),Y(12),Y(13)或Y10 ... Y13,所以我有4子结尾的变量。 Y10-13不是单元格位置,但它们是相关的。 往下看!

实际数据集

变量a指向实际行,因此说Y10 = 400是行5上的唯一条目,行7具有Y10 = 7和Y12 =3。我没有在代码中定义它,因为此部分以function(a)和在函数本身之前定义a,以便子检查最后一行的a = 1。

Dim Y(10 To 13) As Integer 'these are just variables
Dim a as long 'this is an arbitrary row number defined before the function

For I = 10 to 13 'These are column numbers in the actual data range

If .Worksheets("15SK").Cells(a, I) <> "" Then
    Y(I) = .Worksheets("15SK").Cells(a, I).value 'Y(I) basically takes the value of the cell (a,I)
End If

Next

我基本上需要一个动态变量Y(I)或YI(实际上是任何形式的),因为我的代码后面的部分需要它。 我将分享另一部分来陈述我的观点,但是在下一部分中我很难翻译我正在做的事情:

For I = 10 To .Worksheets("15SK").Cells(2, Columns.Count).End(xlToLeft).Column 'All models

    If .Worksheets("15SK").Cells(a, I) <> "" Then 'If product model order > 0
    'removed a section of code here

        j = j + 3 'step 3
If ItemType = "Adaptor" And Y > X1 And DUP <> 1 Then

            Y1 = Y1 + Y(I)

                If ItemType = "Adaptor" And Y1 > X1 Then 'adding another duplicate

                    DOwb.Worksheets(1).Cells(j, 3) = .Worksheets("15SK").Cells(2, I) 'First DO product model row
                    DOwb.Worksheets(1).Cells(j - 1, 1) = Y1 - X1 'Excess Qty to be charged
                    DOwb.Worksheets(1).Cells(j - 3 - 1, 1) = Y(I) - (Y1 - X1) 'Making the previous row's Qty FOC by no. of CD - no. of previous
                    DOwb.Worksheets(1).Cells(j - 1, 2) = "pcs"
                    DOwb.Worksheets(1).Cells(j - 1, 3) = Application.WorksheetFunction.VLookup(DOwb.Worksheets(1).Cells(j, 3), .Worksheets("Catalogue").Range("catalogue"), 2, False) 'Model Description
                    JFOC = j
                    j = j + 3 'step 3
                    DUP = 1 'stops the duplication

                ElseIf Y1 = X1 Then
                    JFOC = j
                End If
            Y1 = 0
        End If

抱歉,对于VBA真的很陌生,或者对此事进行编码。 到目前为止,很棒的帮助!

Y10不能是变量的名称(因为它可能与单元格Y10混淆)。 尝试使用此类变量名的代码将不起作用。 尝试使用其他名称,例如y_10就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM