You can add second test to final IF.
But what in case when 0 is in first row of given product?
Sub UpdateTotalQuantities2()
Dim ws As Worksheet
Dim lastRow As Long
Dim currentProduct As String
Dim minQuantity As Double
Dim currentQuantity As Double
Dim i As Long
' Set the worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") ' Change "Sheet1" to your actual sheet name
' Find the last row in column B
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
' Initialize variables
currentProduct = ""
' Loop through the rows
For i = 2 To lastRow
' Check if the product name is the same as the previous row
If ws.Cells(i, 1).Value Like "*Total*" Then
' Update the Total row with the calculated totalQuantity
ws.Cells(i, 2).Value = minQuantity
ElseIf ws.Cells(i, 1).Value = currentProduct Then
' Update total quantity with the minimum value
currentQuantity = ws.Cells(i, 2).Value
If currentQuantity < minQuantity and currentQuantity<>0 Then
minQuantity = currentQuantity
End If
Else
' If a new product is encountered, update the currentProduct and totalQuantity
currentProduct = ws.Cells(i, 1).Value
minQuantity = ws.Cells(i, 2).Value
End If
Next i
End Sub
Bookmarks