View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Loping through files in a folder

just get a list of the history files and put them in an array, then loop
through the array or use Match for the comparison

The below is untested, so it may contain syntax errors/typos but represents
a general approach

Sub CopyData()
Dim His() As String
Dim Nw() As String
Dim sPathHis As String, sPathNw As String
Dim i As Long, res As Variant
Dim rng As Range, rng1 As Range
Dim sNew As String, sHis As String
Dim wkbk As Workbook
sPathHis = "C:\History\"
sPathNw = "C:\New\"

ReDim His(1 To 1000)
ReDim Nw(1 To 1000)

' get a list from history

sHis = Dir(sPathHis & "*.xls")
i = 0
Do While sHis < ""
i = i + 1
His(i) = sHis
sHis = Dir
Loop
ReDim His(1 To i)

' get a list from new

sNew = Dir(sPathNw & "*.xls")
i = 0
Do While sNew < ""
i = i + 1
Nw(i) = sNew
sNew = Dir
Loop
ReDim Nw(1 To i)

' now process all the files in history

For i = 1 To UBound(Nw)
res = Application.Match(Nw(i), His, 0)
If Not IsError(res) Then
' code to copy data
Set wkbk = Workbooks.Open(sPathNw & Nw(i))
Set rng = wkbk.Worksheets(1).Range("A1").CurrentRegion
rng.Copy
Application.DisplayAlerts = False
wkbk.Close SaveChanges:=False
Application.DisplayAlerts = True
Set wkbk = Workbooks.Open(sPathHis & Nw(i))
Set rng1 = wkbk.Worksheets(1).Cells(Rows.Count, 1).End(xlUp)(2)
rng1.Paste
wkbk.Close SaveChanges:=True
Else
FileCopy sPathNw & Nw(i), sPathHis & Nw(i)
End If
' Kill sPathNw & nw(i)
Next

End Sub


--
Regards,
Tom Ogilvy


Otto Moehrbach wrote in message
...
Tom
My code is the same as yours but mine didn't work last night. Now it
does, as does yours. Senility is a weird thing.
Now I have another question. I have folders New and History. I am
looping through the files in the New folder. For each file I want to ask
the question: "Is this file (same name) in the History folder?" The only
thing I know to do is to loop through all the files in the History folder
and compare file names. Is there a better way? Maybe Find? Thanks for
your help. It is very much appreciated. Otto
"Tom Ogilvy" wrote in message
...
Sub Tester9()
PathOnlysource = "C:\Data2"

ChDir PathOnlysource
TheFile = Dir(PathOnlysource & "\*.xls")
Do While TheFile < ""
Debug.Print TheFile
TheFile = Dir
Loop

End Sub

works fine for me. You want to look at the file, then do the TheFile =

Dir

--
Regards,
Tom Ogilvy


Otto Moehrbach wrote in message
...
Excel 2003
From my notes I have: (PathOnlySource is the full path to the folder

holding
the files)
ChDir PathOnlySource
TheFile = Dir(PathOnlySource & "\*.xls")
Do While TheFile < ""
TheFile = Dir
MsgBox TheFile
Loop

I thought that the line:
TheFile = Dir(PathOnlySource & "\*.xls")
would get the first .xls file name in the folder, which it appears to

be
doing. And I thought that the line:
TheFile = Dir
Would get subsequent file names which it appears it doesn't do. Where

am
I
going wrong? Thanks for your help. Otto