Variables 定义许多不同的变量

Variables 定义许多不同的变量,variables,excel,excel-2013,vba,Variables,Excel,Excel 2013,Vba,有没有更快的方法来定义以下变量。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 no

有没有更快的方法来定义以下变量。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

首先,您不需要在VBA中初始化整型变量-它变暗后将为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

与代码相关的一件重要事情是:当Dim变量类似时:
Dim notAct_b1、notAct_c1、notAct_d1为整数
只有最后一个变量为整数类型。你应该正确地写:
Dim notAct\u b1为整数,notAct\u c1为整数
等等。谢谢你的代码。你能解释一下B2=3,2而不是B2=2,2的可能性吗。如果是,C2和D2是什么?对不起,我的(双重)坏了。范围(3,2)是B3-第三行,第二列!-)