Mscro not Displaying CVS Files in Directory

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,563
Office Version
  1. 2021
Platform
  1. Windows
I have a macro to browse to a specific directory to select CSV Files, but I cannot see these using this code

Code:
 Sub Open_Workbook()

    

    Dim folderDialog As FileDialog
    Dim targetDirectory As String
    Dim fileName As String

    ' Use FileDialog to select the target directory (folder)
    Set folderDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With folderDialog
        .Title = "Select the Target Directory"
        .AllowMultiSelect = False
        .InitialFileName = "\\mtqw\sales\BM DW\"
        If .Show <> -1 Then
            MsgBox "No directory selected.", vbExclamation
            Exit Sub ' User cancelled the selection
        End If
        targetDirectory = .SelectedItems(1)
    End With

    If targetDirectory = "" Then Exit Sub ' User didn't select any directory

    ' Check if the selected directory ends with the path separator and add it if needed
    If Right(targetDirectory, 1) <> Application.PathSeparator Then
        targetDirectory = targetDirectory & Application.PathSeparator
    End If

    ' Process CSV files in the selected target directory
    fileName = Dir(targetDirectory & "*.csv")

    If fileName = "" Then
        MsgBox "No CSV files found in the selected directory.", vbInformation
        Exit Sub
    End If

    Application.ScreenUpdating = False
    Do While fileName <> ""
        With Workbooks.Open(targetDirectory & fileName, Local:=True)
            With Sheets(1)
                .Range("A1:S" & .Range("A" & Rows.Count).End(xlUp).Row).Copy _
                    Destination:=ThisWorkbook.Sheets("Data Import").Range("A" & Rows.Count).End(xlUp).Offset(1)
            End With
            .Close SaveChanges:=False
        End With
        fileName = Dir ' Get next CSV file in the folder
    Loop
    Application.ScreenUpdating = True

 
End Sub

It would be appreciated if someone could amend my code
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I think you need to add a backslash to your target directory, i.e.
Rich (BB code):
   fileName = Dir(targetDirectory & "\*.csv")
 
Upvote 0
Solution

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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