- Excel 2010
Once I opened the file with the raw measurement by myself, I have a macro which performs some actions on the raw data, i.e. sorts it, deletes some stuff, copies some stuff into a new sheet.
At the end it closes the raw data file (while not saving the changes) and also closes, saves and renames the newly created Excel file the macro created after all the sorting, deleting and copying.
So considering I only have this macro file (.xlsm) open…
How can I run that macro on all the Excel files (.xlsx) which are in the same folder of said macro file? I’d prefer not to open them one by one and re-run the macro again.
I am currently closing the open “raw” file via:
ActiveWorkbook.Saved = True ActiveWorkbook.Close SaveChanges:=False Application.DisplayAlerts = True
Have to admit I don’t know if they fully make sense but they work.
It is simpler than the links you posted.
Sub stantial() Dim myfiles, wb As Workbook, ws As Worksheet myfiles = Dir(Thisworkbook.Path & "\*.xlsx") Do While Len(myfiles) <> 0 'Debug.Print myfiles '~~> open as readonly since you don't want to alter the original Set wb = Workbooks.Open(Thisworkbook.Path & "\" & myfiles, , True) '~~> Do your stuff here (edit, copy, sort etc...) wb.Close False Set wb = Nothing '~~> clean up myfiles = Dir Loop End Sub
Above code retrieves all filenames with .xlsx extention then opens it using Workbooks Open method.
Once you’ve opened the file, you can work on the open workbook object and do your stuff.
Before you proceed with the next, close the workbook, clean up variables. HTH.