I have written a macro that is supposed to open and close thousands of workbooks and take the information from these. It fills up the list in sheet2 and when it reaches row 50000 it calls a cleaning macro which sorts the data in sheet1. The macro seems to be working fine except for the memory consumption which keeps increasing until Excel tells me it has run out of it. I have tried implementing a workbook save every time the cleaner macro gets called as it seemed to have helped someone else on the forum with the same problem, but for me it did nothing. Does anyone have any ideas to fix this? I have included my code below.
Sub LoopAllExcelFilesInFolder()
'PURPOSE: To loop through all Excel files in a user specified folder
Dim wb As Workbook
Dim myPath As String
Dim myFile As String
Dim myExtension As String
Dim rc As Long
Dim wbRC As Long
Dim rs As Variant
On Error Resume Next
''Optimize Macro Speed
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
myPath = "C:\Users\QQQ\Documents\Macro testing\BoM_ALL\"
myExtension = "*.xlsx"
'Target Path with Ending Extention
myFile = Dir(myPath & myExtension)
'Loop through each Excel file in folder
Do While myFile <> ""
If Worksheets(2).Range("A50000").Value <> "" Then
Call Cleaner
End If
'Set variable equal to opened workbook
Set wb = Workbooks.Open(Filename:=myPath & myFile)
'DO stuff in opened wb ------------------------------------------------------------------
wb.Worksheets(1).Activate
Range("B:B,D:D,E:E").Select
Selection.Delete Shift:=xlToLeft
ActiveSheet.Range("a1").CurrentRegion.Select
wbRC = Selection.Rows.Count
rs = Application.Match(Range("C3").Value, ThisWorkbook.Worksheets(3).Range("A1:A66950"), 0)
If Application.IsNumber(rs) Then
Range("C2:C" & wbRC).Value = ThisWorkbook.Worksheets(3).Cells(rs, 2).Value
Selection.AutoFilter
ActiveSheet.Range("A1:C" & wbRC).AutoFilter Field:=2, Criteria1:=Array( _
"1", "2", "3", "4", "5", "6", "A", "B"), Operator:=xlFilterValues
Range("A1").Offset(1, 0).Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
ThisWorkbook.Worksheets(2).Activate
If Range("A1").Value = "" Then
Range("A1").Select
Else
ActiveSheet.Range("a1").End(xlDown).Offset(1, 0).Select
End If
ActiveSheet.Paste
ActiveSheet.Range(Selection.Address).RemoveDuplicates Columns:=Array(1), Header:=xlNo
End If
'Close Workbook
wb.Application.CutCopyMode = False
wb.Close SaveChanges:=False
'Get next file name
myFile = Dir
Loop
Call Cleaner
'Message Box when tasks are completed
MsgBox "Task Complete!"
'ResetSettings:
'Reset Macro Optimization Settings
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Sub Cleaner()
Dim rng1 As Range
Dim rng2 As Range
Dim cell As Range
Dim y As Variant
Dim ri As Long
Dim ci As Integer
Set rng1 = Worksheets(1).Range("A:A")
Set rng2 = Worksheets(2).Range("A:A")
Worksheets(1).Activate
ActiveSheet.Range("a1").CurrentRegion.Select
ri = Selection.Rows.Count
Range("A1").Select
For Each cell In rng2
If cell.Value = "" Then
ThisWorkbook.Worksheets(2).Activate
ActiveSheet.Range("a1").CurrentRegion.Select
Selection.Delete
ThisWorkbook.Save
Exit Sub
End If
'y = row location of match
y = Application.Match(cell.Value, rng1, 0)
'if not a match then write in the new machine number
If Not Application.IsNumber(y) Then
Cells(ri + 1, 1) = cell.Value
Cells(ri + 1, 2) = cell.Offset(0, 2).Value
ri = ri + 1
'if mat number exists then write machine number in a new column
Else
ci = 2
Do While True
If Cells(y, ci).Value <> "" Then
If Cells(y, ci).Value = cell.Offset(0, 2).Value Then
Exit Do
End If
Else
Cells(y, ci) = cell.Offset(0, 2).Value
Exit Do
End If
ci = ci + 1
Loop
End If
Next
ThisWorkbook.Worksheets(2).Activate
ActiveSheet.Range("a1").CurrentRegion.Select
Selection.Delete
ThisWorkbook.Save
End Sub