Sub AddSOtoSA()
Dim SO As Worksheet
Dim SA3 As Worksheet
Dim SAlastrow As Long
Set SO = Sheets("Sheet2")
Set SA3 = Sheets("Sheet1")
SAlastrow = ThisWorkbook.Sheets("Sheet1").Range("G" & Rows.count).End(xlUp).row
Application.ScreenUpdating = False
With SO
'===============================================================
'This is to set the autofilter to filter out those that at 'N'
'===============================================================
'*****************
'if range error occur, go to worksheet sales order and select any data cells
'**************
.Range("1:1").Select
Selection.AutoFilter Field:=29, Criteria1:="Tij 3"
Selection.AutoFilter Field:=30, Criteria1:="Y"
'===============================================================
'This foreach loop reads through Sales Order worksheet data that are filtered
'===============================================================
For Each ce In SO.Range("G2:G" & SO.Cells(Rows.count, "G").End(xlUp).row).SpecialCells(xlCellTypeVisible)
'------------------------------------------
'looping through Shipment allocation
'to see if part number matches
'with SO number
'------------------------------------------
For i = 2 To SAlastrow Step 1
'-----------------------------------------
'Check if current cell in SA is equals to SO number
'---------------------------------------
If .Cells(i, "g") = ce.Value Then
'-------------------------------------------------------
'Check if next cell of SA is not equals to SO
'then insert an empty row
'copy the entire row data from SO and insert to empty row
'-------------------------------------------------------
If .Cells(i + 1, "g") <> ce.Value Then
Worksheets("Sheet1").Rows(i + 1 & ":" & i + 1).Insert
ce.EntireRow.Copy Destination:=Worksheets("Sheet1").Rows(i + 1 & ":" & i + 1)
Exit For
End If
Else
End If
Next i
Next ce
Selection.AutoFilter Field:=29
Selection.AutoFilter Field:=30
End With
Range("A2").Select
Application.ScreenUpdating = True
End Sub
Bookmarks