Sub Add_PODs()
Application.ScreenUpdating = False
Dim CS As Worksheet
Set CS = ActiveSheet
Sheets("Shipment Data").Activate
'Import Data
ActiveWorkbook.XmlMaps("WestcoastPODs").DataBinding.Refresh
Dim lastrow, firstPOD, firstList As Long
Sheets("PODs").Range("Westcoast_PODs[#All]").RemoveDuplicates Columns:=Array(1, 2 _
, 3, 4), Header:=xlYes
ActiveWorkbook.Worksheets("PODs").ListObjects("Westcoast_PODs").Sort.SortFields _
.Clear
ActiveWorkbook.Worksheets("PODs").ListObjects("Westcoast_PODs").Sort.SortFields _
.Add Key:=Range("Westcoast_PODs[[#All],[Westcoast Reference]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("PODs").ListObjects("Westcoast_PODs").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
firstPOD = Sheets("Calculations").Range("AC14").Value
'Check which jobs are on the POD list and which jobs can be permanently ignored now
With Sheets("Shipment Data")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("P" & firstPOD & ":P" & lastrow).FormulaR1C1 = "=IF([@Signed]<>"""",""N"",IF(ISNUMBER(INDEX(Westcoast_PODs[Westcoast Reference],MATCH([@[PL Job Number]],Westcoast_PODs[Westcoast Reference],0))),""Y"",""N""))"
.Range("Q" & firstPOD & ":Q" & lastrow).FormulaR1C1 = "=IF(ISNUMBER(INDEX(Westcoast_PODs[Westcoast Reference],MATCH([@[PL Job Number]],Westcoast_PODs[Westcoast Reference],0))),IF(OR([@[Date Delivered]]>(INDEX(Westcoast_PODs[Min POD],MATCH([@[PL Job Number]],Westcoast_PODs[Westcoast Reference],0))),[@[Date Delivered]]=""""),""Y"",""N""),""N"")"
.ListObjects("WestcoastData").Sort. _
SortFields.Clear
.ListObjects("WestcoastData").Sort. _
SortFields.Add Key:=Range("WestcoastData[[#All],[Replace Name]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .ListObjects("WestcoastData"). _
Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
'Set Name/Date Formulas for jobs on POD list and set cells as values
Dim DateFormulaPart1, DateFormulaPart2, DateFormulaPart3 As String
Dim NameFormulaPart1, NameFormulaPart2, NameFormulaPart3, NameFormulaPart4 As String
firstList = Sheets("Calculations").Range("AC15").Value
NameFormulaPart1 = "=IF(LEFT([@[Consignee Name]],33)=""Clarity Computer Distribution Ltd"",""Y_Y_Y()"",IF(LEFT([@[Consignee Name]],12)=""Derry Morgan"",""Y_Y_Y()"",IF(LEFT([@[Consignee Name]],17)=""Primeline Express"",""Y_Y_Y()"",X_X_X())))"
NameFormulaPart2 = "Internal Job"
NameFormulaPart3 = "IFERROR(VLOOKUP([@[PL Job Number]],PODOverride,3,FALSE),Z_Z_Z())"
NameFormulaPart4 = "IFERROR(INDEX(Westcoast_PODs[Signature],MATCH(1,(Westcoast_PODs[Westcoast Reference]=[@[PL Job Number]])*(Westcoast_PODs[Signature]<>""""),0)),"""")"
With Sheets("Shipment Data").Range("J" & firstList)
.FormulaArray = NameFormulaPart1
Application.Wait (Now + TimeValue("0:00:03"))
.Replace "X_X_X()", NameFormulaPart3
Application.Wait (Now + TimeValue("0:00:03"))
.Replace "Y_Y_Y()", NameFormulaPart2
Application.Wait (Now + TimeValue("0:00:03"))
.Replace "Z_Z_Z()", NameFormulaPart4
Application.Wait (Now + TimeValue("0:00:03"))
End With
With Sheets("Shipment Data")
If firstPOD > 3 Then
.Range("Q2:Q" & firstPOD - 1).Value = "N"
End If
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("Q" & firstPOD & ":Q" & lastrow).FormulaR1C1 = "=IF(AND([@[Date Delivered]]>0,[@Signed]<>""""),""N"",IF(ISNUMBER(INDEX(Westcoast_PODs[Westcoast Reference],MATCH([@[PL Job Number]],Westcoast_PODs[Westcoast Reference],0))),""Y"",""N""))"
.ListObjects("WestcoastData").Sort. _
SortFields.Clear
.ListObjects("WestcoastData").Sort. _
SortFields.Add Key:=Range("WestcoastData[[#All],[Replace Date]]"), SortOn:= _
xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .ListObjects("WestcoastData"). _
Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
firstList = Sheets("Calculations").Range("AC16").Value
Sheets("Shipment Data").Range("J" & firstList).AutoFill Destination:=Range("J" & firstList & ":J" & lastrow), Type:=xlFillDefault
DateFormulaPart1 = "=IF(LEFT([@[Consignee Name]],33)=""Clarity Computer Distribution Ltd"",Y_Y_Y(),IF(LEFT([@[Consignee Name]],12)=""Derry Morgan"",Y_Y_Y(),IF(LEFT([@[Consignee Name]],17)=""Primeline Express"",Y_Y_Y(),X_X_X())))"
DateFormulaPart2 = "WORKDAY([@[Ready Date]],1,PublicHols)"
DateFormulaPart3 = "(IFERROR(VLOOKUP([@[PL Job Number]],PODOverride,2,FALSE),IFERROR(INDEX(Westcoast" & _
"_PODs[Min POD],MATCH([@[PL Job Number]],(Westcoast_PODs[Westcoast Reference]),0)),"""")))"
With Sheets("Shipment Data").Range("I" & firstList)
.FormulaArray = DateFormulaPart1
Application.Wait (Now + TimeValue("0:00:03"))
.Replace "X_X_X()", DateFormulaPart3
Application.Wait (Now + TimeValue("0:00:03"))
.Replace "Y_Y_Y()", DateFormulaPart2
Application.Wait (Now + TimeValue("0:00:03"))
End With
Sheets("Shipment Data").Range("I" & firstList).AutoFill Destination:=Range("I" & firstList & ":I" & lastrow), Type:=xlFillDefault
With Sheets("Shipment Data").Range("I" & firstList & ":J" & lastrow)
.Value = .Value
End With
ActiveWorkbook.Worksheets("Shipment Data").ListObjects("WestcoastData").Sort. _
SortFields.Clear
ActiveWorkbook.Worksheets("Shipment Data").ListObjects("WestcoastData").Sort. _
SortFields.Add Key:=Range("WestcoastData[[#All],[PL Job Number]]"), SortOn _
:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Shipment Data").ListObjects("WestcoastData"). _
Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
CS.Activate
Application.ScreenUpdating = True
PODForm.Show
End Sub
One thing I tried was writing the table to an array, and running a loop through columns of the array to do the checks instead of using the helper columns. I used VBA to check each cell value against the imported data and if it needed to be replaced, I added the cell address corresponding to the array address to a range variable (using union after the first address).
Bookmarks