是否有更快的方法来定义以下变量。 notAct是名称,在名称到达单元格位置之后。 “b”表示列,数字表示行。值为0.必须定义非常单个单元格,以便我可以使用变量。
Dim notAct_b1, notAct_c1, notAct_d1, ..., notAct_d20 As Integer
notAct_b1 = 0
notAct_c1 = 0
notAct_d1 = 0
notAct_b2 = 0
notAct_c2 = 0
notAct_d2 = 0
notAct_b3 = 0
notAct_c3 = 0
notAct_d3 = 0
notAct_b4 = 0
notAct_c4 = 0
notAct_d4 = 0
notAct_b5 = 0
notAct_c5 = 0
notAct_d5 = 0
notAct_b6 = 0
notAct_c6 = 0
notAct_d6 = 0
notAct_b7 = 0
notAct_c7 = 0
notAct_d7 = 0
notAct_b8 = 0
notAct_c8 = 0
notAct_d8 = 0
notAct_b9 = 0
notAct_c9 = 0
notAct_d9 = 0
notAct_b10 = 0
notAct_c10 = 0
notAct_d10 = 0
notAct_b11 = 0
notAct_c11 = 0
notAct_d11 = 0
notAct_b12 = 0
notAct_c12 = 0
notAct_d12 = 0
notAct_b13 = 0
notAct_c13 = 0
notAct_d13 = 0
notAct_b14 = 0
notAct_c14 = 0
notAct_d14 = 0
notAct_b15 = 0
notAct_c15 = 0
notAct_d15 = 0
notAct_b16 = 0
notAct_c16 = 0
notAct_d16 = 0
notAct_b17 = 0
notAct_c17 = 0
notAct_d17 = 0
notAct_b18 = 0
notAct_c18 = 0
notAct_d18 = 0
notAct_b19 = 0
notAct_c19 = 0
notAct_d19 = 0
notAct_b20 = 0
notAct_c20 = 0
notAct_d20 = 0
答案 0 :(得分:1)
首先,您不需要在VBA中初始化一个Integer变量 - 它在变暗后将为0。
但是,不是定义4x20变量,而是更好地使用数组:
Dim notAct(1 to 20,1 to 43) As Integer
如果您需要初始化或重置所有值,可以使用小循环:
Dim col As Integer, row As Long
'Initialize/reset
For col = 1 To 4
For row = 1 To 20
notAct(row, col) = 0 'setting 0 not required after Dim
Next row
Next col
如果需要将变量的值分配给单元格,请使用以下语法:
'Assigns a value from the array to B3
Cells(3, 2).Value = notAct(3, 2)
最重要的是,如果您想为所有4x20单元格分配其值,请使用以下一行代码:
Range("A1:D20").Value = notAct