I am running the following VBA code to transfer data from Worksheet Active Bays to worksheet Work Order Log. The code will not transfer data to the first 4 rows on the Work Order Log - it does delete the data from the Active Bays worksheet. If there is already data in a row below the first 4 rows on the Work Order Log worksheet then the data will tranfer correctly.
VBA Code:
Sub SaveWorkOrder()
Dim wsActive As Worksheet
Dim wsLog As Worksheet
Dim LastRowLog As Long
Dim LastRowActive As Long
Dim BayRow As Long
Dim LogRow As Long
On Error GoTo ErrorHandler
' Define the worksheets
Set wsActive = ThisWorkbook.Sheets("Active Bays")
Set wsLog = ThisWorkbook.Sheets("Work Order Log")
' Determine the first available row in Work Order Log
LastRowLog = wsLog.Cells(wsLog.Rows.Count, "A").End(xlUp).Row
If wsLog.Cells(LastRowLog, "A").Value = "" Then
LogRow = 1 ' Start at row 1 if the log is empty
Else
LogRow = LastRowLog + 1
End If
' Find the last used row in Active Bays
LastRowActive = wsActive.Cells(wsActive.Rows.Count, "A").End(xlUp).Row
' Loop through Active Bays to transfer rows with "Completed" status
For BayRow = 2 To LastRowActive
If wsActive.Cells(BayRow, "E").Value = "Completed" Then ' Assuming "Status" is column E
' Write the row to the Work Order Log
wsLog.Rows(LogRow).Value = wsActive.Rows(BayRow).Value
LogRow = LogRow + 1 ' Move to the next available row
' Clear the row in Active Bays
wsActive.Rows(BayRow).ClearContents
End If
Next BayRow
MsgBox "Work orders successfully saved to log.", vbInformation
Exit Sub
ErrorHandler:
MsgBox "An error occurred: " & Err.Description, vbExclamation
End Sub