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
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,214,987
Messages
6,122,613
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top