FOUNDFILES.COUNT RETAINS OLD VALUE

tyronemeister

New Member
Joined
Jun 30, 2006
Messages
28
The following code imports all files from a directory and appends them to a single workbook. It works without problems once, but gives an error after that if the number of files changes in the directory. It retains the count value for the initial directory count.

Does this value need to be cleared at some point or does foundfiles.count need to be initialized at zero?

Thanks.

Code:
Sub ImportDirectoryFiles()
Dim ImportWB As Workbook
Dim MasterWB As Workbook
Dim i As Integer

Cells.Clear
Application.ScreenUpdating = False
Set MasterWB = ActiveWorkbook

With fs
    .NewSearch
    .LookIn = "C:\test2\"
    .Filename = "*.CSV"
     .SearchSubFolders = False
    .Execute

If .Execute() > 0 Then
MsgBox "There were " & .FoundFiles.Count & "file(s) found."
For i = 1 To .FoundFiles.Count

Set ImportWB = Workbooks.Open(.FoundFiles(i))
ImportWB.Sheets(1).UsedRange.Copy MasterWB.Sheets(1).Range("A65536").End(xlUp).Offset(1)
ImportWB.Close False

Next i
Else
MsgBox "There were no files found."
End If
End With
Application.ScreenUpdating = True
End Sub
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Watch MrExcel Video

Forum statistics

Threads
1,109,380
Messages
5,528,366
Members
409,817
Latest member
JiNXX9500

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top