Consulting

Results 1 to 6 of 6

Thread: Search for files with ending ".csv" in a folder and work on it

  1. #1

    Search for files with ending ".csv" in a folder and work on it

    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)
    ......

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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

  4. #4
    Hi snb,

    how can I include the WorkonCSV process in your code?

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    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

  6. #6
    Thank you snb!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •