2

I have declared constants like the below in the module,

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

I am trying to call this in a loop,

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

Loop

The above Dir code is wrong, but I want to call the constant based on the looping integer. Could someone help? Please let me know if the question is not clear.

Community
  • 1
  • 1
Ko Nayaki
  • 74
  • 10

1 Answers1

3

VBA does not provide any method for concatenating a string to be used as a dynamic variable name. You could create a string constant with a delimiter then split it before use.

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