Alright, new version:
Option Compare Text
Sub UpdateQty()
Dim LR1 As Long
Dim LR2 As Long
Dim Remainder As Long
Dim i As Long
Dim j As Long
Application.ScreenUpdating = False
LR1 = Sheets(1).Cells(Rows.Count, "H").End(xlUp).Row
LR2 = Sheets(2).Cells(Rows.Count, "D").End(xlUp).Row
Sheets(2).Range("F1:F" & LR2).Copy Destination:=Sheets(2).Range("H1")
For i = 2 To LR1
Remainder = Sheets(1).Cells(i, "I").Value
For j = 2 To LR2
If Sheets(1).Cells(i, "E").Value = Sheets(2).Cells(j, "A").Value And Sheets(1).Cells(i, "H").Value = Sheets(2).Cells(j, "D").Value And Sheets(2).Cells(j, "H").Value > 0 Then
If Remainder <= Sheets(2).Cells(j, "H").Value Then
Sheets(2).Cells(j, "H").Value = Sheets(2).Cells(j, "H").Value - Remainder
Exit For
Else
Remainder = Remainder - Sheets(2).Cells(j, "H").Value
Sheets(2).Cells(j, "H").Value = 0
End If
End If
Next j
Next i
Sheets(2).Range("K2:K" & LR2).Formula = "=IF(OR(A2="""",D2=""""),0,SUMIFS(Sheet1!I:I,Sheet1!E:E,A2,Sheet1!H:H,D2))"
Application.ScreenUpdating = True
End Sub
Putting "Option Compare Text" at the start of the module should fix the case-sensitivity issue. The problem with leading/trailing spaces can best be solved by wrapping your earlier formulas in a "TRIM" function, e.g.:
=TRIM(LEFT(H4,FIND(" ",H4&" ")-1))
The new version should leave Sheet2, column F alone and create a new quantity total in column H. It should also add your SUMIFS formula to column K.
I don't see any issue with this sub procedure following another, as long as everything in your workbook is in the right place before this procedure runs. You can either call UpdateQty from your first macro or you can paste my code within your other procedure, just be sure that the Option Compare Text is at the very top and make sure you don't have any overlapping definitions with the first part of the process.
Give it a go, let me know if you have any issues...
Bookmarks