在运行时使用文字调用常量

时间:2018-03-26 01:14:38

标签: excel excel-vba constants vba

我在模块中声明了如下的常量,

Public Const strFolderA1 = "C:\ABCD\One"
Public Const strFolderA2 = "C:\ABCD\two"

我试图在循环中调用它,

For i = 1 To 3
strFile = Dir(strFolderA & i & "\" & filenm)

Loop

上面的Dir代码是错误的,但我想根据循环整数调用常量。 有人可以帮忙吗? 如果问题不明确,请告诉我。

1 个答案:

答案 0 :(得分:3)

VBA没有提供任何连接字符串以用作动态变量名的方法。您可以使用分隔符创建字符串常量,然后在使用前将其拆分。

Option Explicit
Public Const strFolderA As String = "C:\ABCD\One|C:\ABCD\Two|C:\ABCD\Three"

Sub abcs()
    Dim i As Long, fldrs As Variant

    fldrs = Split(strFolderA, "|")
    For i = LBound(fldrs) To UBound(fldrs)
        Debug.Print fldrs(i)
    Next i
End Sub