Since it has too many loop, loop inside loop, this works very slowly. How can we increase the speed of this program? I am trying to copy a range from one file to another file of same name in different folder. I have folders named "A","B","C"..."G"
inside c:\Charts\1\
and c:\Charts\0\
. Inside each folder "A","B","C"..."G"
there are files named 1,2,3,4,..10
.
dim vArr1 as array
Dim vArr as array
vArr1 = Array("A", "B", "C", "D", "E", "F", "G")
vArr = Array("A", "B", "C", "D", "E", "F", "G")
Dim fileName1, Pathname1 As String
Pathname1 = "c:\Charts\1\"
Pathname="c:\charts\0\"
For Each vFile1 In vArr1
fileName1 = Dir(Pathname1 & vFile1 & "\" & "*.xlsx")
Do While fileName1 <> ""
For Each vFile In vArr
filename = Dir(Pathname & vFile & "\" & "*.xlsx")
Do While filename <> ""
If filename = fileName1 Then
Set WB1 = Workbooks.Open(Pathname1 & vFile & "\" & fileName1)
WB1.Application.ScreenUpdating = False
WB1.ActiveSheet.Range("M1:M30").Copy
WB1.Close (False)
Set WBD1 = Workbooks.Open(Pathname & vFile & "\" & filename)
WBD1.ActiveSheet.Range("C1").Select
WBD1.ActiveSheet.Paste
WBD1.ActiveSheet.Cells(1, 3).Value = "HSI Q4 2014-15"
WBD1.Close (True)
filename = Dir()
Else
End If
fileName1 = filename
Loop
Next
Loop
Next
Sub ... End Sub
part? Also, arevFile
variables declared at module-scope or simply undeclared? I strongly recommend stickingOption Explicit
at the top of your module. \$\endgroup\$Dir()
should only give you the first set of files in the inner-most loop. The second time you call it, it blows away the first result. \$\endgroup\$