Private Sub Worksheet_Change(ByVal Target As Range)
' Define the source and destination sheets
Dim wsSourceRCF As Worksheet
Dim wsSourceRack As Worksheet
Dim wsQuotation As Worksheet
Set wsSourceRCF = ThisWorkbook.Sheets("RCF TT+")
Set wsSourceRack = ThisWorkbook.Sheets("RACK")
Set wsQuotation = ThisWorkbook.Sheets("RCF TT+ QUOTATION")
' Handling changes in 'RCF TT+' sheet
If Not Intersect(Target, wsSourceRCF.Range("E2:E10")) Is Nothing Then
Application.EnableEvents = False
' Disable events to prevent infinite loops
Dim cell As Range
For Each cell In Intersect(Target, wsSourceRCF.Range("E2:E10"))
Dim quotationRow As Long
' Check if the selected value already exists in 'RCF TT+ QUOTATION'
On Error Resume Next
quotationRow = Application.Match(cell.Value, wsQuotation.Range("E:E"), 0)
On Error GoTo 0
If Not IsEmpty(cell.Value) Then
' If the value is selected in 'RCF TT+', and doesn't exist in quotation, add it
If quotationRow = 0 Then
Dim lastRowQuotation As Long
lastRowQuotation = wsQuotation.Cells(Rows.Count, "A").End(xlUp).Row + 1
' Copy the entire row from '2025' based on the dropdown value
Dim foundRow As Range
Set foundRow = ThisWorkbook.Sheets("2025").Range("E:E")
.Find(cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not foundRow Is Nothing Then
wsQuotation.Cells(lastRowQuotation, "A")
.Resize(1, 15).Value = wsSource.Cells(foundRow.Row, "A")
.Resize(1, 15).Value
wsQuotation.Cells(lastRowQuotation, "P").Resize(1, 4)
.Value = wsSource.Cells(foundRow.Row, "P")
.Resize(1, 4).Value
End If
End If
Else
' If the dropdown is cleared in 'RCF TT+', remove the corresponding row in quotation
If quotationRow > 0 Then
wsQuotation.Rows(quotationRow).Delete
End If
End If
Next cell
Application.EnableEvents = True
End If
' Re-enable events
End If
' Handling changes in 'RACK' sheet
If Not Intersect(Target, Union(wsSourceRack.Range("C4:H150"), wsSourceRack.Range("J4:P150"))) Is Nothing Then
Application.EnableEvents = False
' Disable events
Dim cellRack As Range
For Each cellRack In Intersect(Target, Union(wsSourceRack.Range("C4:H150"), wsSourceRack.Range("J4:P150")))
If Not IsEmpty(cellRack.Value) Then
' Find the corresponding row in '2025'
Dim foundRowRack As Range
Set foundRowRack = ThisWorkbook.Sheets("2025").Range("E:E").Find(cellRack.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not foundRowRack Is Nothing Then
' Check if this item from 'RACK' is already in the quotation
Dim quotationRackRow As Long
On Error Resume Next
quotationRackRow = Application.Match(foundRowRack.Value, wsQuotation.Range("E:E"), 0)
On Error GoTo 0
If quotationRackRow = 0 Then
' Add a new row if it doesn't exist
Dim lastRowQuotation As Long
lastRowQuotation = wsQuotation.Cells(Rows.Count, "A").End(xlUp).Row + 1
wsQuotation.Cells(lastRowQuotation, "A").Resize(1, 4).Value = ThisWorkbook.Sheets("2025").Cells(foundRowRack.Row, "A").Resize(1, 4).Value
wsQuotation.Cells(lastRowQuotation, "E").Value = foundRowRack.Value
wsQuotation.Cells(lastRowQuotation, "F").Resize(1, 6).Value = ThisWorkbook.Sheets("2025").Cells(foundRowRack.Row, "F").Resize(1, 6).Value
wsQuotation.Cells(lastRowQuotation, "K").Value = ThisWorkbook.Sheets("2025").Cells(foundRowRack.Row, "K").Value
wsQuotation.Cells(lastRowQuotation, "P") +
.Resize(1, 4).Value = ThisWorkbook.Sheets("2025").Cells(foundRowRack.Row, "P").Resize(1, 4).Value
Else
' If it exists, maybe you want to update something?
For now, we'll leave it as is.
' You can add logic here if you need to update quantities or other details.
End If
' Update the count in column P
Dim countRange As Range
Set countRange = wsQuotation.Range("E:E")
Dim matchCount As Long
matchCount = Application.WorksheetFunction.CountIf(countRange, foundRowRack.Value)
' Find the first occurrence of this value in 'RCF TT+ QUOTATION' and update column P
Dim firstMatch As Range
Set firstMatch = countRange.Find(foundRowRack.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not firstMatch Is Nothing Then
wsQuotation.Cells(firstMatch.Row, "P").Value = matchCount
End If
End If
Else
' If a value is cleared in 'RACK', you might want to remove it from quotation
' This requires careful consideration of how to identify the correct row to delete as multiple 'RACK' cells could refer to the same item.
' A more robust solution might involve a unique identifier.
' For now, we'll skip automatic deletion based on clearing
'RACK' cells.
End If
Next cellRack
Application.EnableEvents = True
' Re-enable events
End If
End Sub