Trevor Kaltenbrunn
New Member
- Joined
- Feb 2, 2019
- Messages
- 9
Hi All
I have written a code that i cant get working, Please can someone help?
This code is for automating invoice to inventory so that when the invoice has a quantity of a certain item it will subtract from the stock. If i run the code it works on the first line to subtract the correct value entered into the invoice off the inventory.
Sheet 1 is my invoice and the stock code is entered into Row B16 to B26 or last row (The invoice is limited to 10 rows)
Sheet 2 is the inventory list and the stock is entered from A2 to last row. (The inventory could expand in rows as new inventory is added.
Please see below code, Please can you give me assistance.
I have written a code that i cant get working, Please can someone help?
This code is for automating invoice to inventory so that when the invoice has a quantity of a certain item it will subtract from the stock. If i run the code it works on the first line to subtract the correct value entered into the invoice off the inventory.
Sheet 1 is my invoice and the stock code is entered into Row B16 to B26 or last row (The invoice is limited to 10 rows)
Sheet 2 is the inventory list and the stock is entered from A2 to last row. (The inventory could expand in rows as new inventory is added.
Please see below code, Please can you give me assistance.
Code:
Sub updateInventorys()'we declare 4 variables
Dim rng1, rng2, cell1, cell2 As Range
Dim lastRow1 As Long
lastRow1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Set rng1 = Worksheets("Sheet1").Range("B16,B" & lastRow1)
Dim lastRow2 As Long
lastRow2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Set rng2 = Worksheets("Sheet2").Range("A2,A" & lastRow2)
For Each cell1 In rng1
If IsEmpty(cell1.Value) Then Exit For
For Each cell2 In rng2
If IsEmpty(cell2.Value) Then Exit For
If cell1 = cell2 Then
cell2.Offset(0, 1) = cell2.Offset(0, 1) - cell1.Offset(0, 2)
End If
Next cell2
Next cell1
End Sub
Last edited by a moderator: