Macro To Loop Through CSV Files in Folder

andybason

Board Regular
Joined
Jan 7, 2012
Messages
217
Office Version
  1. 2016
Hello

I have lots of CSV files in a folder than contain text and number in column A of sheet 1. Is it possible to loop through all the files and copy/paste all cells that contain either 0.11, 0.12 or 0.13 into the first empty cell in column A of the workbook the macro is coded in?

Thank you
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Something like this should get you started, but I'm not clear if you want to combine all the found numbers into a single cell in your master workbook, or paste them down a growing list.

The following assumes all your source files are in a seperate sub-folder, and it will ask you to select that folder location in a pop-up:

VBA Code:
 Dim WkbM As Workbook
    Set WkbM = ThisWorkbook
 
   Dim MyObj As Object
    Dim MySource As Object
    Dim file As Variant
    Dim wb As Workbook
 
   Set MyObj = New FileSystemObject
 
    Dim myFile As String
    Dim PickFolder As FileDialog

    'Retrieve Target Folder Path From User
    Set PickFolder = Application.FileDialog(msoFileDialogFolderPicker)

    With PickFolder
      .Title = "Select A Target Folder"
      .AllowMultiSelect = False
        If .Show <> -1 Then GoTo NextCode
        myPath = .SelectedItems(1)
    End With
 
   'In Case of Cancel
NextCode:
  myPath = myPath
  If myPath = "" Then GoTo ResetSettings
 
  Application.DisplayAlerts = True
  Application.ScreenUpdating = True
 
   Dim NUMB As String
     
   'START Loop through all files in selected folder regardless of file type (cvs or excel formats)
 
   Set MySource = MyObj.GetFolder(myPath)
 
   For Each file In MySource.Files
 
         Set wb = Workbooks.Open(Filename:=MySource & "\" & file.Name)
          
         Dim FN As String
         FN = file.Name   
        
         NUMB = Sheets(1).Cells(1, 1).Value  'picks up the number value in cell A1
 
        If NUMB = "0.11" Or NUMB = "0.12" Or NUMB = "0.13" Then    'I'm not certain if the source cell's formatting will through this off... if the number is actually 0.1101 but only is displaying 2 decimals for example?
                 
            WkbM.Sheets(1).Cells(1, 1).Value = NUMB   'I'm not clear here if you want to combine the numbers into a single cell, or paste them down a list
      
        Else
        End If
                              
         wb.Close SaveChanges:=False  'closes the file w/o saving
   Next file

ResetSettings:
  'Reset Macro Optimization Settings
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
 
Last edited:
Upvote 0
Hello Oddball

Thank you very much for your reply. I have run the macro but get the error in the image below. It is the highlighted part that seems to be the issue.

Untitled.png
 
Upvote 0
Hello Oddball

Thank you very much for your reply. I have run the macro but get the error in the image below. It is the highlighted part that seems to be the issue.

View attachment 38567
Opps sorry about that, always forget to mention this. Check for the following scripts (mark with check box) in the VBA editor box then look for Tools -> References in the top bar menus, click off everything I have in the picture below, then try again.
 

Attachments

  • Scripts2.JPG
    Scripts2.JPG
    38.4 KB · Views: 67
Upvote 0
Solution

Forum statistics

Threads
1,214,535
Messages
6,120,093
Members
448,944
Latest member
SarahSomethingExcel100

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