Sub CompareAndHighlight()
Dim rng1 As Range, i As Long
For i = 1 To Sheets("Sheet1").Range("D" & Rows.Count).End(xlUp).Row
Set rng1 = Sheets("Sheet1").Range("D" & i)
If StrComp(Trim(rng1.Text), Trim(Sheets("Sheet1").Range("H7").Text), vbTextCompare) = 0 Then
rng1.Interior.Color = RGB(255, 255, 0)
'Reduce quantity by quantity selected
rng1.Offset(0, 1).Value = rng1.Offset(0, 1).Value - Sheets("Sheet1").Range("I7").Value
End If
Set rng1 = Nothing
Next i
End Sub
This version will process multiple input values in H:I, starting at row 7:
Sub UpdateInventory()
Dim rNew As Long 'Row of new items
Dim rTable As Long 'Row within main table
Dim partNo As Variant 'To store part number being processed
Dim qty As Variant 'To store new quantity
With Worksheets("Sheet1")
'Uncomment the following line if you want to clear out cell colouring
'in column "D" so that it is easier to see which rows have been
'affected by running this macro
'.Columns("D").Interior.Color = xlNone
For rNew = 7 To .Range("H" & .Rows.Count).End(xlUp).Row
partNo = Trim(.Cells(rNew, "H").Text)
qty = .Cells(rNew, "I").Value
For rTable = 1 To .Range("D" & .Rows.Count).End(xlUp).Row
If StrComp(Trim(.Cells(rTable, "D").Text), partNo, vbTextCompare) = 0 Then
'Highlight cell to show that change has occurred?
.Cells(rTable, "D").Interior.Color = RGB(255, 255, 0)
'Reduce quantity by quantity selected
.Cells(rTable, "E").Value = .Cells(rTable, "E").Value - qty
Exit For
End If
Next
Next
End With
End Sub
Note: The inner loop could be replaced with a Find. If you have a lot of data, that would be more efficient. If you don't have a lot of data (e.g. more than a couple of hundred rows) my preference would be to continue to use the loop.
To use different sheets for the drop downs and the stock list, I would use the following:
Option Explicit
Sub UpdateInventory()
Dim wsJobCard As Worksheet
Dim r1JobCard As Long
Dim rJobCard As Long
Dim colPartNoJobCard As String
Dim colQtyJobCard As String
Dim wsPartsList As Worksheet
Dim r1PartsList As Long
Dim rPartsList As Long
Dim colPartNoPartsList As String
Dim colQtyPartsList As String
Dim partNo As Variant
Dim qty As Variant
Set wsJobCard = Worksheets("Job_Card")
Set wsPartsList = Worksheets("Parts_List")
'Adjust these to show which columns are being used on the two sheets
colPartNoJobCard = "G" '????
colQtyJobCard = "H" '????
colPartNoPartsList = "B"
colQtyPartsList = "C"
'Adjust these to show which row is the start of data on each sheet
r1JobCard = 67
r1PartsList = 2
With wsPartsList
'Uncomment the following line if you want to clear out previous
'cell colouring so that it is easier to see which rows have been
'affected by running this macro
'.Columns(colPartNoPartsList).Interior.Color = xlNone
For rJobCard = r1JobCard To wsJobCard.Range(colPartNoJobCard & wsJobCard.Rows.Count).End(xlUp).Row
partNo = Trim(wsJobCard.Cells(rJobCard, colPartNoJobCard).Text)
qty = wsJobCard.Cells(rJobCard, colQtyJobCard).Value
For rPartsList = 1 To .Range(colPartNoPartsList & .Rows.Count).End(xlUp).Row
If StrComp(Trim(.Cells(rPartsList, colPartNoPartsList).Text), partNo, vbTextCompare) = 0 Then
'Highlight cell to show that change has occurred?
.Cells(rPartsList, colPartNoPartsList).Interior.Color = RGB(255, 255, 0)
'Reduce quantity by quantity selected
.Cells(rPartsList, colQtyPartsList).Value = .Cells(rPartsList, colQtyPartsList).Value - qty
Exit For
End If
Next
Next
End With
End Sub