PDA

View Full Version : [SOLVED] Search for files with ending ".csv" in a folder and work on it



Cinema
10-16-2016, 06:38 AM
Hi,

I wrote a code that searches in a folder for files with the ending .csv and then makes some copy paste procedure.
I have some problems with my code.
1.) How to search just for cvs files that begin with "test"
2.) My code does not work. Can anybody see the mistake?


Sub Import()
Dim MyObj As Object
Dim MySource As Object
Dim file As Variant
Dim fpath As String

fpath = Sheets("Sheet1").Range("B1").Value
Set MySource = MyObj.GetFolder(fpath)






For Each file In MySource.Files
If InStr(file.Name, ".csv") Then WorkOnCSV MySource, file
Next file
End Sub

Private Sub WorkOnCSV(MySource As Object, file As Variant)
Dim tool As Workbook
Dim data As Workbook






Set tool = ThisWorkbook
Set data = Workbooks.Open(MySource & file, ReadOnly:=True)
......

Kenneth Hobs
10-16-2016, 07:40 AM
Not sure how that works as you left out the FSO object but if it does, just add another condition.

If InStr(file.Name, ".csv") and Left(file.Name, 4)= "test" Then WorkOnCSV MySource, file
FSO as you used, or Shell32, or Dir(), or DIR, methods can be used to iterate file names. The Shell32 and other that use arrays with Filter's can be handy.

snb
10-16-2016, 09:55 AM
Sub M_snb()
c00="G:\OF\"
c01=dir(c00 & "test*.csv")

do until c01=""
with getobject(c00 & c01)

.close 0
end with
c01=dir
loop
End Sub

Cinema
10-17-2016, 02:01 AM
Hi snb,

how can I include the WorkonCSV process in your code?

snb
10-17-2016, 02:48 AM
Sub M_snb()
c00="G:\OF\"
c01=dir(c00 & "test*.csv")

Do Until c01=""
With getobject(c00 & c01)
msgbox .sheets(1).name & vblf & .sheets(1).cells(1).value
.close 0
End With
c01=dir
Loop
End Sub

Cinema
10-17-2016, 04:42 AM
Thank you snb!