Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops VBA Access使用循环声明变量名称_Loops_Ms Access_Variables_Vba - Fatal编程技术网

Loops VBA Access使用循环声明变量名称

Loops VBA Access使用循环声明变量名称,loops,ms-access,variables,vba,Loops,Ms Access,Variables,Vba,如何在循环中声明变量名?例如,我需要声明Combo1Count、Combo2Count、Combo3Count等: Dim i As Integer For i = 1 To 6 Dim Combo & i & Count as Integer Next i 随后我将需要参考这些变量,例如: For i = 1 To 6 If Combo & i & Count > 0 .... Next i 循环中定义的变量受每种语言的“范围”规则的约

如何在循环中声明变量名?例如,我需要声明Combo1Count、Combo2Count、Combo3Count等:

Dim i As Integer
For i = 1 To 6
    Dim Combo & i & Count as Integer
Next i
随后我将需要参考这些变量,例如:

For i = 1 To 6
    If Combo & i & Count > 0 ....
Next i

循环中定义的变量受每种语言的“范围”规则的约束。对于VBA,您可以在此处阅读更多内容:


因此,在循环或条件语句(etc)中定义的任何变量都不能在该范围之外访问。如果希望一个变量在循环中接收值,则应将其调暗到循环声明上方。然后可以根据需要在以后的代码中访问它(甚至在循环中)

范围在VBA中不是问题。问题是。。。你不能

解决方法是使用选择案例:

Dim i As Integer
For i = 1 To 6
    Select Case 1
        Dim Combo1 As Integer
    Select Case 2
        Dim Combo2 As Integer
    ' ....
    Select Case 6
        Dim Combo6 As Integer
    End Select
Next i
当然,这毫无意义。因此,请使用:

Dim Combo1 As Integer
Dim Combo2 As Integer
' ....
Dim Combo6 As Integer

是否有不使用数组的原因?当您知道需要多少位置/索引时,您可以将其变暗为变量,然后重新进行变暗。该链接指向VB.NET文档,而不是VBA。