G'day guys -
I have the code below: I have 3 virtual machines. 2 virtual machines with the same code runs fine (however only 3 subs are on these two).
The below speaks to 6 pieces of roll formers in our factory and does run well (most days), I've been experiencing issues in relation to it just crashing randomly. The code can sometimes work for days even up to a week on an indefinite loop (by design) it needs to loop to each machine constantly within 8 seconds to determine to move next batches in.
However the code is ugly as hell, I'm just after tips or pointers or what may be causing memory leaks:
Will only post 1 machine (the others are the exact same code) as it calls one another.
I have the code below: I have 3 virtual machines. 2 virtual machines with the same code runs fine (however only 3 subs are on these two).
The below speaks to 6 pieces of roll formers in our factory and does run well (most days), I've been experiencing issues in relation to it just crashing randomly. The code can sometimes work for days even up to a week on an indefinite loop (by design) it needs to loop to each machine constantly within 8 seconds to determine to move next batches in.
However the code is ugly as hell, I'm just after tips or pointers or what may be causing memory leaks:
Will only post 1 machine (the others are the exact same code) as it calls one another.
Code:
Sub Timercontrol()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
'' Keep in mind MITSUBIHI takes 2 seconds to run it's side '' so 2+ your TIMETORUN value is the turn around time for new job load
TimeToRun = Now + TimeValue("00:00:08")
If v = 1 Then
Exit Sub
End If
If v = 0 Then
'TimeToRun = Now + TimeValue("00:00:07")
Application.OnTime TimeToRun, "LoadDownpipe"
End If
End Sub
Sub LoadDownpipe()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
''' LOAD JOB from Machine DATA sheet
ThisWorkbook.Sheets("Downpipe Machine Batch").Activate
'N3 = 3 Batch Completed
'P3 = 1 Opperator is ready for next job
'AJ3 = 2 Unique flag multi jobs won't move this way
If ActiveSheet.Range("N3").Value = "3" And ActiveSheet.Range("P3").Value = "1" And ActiveSheet.Range("AJ3").Value = "0" Then
'Unique Value flag don't shift haven't recieved grant's magic
ActiveSheet.Range("AJ3") = "2"
'''Job has been completed TIME STAMP
Range("AN3").Value = Now
''Engage the move of completed downpipe batch once completed triggers are found
Call movecompleteddownpipe
End If
'''''''''''''''''
''Run the Load''
''Upon STARTUP you need to write the 0 to each machine to ensure the fresh load.
''One shot trigger the job'' to ensure no double load (BATCH QTY/LENGTH)
'''''''''''''''''
If ActiveSheet.Range("N3").Value = "3" And ActiveSheet.Range("P3").Value = "1" And ActiveSheet.Range("AJ3") = "4" Then
Sheets("Downpipe Machine Data").Select
If Range("A3") >= 1 Then
Sheets("Downpipe Machine Batch").Activate
ActiveSheet.Range("AJ3") = "3"
'''''''''''''''''
''Load next job
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("B6").Value = ThisWorkbook.Sheets("Downpipe Machine Data").Range("A3").Value ''AS B2 in Data sheet is always going to be current First in First out concept
For i = 3 To 50000 ''count how many lines for this job (as jobs will always be stacked together this is easy to find
If Not ThisWorkbook.Sheets("Downpipe Machine Data").Range("A" & i).Value = ThisWorkbook.Sheets("Downpipe Machine Batch").Range("B6").Value Then
Exit For
End If
Next i
Sheets("Downpipe Machine Data").Select ''copy data to main menu page
ThisWorkbook.Sheets("Downpipe Machine Data").Range("A3:Z" & i - 1).Select
Selection.Copy
Sheets("Downpipe Machine Batch").Select
ThisWorkbook.Sheets("Downpipe Machine Batch").Cells(3, 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
'Finished loading the Downpipe JOB
''''TIME STAMP LOAD IN
Range("AM3").Value = Now
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''Remove data from the original DATA Sheet from Downpipe Machine Sheet where the Batch originated from
ThisWorkbook.Sheets("Downpipe Machine Data").Rows("3:" & i - 1).Delete
'''WIRTE Zeros if No QTY/Length is present to enforce machine register START
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F4").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F4:G4") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F5").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F5:G5") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F6").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F6:G6") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F7").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F7:G7") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F8").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F8:G8") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F9").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F9:G9") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F10").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F10:G10") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F11").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F11:G11") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F12").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F12:G12") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F13").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F13:G13") = "0"
End If
If ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F14").Value = "" Then
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("F14:G14") = "0"
End If
'''Tell the machine the job has been loaded
ActiveSheet.Range("R3") = "1"
'''AJ3 needs to be 0 awaiting batch complete and opperator load
End If
End If
'''Grant's magic - NOTHING WORKS IF! Grant's Magic doesn't come on!
If ActiveSheet.Range("AK3") = "1" Then
ActiveSheet.Range("R3") = "0"
ActiveSheet.Range("AJ3") = "0"
End If
'''END the Enforcement Section for Qty/Length
'IF non of the above criteria is met, call next machine
Call LoadGutter
End Sub
Sub movecompleteddownpipe()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
''Move the job to daily recorded sheet
'ThisWorkbook.Sheets("Downpipe Machine Batch").Range("B6").Value = ThisWorkbook.Sheets("Downpipe Machine").Range("B6").Value ''AS B2 in Data sheet is always going to be current First in First out concept
For i = 4 To 50000 ''count how many lines for this job (as jobs will always be stacked together this is easy to find
If Not ThisWorkbook.Sheets("Downpipe Machine Batch").Range("A" & i).Value = ThisWorkbook.Sheets("Downpipe Machine Batch").Range("A3").Value Then
Exit For
End If
Next i
Sheets("Downpipe Machine Batch").Select ''copy data to main menu page
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("A3:AN" & i - 1).Select
Selection.Copy
''' Open Completed Job Location
Workbooks.Open Filename:="C:\Users\Luke.STONEHOMES\Desktop\RecordedDailyJobs.xlsm"
'''
'''Record the Completed Downpipe Job to DailyRecordedJob Listing in the other workbook
Call RecordDownpipe
End Sub
Sub RecordDownpipe()
Sheets("Downpipe").Select
'Find last row
lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
Sheets("Downpipe").Activate
erow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ActiveSheet.Cells(erow, 1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWorkbook.Close
'Saved & Closed the workbook of record data from downpipe
Call ClearDownpipeBatch
End Sub
Sub ClearDownpipeBatch()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Sheets("Downpipe Machine Batch").Select
For i = 4 To 50000 ''count how many lines for this job (as jobs will always be stacked together this is easy to find
If Not ThisWorkbook.Sheets("Downpipe Machine Batch").Range("A" & i).Value = ThisWorkbook.Sheets("Downpipe Machine Batch").Range("A3").Value Then
Exit For
End If
Next i
'''Clear the job that was previously manufactured
Sheets("Downpipe Machine Batch").Select ''copy data to main menu page
ThisWorkbook.Sheets("Downpipe Machine Batch").Range("A3:CC" & i - 1).Select
Selection.ClearContents
'''Job has now been cleared successfully
'''Unique Value indicating Job is in the ready position for NEW JOB - Waiting for Grant's Magic
ActiveSheet.Range("AJ3") = "4"
'' Call next machine ''
Call LoadDownpipe
End Sub