Opening CSV Files from a Folder and all its Subfolders

ilak1008

New Member
Joined
Aug 2, 2010
Messages
38
I tried to search for threads about opening files from multiple subfolders but the following codes does not open files. Could someone please check and edit the code that I have below:

Dim lCount As Long
Dim wbResults As Workbook
Dim wbCodeBook As Workbook


Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

On Error Resume Next
Set wbCodeBook = ThisWorkbook
With Application.FileSearch
.NewSearch
.LookIn = "I:\MainFolder"
.FileType = msoFileTypeExcelWorkbooks
.Filename = "*Report.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)


If Range("A3").Text = "" Then
Range("A2:N2").Select
Selection.Copy
Else
Range("A2:N2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
End If
wbResults.Close SaveChanges:=False
Next lCount
End If
End With
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
 

Some videos you may like

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.

mcget

New Member
Joined
Feb 26, 2008
Messages
47
what version of excel are you using? application.filesearch doesnt work on office 2010
 

mcget

New Member
Joined
Feb 26, 2008
Messages
47
try

Sub opencsv()
Dim FolderPath As String
Dim FileName As String

FolderPath = "C:\desktop"

FileName = Dir(FolderPath & "*.csv*")

Do While FileName <> ""

Workbooks.Open (FolderPath & FileName)

FileName = Dir()

Loop
End Sub
 

ilak1008

New Member
Joined
Aug 2, 2010
Messages
38
try

Sub opencsv()
Dim FolderPath As String
Dim FileName As String

FolderPath = "C:\desktop"

FileName = Dir(FolderPath & "*.csv*")

Do While FileName <> ""

Workbooks.Open (FolderPath & FileName)

FileName = Dir()

Loop
End Sub

Hello mcget,

I tried your code below but nothing happened:
Sub opencsv()
Dim FolderPath As String
Dim FileName As String

FolderPath = "C:\Desktop"

FileName = Dir(FolderPath & "*.csv*")

Do While FileName <> ""

Workbooks.Open (FolderPath & "*.csv")
If Range("A3").Text = "" Then
Range("A2:N2").Select
Selection.Copy
Else
Range("A2:N2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
End If
Windows(2).Activate
ActiveWorkbook.Close False

FileName = Dir()

Loop
End Sub
 

ilak1008

New Member
Joined
Aug 2, 2010
Messages
38

ADVERTISEMENT

Hello mcget,

Thanks for replying. I tried your code below but nothing happened:

Sub opencsv()
Dim FolderPath As String
Dim FileName As String

FolderPath = "C:\Desktop"

FileName = Dir(FolderPath & "*.csv*")

Do While FileName <> ""

Workbooks.Open (FolderPath & "*.csv")
If Range("A3").Text = "" Then
Range("A2:N2").Select
Selection.Copy
Else
Range("A2:N2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
End If
Windows(2).Activate
ActiveWorkbook.Close False

FileName = Dir()

Loop
End Sub
 

ilak1008

New Member
Joined
Aug 2, 2010
Messages
38
Hello mcget,

Thanks for replying. I tried your code below but nothing happened:

Sub opencsv()
Dim FolderPath As String
Dim FileName As String

FolderPath = "C:\Desktop"

FileName = Dir(FolderPath & "*.csv*")

Do While FileName <> ""

Workbooks.Open (FolderPath & "*.csv")
If Range("A3").Text = "" Then
Range("A2:N2").Select
Selection.Copy
Else
Range("A2:N2").Select
Range(Selection, Selection.End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
End If
Windows(2).Activate
ActiveWorkbook.Close False

FileName = Dir()

Loop
End Sub


Anyone can help? Thanks.
 

ilak1008

New Member
Joined
Aug 2, 2010
Messages
38
I've been waiting for any response to my query. Any help would be much appreciated.

o_O
 

Watch MrExcel Video

Forum statistics

Threads
1,127,133
Messages
5,622,911
Members
415,938
Latest member
Walkie1994

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
Top