Loop through all .csv files in a folder (excel 2011)

didj

Board Regular
Joined
Feb 7, 2008
Messages
50
Hi everyone,

I want to open all csv files in a folder and just copy and paste some columns into another file. I am having a bit of trouble with the following code which can be found at ozgrid and I have modified slightly so it looks in the current directory instead of a fixed path...

Code:
Sub RunCodeOnAllFiles()
Dim lCount As Long
Dim MyDir as string
Dim wbResults As Workbook
Dim wbCodeBook As Workbook

    MyDir = CurDir()

On Error Resume Next
   Set wbCodeBook = ThisWorkbook
       With Application.FileSearch
           .NewSearch
           .LookIn = MyDir           
           .FileType = msoFileTypeExcelWorkbooks
           .Filename = "*.csv"
               
If .Execute > 0 Then 'Workbooks in folder
                   For lCount = 1 To .FoundFiles.Count 'Loop through all
 
'Open Workbook x and Set a Workbook variable to it
                       Set wbResults = Workbooks.Open(Filename:=.FoundFiles(lCount), UpdateLinks:=0)
                       
                       'MY COPY AND PASTE CODE GOES HERE

                       wbResults.Close SaveChanges:=False
                   Next lCount
               End If
       End With
On Error GoTo 0
End Sub
For starters, if I don't use on error resume next I get a runtime error 445: object doesn't support this action. So when I use the on error resume next, then by the time it gets to the foundfiles line, there are no variables set, so on the next line (workbooks.open) nothing happens.

Is there an easier (more up to date?) method to open all csv files (or excel files) in a folder?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
also would it be simpler to use.....

Code:
for each vaFileName in .foundfiles
workbooks.open vaFileName
or do I need to use

workbooks.opentext Filename:=vaFileName, Datatype:=xldelimited

etc etc
 
Upvote 0
Thanks Alvin. It doesn't explain the runtime error though that I am getting at application.filesearch.

Anyone got any suggestions how to fix or improve this code?
 
Upvote 0
Ok, 2007 and above can try:

Your files are under fileArray

Code:
Sub GetFileLists()
 
    Dim FileArray() As Variant
    Dim FileCount As Integer
    Dim FileName, FileDir, FileSearch As String
 
 
 
    FileDir = "C:\Documents and Settings\Alvin Wong\Desktop\New Folder\"
    FileSearch = "*.xls"
 
    FileCount = 0
    FileName = Dir(FileDir & FileSearch)
    Do While FileName <> ""
        FileCount = FileCount + 1
        ReDim Preserve FileArray(1 To FileCount)
        FileArray(FileCount) = FileName
        FileName = Dir()
    Loop
End Sub
 
Upvote 0
Thanks Alvin! I found out that 2007 and above doesn't recognise application.filesearch but I also found another solution here which I have tested and is working nicely.

Code:
MyDir = CurDir()
 
sDir = Dir$(MyDir & "\" & "*.csv", vbNormal)
 
Do Until LenB(sDir) = 0
 
Set oWB = Workbooks.Open(sDir)
 
‘ Do some stuff here
 
oWB.close
 
sDir = Dir$
 
Loop
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,387
Messages
6,124,633
Members
449,177
Latest member
Sousanna Aristiadou

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