This should work exactly as is against your own workbooks, as I've left your code untouched except for Integer
-> Long
and commenting out the unnecessary lines. (It works fine using my test worksheets.)
Note that it only uses one loop! The inner loop is replaced with filtering and sorting
Sub Demand_Minus_Storage()
'Dim QT As Long
'Dim i As Long
Dim Demand_WB As Workbook
Set Demand_WB = Workbooks.Open("C:\Users\rosipov\Desktop\eliran\MFG - GSS\Demand_Optics " & Format(Now(), "dd.mm.yyyy") & ".xlsx")
Dim storage_wb As Workbook
Set storage_wb = Workbooks.Open("C:\Users\rosipov\Desktop\eliran\MFG - GSS\OpticLabStorage.xlsm")
'storage_wb.Worksheets("Illuminator").Range("C3").Activate
'QT = ActiveCell.Value
Demand_WB.Worksheets("Illuminators").Activate
Dim rngRow As Range
With storage_wb.Worksheets("Illuminator")
For Each rngRow In .Range(.Rows(3), .Rows(WorksheetFunction.Match("*", .Range("A:A"), -1))).Rows
With Demand_WB.Worksheets("Illuminators").UsedRange.Offset(1)
.Sort .Columns(5) ' Tool Type
.Offset(-1).AutoFilter Field:=5, Criteria1:="=" & rngRow.Cells(1) & "*"
.Sort .Columns(2) ' Due Date
With .SpecialCells(xlCellTypeVisible).EntireRow.Areas(1)
Range(.Rows(1), .Rows(WorksheetFunction.Min(rngRow.Cells(3), .Rows.Count))).Delete
End With
.Offset(-1).AutoFilter
.Sort .Columns(2) ' Due Date
End With
Next
End With
Cells(1).Select
End Sub
Caveat:
This one loop technique will only work if the tool type in the demand table starts with the name of the tool from the storage table.
I've also added a tidied and fully documented version, so you can understand how it works:
Sub Demand_Minus_Storage()
Const n_DemandHeaderRows As Long = 1
Const i_SN_UTID As Long = 1
Const i_Due_Date As Long = 2
Const i_Tool_Type As Long = 5
Const n_StorageHeaderRows As Long = 2
Const i_Tool As Long = 1
Const i_QT As Long = 3
Dim rngRow As Range
Dim ƒ As WorksheetFunction: Set ƒ = WorksheetFunction
Dim storage_wb As Workbook
Set storage_wb = Workbooks.Open("C:\Users\rosipov\Desktop\eliran\MFG - GSS\OpticLabStorage.xlsm")
Dim Demand_WB As Workbook
Set Demand_WB = Workbooks.Open("C:\Users\rosipov\Desktop\eliran\MFG - GSS\Demand_Optics " & Format(Now(), "dd.mm.yyyy") & ".xlsx")
With storage_wb.Worksheets("Illuminator")
' Use the worksheet function "Match" to find the last storage used row
' Then loop through each storage row
For Each rngRow In .Range(.Rows(n_StorageHeaderRows + 1), .Rows(ƒ.Match("*", .Columns(i_SN_UTID), -1))).Rows
' Skip the header rows and at the same time add at least one row after the end of the table
With Demand_WB.Worksheets("Illuminators").UsedRange.Offset(n_DemandHeaderRows)
' Need to sort by tool type so the rows to be deleted are contiguous
.Sort .Columns(i_Tool_Type)
' Back up to last header row and apply the filter
' The filter is for any tool type that starts with the tool in the current storage row
.Offset(-1).AutoFilter Field:=i_Tool_Type, Criteria1:="=" & rngRow.Cells(i_Tool) & "*"
' Need to re-sort by date as we previously sorted by tool type
.Sort .Columns(i_Due_Date)
' Grab the first visible contiguous area. There is always at least the one from the row(s) after the end of the table.
' If there are any matching tool tips, these will form an area preceding the end of table area.
With .SpecialCells(xlCellTypeVisible).EntireRow.Areas(1)
' Make sure we don't delete more rows than were actually found.
' If none were found, empty rows at the end of the table get deleted.
Range(.Rows(1), .Rows(ƒ.Min(rngRow.Cells(i_QT), .Rows.Count))).Delete
End With
' Turn autofilter off and show all hidden rows
.Offset(-n_DemandHeaderRows).AutoFilter
' Need to re-sort by date as hidden rows were not sorted in previous date sort
.Sort .Columns(i_Due_Date)
End With
Next
End With
' Tidy up
Cells(1).Select
End Sub