I have a macro to browse to a specific directory to select CSV Files, but I cannot see these using this code
It would be appreciated if someone could amend my 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