
我試圖找出錯誤所在,已經花了兩個小時來谷歌它:(
我建立了陣列 (ArrayTestPrime),其中包含以下元素:變數、陣列和 4 個數字。我在所有“Subs”之上聲明了它,因此它必須在整個模組中看到。
我只想在第一個過程中更改數組(ArrayTestPrime)中元素(0)的值,然後列印它。然後呼叫Procedure_2列印它,然後在sheets(1)處的row1中循環並再次更改它。
偵錯. print 向我展示了這個:
First_Procedure =
At the beginning of Second_Procedure =
Variable must be equal = 17
At the end of Second_Procedure =
但真正的價值觀必須是:
First_Procedure = 100
At the beginning of Second_Procedure = 100
Variable must be equal = 17
At the end of Second_Procedure = 17
我的程式碼震撼了我的大腦:
Public Article As String
Public ArticleCol As Variant
Public ArrayTestPrime As Variant
Public ArrayInArray() As Variant
Sub First_Procedure()
Article = "ARTICLE"
ArrayTestPrime = Array(ArticleCol, ArrayInArray(), 1, 2, 2, 1)
ArticleCol = 100
Debug.Print "First_Procedure = " & ArrayTestPrime(0)
Call Second_Procedure
End Sub
Sub Second_Procedure()
Dim Sub_J As Integer
Debug.Print "At the beginning of Second_Procedure = " & ArrayTestPrime(0)
For Sub_J = 1 To 27
If Cells(1, Sub_J) = Article Then ArticleCol = Sub_J
Next Sub_J
Debug.Print "Variable must be equal = " & ArticleCol
Debug.Print "At the end of Second_Procedure = " & ArrayTestPrime(0)
End Sub
請幫我!我將不勝感激任何形式的幫助!
答案1
您的變數分配有兩個問題...
1)你的訂單在這段程式碼中翻轉:
Sub First_Procedure()
Article = "ARTICLE"
→ ArrayTestPrime = Array(ArticleCol, ArrayInArray(), 1, 2, 2, 1) 'ArticleCol = ""
→ ArticleCol = 100
Debug.Print "First_Procedure = " & ArrayTestPrime(0) 'ArticleCol = ""
它應該是:
Sub First_Procedure()
Article = "ARTICLE"
→ ArticleCol = 100
→ ArrayTestPrime = Array(ArticleCol, ArrayInArray(), 1, 2, 2, 1) 'ArticleCol = 100
Debug.Print "First_Procedure = " & ArrayTestPrime(0) 'ArticleCol = 100
2) 您忘記設定 ArrayTestPrime(0) 的值:
For Sub_J = 1 To 27
If Cells(1, Sub_J) = Article Then ArticleCol = Sub_J
Next Sub_J
→ ArrayTestPrime(0) = ArticleCol 'We must set our new value for ArrayTestPrime(0)
Debug.Print "Variable must be equal = " & ArticleCol
Debug.Print "At the end of Second_Procedure = " & ArrayTestPrime(0)
End Sub