我正在尝试查看是否可以使用 For 循环设置大量变量的值,而无需在变量名称末尾附加整数。已经对此进行了一些尝试,但尚未找到任何解决方案。我假设这是不可能的,除非我构建像 [Variable1, Variable2, Variable3 --- For Loop: Variable & I = ] 这样的变量名称。
**MODULE VARIABLES**
Private ADwsLoadType As String
Private ADwsShipDate As Date
Private ADwsCustomer As String
Private ADwsBOL As String
Private ADwsLocation As String
Private ADwsSOproduct As String
Private ADwsSOsize As String
Private ADwsSOweight As Long
Private ADwsSOadjust As Long
Private ADwsScaleWeight As Long
Private ADwsLoadWeight As Long
Private ADwsProduct1 As String
Private ADwsProduct2 As String
Private ADwsProduct3 As String
Private ADwsProduct4 As String
Private ADwsQty1 As Long
Private ADwsQty2 As Long
Private ADwsQty3 As Long
Private ADwsQty4 As Long
Private ADwsSize1 As String
Private ADwsSize2 As String
Private ADwsSize3 As String
Private ADwsSize4 As String
Private ADwsLoadEst1 As Long
Private ADwsLoadEst2 As Long
Private ADwsLoadEst3 As Long
Private ADwsLoadEst4 As Long
Private ADwsLoadAct1 As Double
Private ADwsLoadAct2 As Double
Private ADwsLoadAct3 As Double
Private ADwsLoadAct4 As Double
Private ADwsToteAct1 As Double
Private ADwsToteAct2 As Double
Private ADwsToteAct3 As Double
Private ADwsToteAct4 As Double
Private ADwsTLproduct As String
Private ADwsTLweight As String
Private ADwsTL90 As String
Private Sub SetVariables()
Call OperationCwal **Disable Calculation & Screen Updating**
Call ShowMeTheMoney **Set Values for Public Variables**
With ADws
'**************************************************************************************************
**Private Variables**
Dim NamesArr As Variant
NamesArr = Array(ADwsLoadType, ADwsShipDate, ADwsCustomer, ADwsBOL, ADwsLocation, _
ADwsSOproduct, ADwsSOsize, ADwsSOweight, ADwsSOadjust, _
ADwsScaleWeight, ADwsLoadWeight, ADwsProduct1, ADwsProduct2, _
ADwsProduct3, ADwsProduct4, ADwsQty1, ADwsQty2, ADwsQty3, ADwsQty4, _
ADwsSize1, ADwsSize2, ADwsSize3, ADwsSize4, ADwsLoadEst1, _
ADwsLoadEst2, ADwsLoadEst3, ADwsLoadEst4, ADwsLoadAct1, ADwsLoadAct2, _
ADwsLoadAct3, ADwsLoadAct4, ADwsToteAct1, ADwsToteAct2, _
ADwsToteAct3, ADwsToteAct4, ADwsTLproduct, ADwsTLweight, ADwsTL90)
**Values**
Dim ValueArr As Variant
ValuesArr = Array(.[B7], .[B11], .[B15], .[B19], .[B23], .[F5], _
.[F6], .[J5], .[F6], .[F8], .[J8], .[E13], _
.[E15], .[E17], .[E19], .[F13], .[F15], .[F17], .[F19], _
.[G13], .[G15], .[G17], .[G19], .[I13], .[I15], .[I17], _
.[I19], .[J13], .[J15], .[J17], .[J19], .[K13], _
.[K15], .[K17], .[K19], .[E23], .[G23], .[J23])
For I = LBound(NamesArr) To UBound(NamesArr)
If Values(I) = "" Then
NamesArr(I) = ""
Else
NamesArr(I) = ValuesArr(I)
End If
Next I
**IMMEDIATE WITH SHOULD RETURN "TANK"**
Debug.Print ADwsLoadType **Returned Value = ""**
Debug.Print NamesArr(1) **Returned Value = "Tank"**
Debug.Print ValuesArr(1) **Returned Value = "Tank"**
**VALUE IS ASSOCIATED WITH BOTH ARRAYS BUT NOT WITH VARIABLE NAME**
'**************************************************************************************************
End With
Call BlackSheepWall **Enable Calculation & Screen Updating**
End Sub```
也许一种方法是使用这样的字典
Option Explicit
Dim dict As Scripting.Dictionary
Sub testDict()
Set dict = New Scripting.Dictionary
' The following lines add key-value pairs to the dictionary.
' Each pair consists of a unique key (like "ADwsLoadType")
' and an associated value (like "Data ADwsLoadType").
' Note that #1/4/2024# is a date literal in VBA.
dict.Add "ADwsLoadType", "Data ADwsLoadType"
dict.Add "ADwsShipDate", #1/4/2024#
dict.Add "ADwsCustomer", "Data ADwsCustomer"
dict.Add "ADwsBOL", "Data ADwsBOL"
dict.Add "ADwsLocation", "Data ADwsLocation"
dict.Add "ADwsSOproduct", "Data ADwsSOproduct"
' and so on and so forth
' This block of code iterates through each key in the dictionary.
' For each key v, it prints the key and its associated value
' to the Immediate Window using Debug.Print.
Dim v As Variant
For Each v In dict
Debug.Print v & " - " & dict(v)
Next
' This section demonstrates how to change the value associated
' with a specific key in the dictionary.
' The line dict("ADwsLoadType") = "Data changed ADwsLoadType"
' updates the value for the key "ADwsLoadType".
dict("ADwsLoadType") = "Data changed ADwsLoadType"
Debug.Print dict.Item("ADwsLoadType")
End Sub
等主题