Sorry if not enough info - first post! I have the following set of Macros that when run follows a path to each task. This works if I manually run it from the workbook I require, however I am looking for the ability to run the same Macro for all files in a folder. Eg, File 1,2,3,4,5 will each run the Macro below creating the new files for each. What is the best way to achieve this? Tried using this code, but this seems to only create files for the existing open workbook that contains no data:
Sub DirectoryFileLoop2()
Dim fileName As Variant
fileName = Dir("C:\New Name Test\ConvertxlsxTest\Test3\")
Application.ScreenUpdating = False
While fileName <> ""
Call CreateUniqueContainers
fileName = Dir
Wend
End Sub
Macro To Run Working when run from single file
Sub CreateUniqueContainers()
Dim xRCount As Long
Dim xSht As Worksheet
Dim xNSht As Worksheet
Dim I As Long
Dim xTRrow As Integer
Dim xCol As New Collection
Dim xTitle As String
Dim xSUpdate As Boolean
Set xSht = ActiveSheet
On Error Resume Next
xRCount = xSht.Cells(xSht.Rows.Count, 1).End(xlUp).Row
xTitle = "A1:BK1"
xTRrow = xSht.Range(xTitle).Cells(1).Row
For I = 2 To xRCount
Call xCol.Add(xSht.Cells(I, 7).Text, xSht.Cells(I, 7).Text)
Next
xSUpdate = Application.ScreenUpdating
Application.ScreenUpdating = False
For I = 1 To xCol.Count
Call xSht.Range(xTitle).AutoFilter(7, CStr(xCol.Item(I)))
Set xNSht = Nothing
Set xNSht = Worksheets(CStr(xCol.Item(I)))
If xNSht Is Nothing Then
Set xNSht = Worksheets.Add(, Sheets(Sheets.Count))
xNSht.Name = CStr(xCol.Item(I))
Else
xNSht.Move , Sheets(Sheets.Count)
End If
xSht.Range("A" & xTRrow & ":A" & xRCount).EntireRow.Copy xNSht.Range("A1")
xNSht.Columns.AutoFit
Next
xSht.AutoFilterMode = False
xSht.Activate
Application.ScreenUpdating = xSUpdate
Call CreateNewWorkbookForEachContainerSheet
End Sub
Sub CreateNewWorkbookForEachContainerSheet()
Dim ws As Worksheet
Dim Path As String
Path = "C:\New Name Test\ConvertxlsxTest\TestFull"
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
ws.Copy
ActiveSheet.Name = "Container"
Sheets("Container").Copy After:=Worksheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = "Orders"
Sheets("Container").Copy After:=Worksheets(Sheets.Count)
On Error Resume Next
ActiveSheet.Name = "Items"
ActiveWorkbook.SaveAs Path & "\" & ws.Name
ActiveWorkbook.Close SaveChanges:=True
Next ws
Application.ScreenUpdating = True
Call Delete_Sheet1
End SubSub Delete_Sheet1()
Kill "C:\New Name Test\Sheet1.xlsx"
End Sub