Попробуйте, если я предполагаю, что в вашем инвентаре нет повторяющихся предметов, иначе произойдет сбой при хранении предметов в словаре
Option Explicit
Sub printInvoice()
Dim ws As Worksheet, wsInventory As Worksheet
Dim LastRow As Long, LastRowInventory As Long, i As Long
Dim DictInventory As Scripting.Dictionary 'You need to check Microsoft Scripting Runtime on your references for this to work
Dim C As Range
Dim Substract As Integer
Dim arrInventory 'here we will store your inventory
With ThisWorkbook 'with this you are referencing both sheets on this workbook so its shorter to call them
Set ws = .Sheets("Sheet1")
Set wsInventory = .Sheets("Sheet2")
End With
Set DictInventory = New Scripting.Dictionary
LastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row 'this is to know how many items are sold
With wsInventory
LastRowInventory = .Cells(.Rows.Count, 2).End(xlUp).Row 'this is to get all your inventory
arrInventory = .Range(.Cells(1, 2), .Cells(LastRowInventory, 4)).Value 'storing your inventory
End With
For i = 2 To UBound(arrInventory) 'here we loop through your inventory to know in which row is every item and store the row
If Not arrInventory(i, 1) = vbNullString Then DictInventory.Add arrInventory(i, 1), arrInventory(i, 3)
Next i
With ws
For Each C In .Range("B2:B" & LastRow) 'we are looping through the first sheet to substract the inventory
Substract = .Cells(C.Row, 3)
arrInventory(DictInventory(C.Value), 3) = arrInventory(DictInventory(C.Value), 3) - Substract
Next C
End With
'Paste the values modified to the sheet
wsInventory.Range(wsInventory.Cells(1, 2), wsInventory.Cells(LastRowInventory, 4)).Value = arrInventory
End Sub
Редактировать: Я не знаю, думали ли вы уже оэто, но во избежание проблем, я бы использовал список проверки данных на листе получения, чтобы подобрать продукт, который подается в инвентарном листе.Таким образом, не будет проблем с поиском некоторых продуктов для опечаток.