Dir(FolderPath) - multiple file formats

gomes123

New Member
Joined
Jun 16, 2021
Messages
19
Office Version
  1. 2007
Platform
  1. Windows
I've got an excel macro that will open through all files in a folder and run some code, The problem is the files in the folder are either .xlsx or .csv.
The part of the code I'm using is as below.
Code:
fileName = Dir(folderPath & "*.xlsx")

I have to change the code manually to
Code:
fileName = Dir(folderPath & "*.csv")
and run the macro twice.

Is there a way to get the macro to open both xlsx and csv files?

Thanks.

Below is the code I'm using
Code:
Sub SearchAndCompileData()
    Dim keyword As String
    Dim folderPath As String
    Dim fileName As String
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim file As String
    Dim arrData() As Variant
    Dim i As Long
    Dim lastRow As Long
    

    keyword = InputBox("Enter search keyword")
    If keyword = "" Then Exit Sub
    
 
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Select Folder"
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        folderPath = .SelectedItems(1) & "\"
    End With
    

    Set ws = ThisWorkbook.ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    i = lastRow + 1
    

    fileName = Dir(folderPath & "*.xlsx")
    Do While fileName <> ""
      
        Set wb = Workbooks.Open(folderPath & fileName)
        
     
        Set ws = wb.Worksheets(1)
 
        For Each cell In ws.Range("B1:B" & ws.Cells(ws.Rows.Count, "B").End(xlUp).Row)
            If cell.Value = keyword Then
          
                ws.Range("C" & cell.Row).Copy Destination:=ThisWorkbook.ActiveSheet.Range("C" & i)
                ws.Range("D" & cell.Row).Copy Destination:=ThisWorkbook.ActiveSheet.Range("D" & i)
                i = i + 1
            End If
        Next cell
        
     
        wb.Close SaveChanges:=False
        
      
        fileName = Dir
    Loop
    
    
    MsgBox "Search and compilation completed."
End Sub
 
Last edited:

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
This could be an option:


VBA Code:
  Filename = Dir(folderPath & "*.*")
  
  Do While Filename <> ""
    If Filename Like "*.xlsx" Or Filename Like "*.csv" Then
      'do the work
    
    End If
    
    Filename = Dir()
  Loop
 
Upvote 1
Solution

Forum statistics

Threads
1,215,072
Messages
6,122,968
Members
449,095
Latest member
Mr Hughes

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