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
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
what version of excel are you using? application.filesearch doesnt work on office 2010
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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