Hi,
I've been racking my brains on this but can't seem to get it working. I have this code below that works file looping through all the files in one folder. What I'm trying to do is make loop through all subfolders also.
Any help is highly appreciated.
AMAS
I've been racking my brains on this but can't seem to get it working. I have this code below that works file looping through all the files in one folder. What I'm trying to do is make loop through all subfolders also.
Any help is highly appreciated.
Code:
Sub Convert_CSV()
Dim screenUpdateState As Variant
Dim statusBarState As Variant
Dim eventsState As Variant
Dim fso As Object
Dim fPath As String
Dim myFolder, myFile
Dim wb As Workbook
Dim SavePath As String
' Turn off some Excel functionality so your code runs faster
screenUpdateState = Application.ScreenUpdating
statusBarState = Application.DisplayStatusBar
eventsState = Application.EnableEvents
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.DisplayAlerts = False
' Use File System Object to choose folder with files
Set fso = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
.InitialFileName = "C:\Users\HP-Server\Desktop\" ' Default path
.Title = "Please Select a Folder"
.ButtonName = "Select Folder"
If .SelectedItems.Count > 0 Then fPath = .SelectedItems(1) & "\"
If .SelectedItems.Count = 0 Then
MsgBox "No folder was chosen." & vbLf & vbLf & "Please try again.", vbExclamation, "User Cancelled."
Exit Sub
End If
End With
' Open each file consequently
Set myFolder = fso.GetFolder(fPath).Files
For Each myFile In myFolder
If LCase(myFile) Like "*.csv" Then
' Perform tasks with each file
' More code here
' Save file in original folder, but as csv file format
SavePath = fso.GetFolder(fPath).Name & "\" & fso.GetBaseName(myFile) & ".csv"
wb.SaveAs fileName:=SavePath, FileFormat:=xlCSV, CreateBackup:=False
' Close file
wb.Close True
End If
' Loop through all files in folder
Next myFile
'clean up
myFile = vbNullString
I = 1
' Turn Excel functionality back on
Application.ScreenUpdating = screenUpdateState
Application.DisplayStatusBar = statusBarState
Application.EnableEvents = eventsState
End Sub
AMAS