簡體   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