Cycle through Variables

B

Bythsx-Addagio

Hello,
I am trying to think of a way that I could loop through a range of defined
variables. Perhaps I am approaching this the wrong way so any suggestions
would be greatly appreciated. Basically what I am trying to do is shown
below.

Thanks in advance!
'****
Dim SourceFile As String, SourcePath As String, sFile As String
Dim DestFile As String, DestPath As String, dFile As String
Dim r As Long

SourcePath = "I:\"
DestPath = "C:\"

fname1 = "RS_2626(Z)_1YR.pdf"
fname2 = "RS_2626(Z)_2YR.pdf"
fname3 = "RS_2626(Z)_3YR.pdf"
fname4 = "RS_2626(Z)_4YR.pdf"
fname5 = "RS_4501(Z)_1YR.pdf"
fname6 = "RS_4501(Z)_2YR.pdf"
fname7 = "RS_4501(Z)_3YR.pdf"

'****Loop file names to File Var

For r = 1 To 7 Step 1

sFile = fname & r

'*****Copy File **********
SourceFile = SourcePath & sFile
DestFile = DestPath & sFile

FileCopy SourceFile, DestFile
'****End Copy *********

Next r
 
S

Shasur

Hi

You can try using arrays

Dim SourceFile As String, SourcePath As String, sFile As String
Dim DestFile As String, DestPath As String, dFile As String
Dim r As Long
Dim fname(1 To 7) As String

SourcePath = "I:\"
DestPath = "C:\"

fname(1) = "RS_2626(Z)_1YR.pdf"
fname(2) = "RS_2626(Z)_2YR.pdf"
fname(3) = "RS_2626(Z)_3YR.pdf"
fname(4) = "RS_2626(Z)_4YR.pdf"
fname(5) = "RS_4501(Z)_1YR.pdf"
fname(6) = "RS_4501(Z)_2YR.pdf"
fname(7) = "RS_4501(Z)_3YR.pdf"

'****Loop file names to File Var

For r = 1 To 7 Step 1

sFile = fname(r)

'*****Copy File **********
SourceFile = SourcePath & sFile
DestFile = DestPath & sFile

FileCopy SourceFile, DestFile
'****End Copy *********

Next r


Cheers
Shasur
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top