VBA Copy of Formatting to Files in Subfolders

JSyms

New Member
Joined
May 28, 2015
Messages
1
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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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