I have a collection of CSV files in a set of subfolders and a main (but empty) file that contains formatting (column widths, colors and cell merges). I would like to copy the formatting from the formatting template xlsx file to each csv file then save the csv file as an xlsx file. I've got the recursive finding of files OK but it is the copying from the template to the csv file that I can't get working. I get a type mismatch error when I try to copy from the formatting workbook.
Sub FindPatternMatchedFiles()
Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.Pattern = ".*csv"
objRegExp.IgnoreCase = True
Dim colFiles As Collection
Set colFiles = New Collection
RecursiveFileSearch "C:\Path to top folder", objRegExp, colFiles, objFSO
For Each f In colFiles
Debug.Print (f)
'Insert code here to do something with the matched files
CSV_to_XLS (f)
Next
'Garbage Collection
Set objFSO = Nothing
Set objRegExp = Nothing
End Sub
Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
ByRef matchedFiles As Collection, ByRef objFSO As Object)
Dim objFolder As Object
Dim objFile As Object
Dim objSubFolders As Object
'Get the folder object associated with the target directory
Set objFolder = objFSO.GetFolder(targetFolder)
'Loop through the files current folder
For Each objFile In objFolder.Files
If objRegExp.Test(objFile) Then
matchedFiles.Add (objFile)
End If
Next
'Loop through the each of the sub folders recursively
Set objSubFolders = objFolder.Subfolders
For Each objSubfolder In objSubFolders
RecursiveFileSearch objSubfolder, objRegExp, matchedFiles, objFSO
Next
'Garbage Collection
Set objFolder = Nothing
Set objFile = Nothing
Set objSubFolders = Nothing
End Sub
Sub CSV_to_XLS(strFile)
Dim wb As Workbook
Dim format_wb As Workbook
Application.DisplayAlerts = False
xlsxFormatFile = "data_formatting.xlsx"
formatSheet = "format template"
formatRange = "A1:LL8203"
Set wb = Workbooks.Open(Filename:=strFile, Local:=True)
Set format_wb = Workbooks.Open(Filename:=xlsxFormatFile, Local:=True)
Workbooks(format_wb).Sheets(formatSheet).Range(formatRange).Copy
Workbooks(wb).Sheets(1).Range(formatRange).PasteSpecial (xlPasteAll)
wb.SaveAs Replace(wb.FullName, ".csv", ".xlsx"), FileFormat:=51
wb.Close True
Application.DisplayAlerts = True
Set wb = Nothing
End Sub
Sub FindPatternMatchedFiles()
Dim objFSO As Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objRegExp As Object
Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.Pattern = ".*csv"
objRegExp.IgnoreCase = True
Dim colFiles As Collection
Set colFiles = New Collection
RecursiveFileSearch "C:\Path to top folder", objRegExp, colFiles, objFSO
For Each f In colFiles
Debug.Print (f)
'Insert code here to do something with the matched files
CSV_to_XLS (f)
Next
'Garbage Collection
Set objFSO = Nothing
Set objRegExp = Nothing
End Sub
Sub RecursiveFileSearch(ByVal targetFolder As String, ByRef objRegExp As Object, _
ByRef matchedFiles As Collection, ByRef objFSO As Object)
Dim objFolder As Object
Dim objFile As Object
Dim objSubFolders As Object
'Get the folder object associated with the target directory
Set objFolder = objFSO.GetFolder(targetFolder)
'Loop through the files current folder
For Each objFile In objFolder.Files
If objRegExp.Test(objFile) Then
matchedFiles.Add (objFile)
End If
Next
'Loop through the each of the sub folders recursively
Set objSubFolders = objFolder.Subfolders
For Each objSubfolder In objSubFolders
RecursiveFileSearch objSubfolder, objRegExp, matchedFiles, objFSO
Next
'Garbage Collection
Set objFolder = Nothing
Set objFile = Nothing
Set objSubFolders = Nothing
End Sub
Sub CSV_to_XLS(strFile)
Dim wb As Workbook
Dim format_wb As Workbook
Application.DisplayAlerts = False
xlsxFormatFile = "data_formatting.xlsx"
formatSheet = "format template"
formatRange = "A1:LL8203"
Set wb = Workbooks.Open(Filename:=strFile, Local:=True)
Set format_wb = Workbooks.Open(Filename:=xlsxFormatFile, Local:=True)
Workbooks(format_wb).Sheets(formatSheet).Range(formatRange).Copy
Workbooks(wb).Sheets(1).Range(formatRange).PasteSpecial (xlPasteAll)
wb.SaveAs Replace(wb.FullName, ".csv", ".xlsx"), FileFormat:=51
wb.Close True
Application.DisplayAlerts = True
Set wb = Nothing
End Sub