LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
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










 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pulling pdf files from general folder to specific folder [email protected] Excel Discussion (Misc queries) 2 September 8th 09 09:41 PM
Name of files in a folder Irfan Khan[_2_] Excel Discussion (Misc queries) 2 August 26th 08 08:32 AM
Check if a folder has x files in it. Dave Excel Discussion (Misc queries) 8 November 15th 07 03:35 PM
Copying all files in a folder to new folder michaelberrier Excel Discussion (Misc queries) 2 June 20th 06 05:35 AM
how can I specific a folder with wildcard criteria and excel will import all the correct files in that folder? Raven Excel Discussion (Misc queries) 1 January 24th 06 03:28 PM


All times are GMT +1. The time now is 03:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"