I have the following VBA code and everything goes well except for copying the data from sheet: daily log to sheet backlog.
Sheet Daily Log:
In column C (from row 10) I can make a choice and 1 of the choices is "new".
Sheet Backlog: in this sheet I have a row with the name "Daily Log *".
Action:
If there is a row in the sheet Daily Log with the text "new" then a row must be added in the sheet Backlog under "Daily Log *". Row 10 is the template of the row that must then be added.
If the row is added in the sheet Backlog then the data of the row, range column B to Column M in the sheet Daily log where new is stated must be copied and placed in the new row, but then in the range Column C to Column N.
Only the value must then be copied.
The VBA code below works fine until the creation of the new row in the sheet Backlog. But copying the data from the sheet daily log does not work. Whatever I change. it still does not work. Who can help me?
Sheet Daily Log:
In column C (from row 10) I can make a choice and 1 of the choices is "new".
Sheet Backlog: in this sheet I have a row with the name "Daily Log *".
Action:
If there is a row in the sheet Daily Log with the text "new" then a row must be added in the sheet Backlog under "Daily Log *". Row 10 is the template of the row that must then be added.
If the row is added in the sheet Backlog then the data of the row, range column B to Column M in the sheet Daily log where new is stated must be copied and placed in the new row, but then in the range Column C to Column N.
Only the value must then be copied.
The VBA code below works fine until the creation of the new row in the sheet Backlog. But copying the data from the sheet daily log does not work. Whatever I change. it still does not work. Who can help me?
VBA Code:
Sub AddRowsAndCopyDataToBacklogFinal()
Dim wsDailyLog As Worksheet, wsBacklog As Worksheet
Dim lastRow As Long, newCount As Long, insertRow As Long
Dim foundCell As Range
Dim dailyLogRow As Long, backlogRow As Long
Dim srcRange As Range, destRange As Range
' Speed up processing
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
' Set worksheets
On Error Resume Next
Set wsDailyLog = ThisWorkbook.Sheets("Daily log")
Set wsBacklog = ThisWorkbook.Sheets("backlog")
On Error GoTo 0
' Ensure worksheets exist
If wsDailyLog Is Nothing Or wsBacklog Is Nothing Then
MsgBox "One or both worksheets are missing. Check sheet names.", vbCritical
GoTo Cleanup
End If
' Find last used row in column B of "Daily log"
lastRow = wsDailyLog.Cells(wsDailyLog.Rows.Count, 2).End(xlUp).Row
' Count "new" occurrences in column C from row 10 onwards
newCount = Application.WorksheetFunction.CountIf(wsDailyLog.Range("C10:C" & lastRow), "new")
' Exit if no "new" statuses are found
If newCount = 0 Then
MsgBox "No rows with 'new' found in 'Daily log' column C from row 10 onward.", vbExclamation
GoTo Cleanup
End If
' Find the first occurrence of "Daily Log *" in "backlog" column C
Set foundCell = wsBacklog.Range("C:C").Find(What:="Daily Log *", LookAt:=xlPart, MatchCase:=False)
' Ensure foundCell is valid
If foundCell Is Nothing Then
MsgBox "No row with 'Daily Log *' found in column C of 'backlog'.", vbExclamation
GoTo Cleanup
End If
insertRow = foundCell.Row + 1 ' Insert below this row
' Ensure row 10 in "backlog" is not empty (it must be a template)
If Application.WorksheetFunction.CountA(wsBacklog.Rows(10)) = 0 Then
MsgBox "Row 10 in 'backlog' is empty and may not be a valid template.", vbExclamation
GoTo Cleanup
End If
' Insert new rows first
wsBacklog.Rows(insertRow).Resize(newCount).Insert Shift:=xlDown
' Copy row 10 as a template to the newly inserted rows
wsBacklog.Rows(10).Copy
wsBacklog.Rows(insertRow).Resize(newCount).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
' Copy values from Daily Log (B:M) to Backlog (C:N)
backlogRow = insertRow ' Start pasting at the first inserted row
For dailyLogRow = 10 To lastRow ' Start from row 10 in "Daily Log"
If wsDailyLog.Cells(dailyLogRow, 3).Value = "new" Then
' Copy range B:M from Daily Log
Set srcRange = wsDailyLog.Range("B" & dailyLogRow & ":M" & dailyLogRow)
' Paste into range C:N in Backlog
Set destRange = wsBacklog.Range("C" & backlogRow & ":N" & backlogRow)
' Paste values only
destRange.Value = srcRange.Value
' Move to the next row in Backlog
backlogRow = backlogRow + 1
End If
Next dailyLogRow
MsgBox newCount & " rows successfully added and data copied to 'backlog'.", vbInformation
Cleanup:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
End Sub