Hello,
This may sound a little bit complex, but I am looking for a Code that would Loop through all excel files in a Folder and:
- on each excel workbook perform a check of Worksheet names with the Original template (order is not important although I can easily sort it if needed)
- on each excel worksheet (on all the workbooks in the loop) perform a check of Header column names and order
= if it will find a mismatch, for example 1st worksheet in the looped files will be named A and its name should be B (as in the TEMPLATE xls),
I will get a prompt/message box = Error found in Cell A1/Object "Sheet 1" + the object will get a yellow fill.
= The same goes for the Header cells = if A1 does not equal A1 in the template (it has different value) then message box + highlight
Also, its important the Loop will continue after prompt + fill.
I am checking 30+ excel files like this manually. Each has 3 Worksheets with 30+ columns. I am able to write a code to loop through the excel files and save them for example, but I cant figure out how to solve the compare with template + fill if different part on so many objects...
I am thinking about using and modyfing this code, But any possible solution on how to automate this is welcome.
Sub Loop_Compare_Highlight()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Thanks.
This may sound a little bit complex, but I am looking for a Code that would Loop through all excel files in a Folder and:
- on each excel workbook perform a check of Worksheet names with the Original template (order is not important although I can easily sort it if needed)
- on each excel worksheet (on all the workbooks in the loop) perform a check of Header column names and order
= if it will find a mismatch, for example 1st worksheet in the looped files will be named A and its name should be B (as in the TEMPLATE xls),
I will get a prompt/message box = Error found in Cell A1/Object "Sheet 1" + the object will get a yellow fill.
= The same goes for the Header cells = if A1 does not equal A1 in the template (it has different value) then message box + highlight
Also, its important the Loop will continue after prompt + fill.
I am checking 30+ excel files like this manually. Each has 3 Worksheets with 30+ columns. I am able to write a code to loop through the excel files and save them for example, but I cant figure out how to solve the compare with template + fill if different part on so many objects...
I am thinking about using and modyfing this code, But any possible solution on how to automate this is welcome.
Sub Loop_Compare_Highlight()
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim FldrPicker As FileDialog
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Set FldrPicker = Application.FileDialog(msoFileDialogFolderPicker)
With FldrPicker
.Title = "Select A Target Folder"
.AllowMultiSelect = False
If .Show <> -1 Then GoTo NextCode
myPath = .SelectedItems(1) & "\"
End With
'In Case of Cancel
NextCode:
myPath = myPath
If myPath = "" Then GoTo ResetSettings
'Target File Extension (must include wildcard "*")
myExtension = "*.xls"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'Save and Close Workbook
wb.Close SaveChanges:=True
'Get next file name
myFile = Dir
Loop
'Message Box when tasks are completed
MsgBox "Task Complete!"
ResetSettings:
'Reset Macro Optimization Settings
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Thanks.