Advertisement
Guest User

Untitled

a guest
Aug 4th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub NewUpdateLoads()
  2.  
  3. Application.ScreenUpdating = False
  4.  
  5. ''Assumptions''
  6.  
  7.  
  8.  
  9. Dim Loads, Data, Carriers, Invoices, Quotes As Worksheet
  10. Dim StartRow_Q, Col_Q, StartRow_L, Col_L, N, i As Integer
  11. Dim SearchRange, BolNumR As Range
  12. Dim SearchVal As String
  13. Dim DataRow, CarriersRow, InvoicesRow, shprow As Integer
  14.  
  15. Dim StatusLink As String
  16. Dim StatusText As String
  17. Dim ShipDate As Variant
  18. Dim OfferedLane As String
  19. Dim Customer As String
  20. Dim Equipment As String
  21. Dim Ratee As Variant
  22. Dim Profit As Variant
  23. Dim Carrier As String
  24. Dim CarrierEmail As String
  25. Dim CarrierPhone As String
  26. Dim InvoiceStatus As String
  27. Dim CustomerInvoiced As String
  28. Dim InvoiceAge As Variant
  29. Dim NewNumber As String
  30.  
  31. 'To see how long the macro takes to run
  32. Dim StartTime As Double
  33. Dim SecondsElapsed As Double
  34.  
  35. StartTime = Timer
  36.  
  37. Set Quotes = Worksheets("Quotes")
  38. Set Loads = Worksheets("Loads")
  39. Set Data = Worksheets("Data")
  40. Set Carriers = Worksheets("Carriers")
  41. Set Invoices = Worksheets("Invoices")
  42.  
  43. 'number of rows in each sheet
  44. DataRow = Invoices.Cells(Rows.Count, 4).End(xlUp).Row + 1000
  45. 'MsgBox DataRow
  46. CarriersRow = Carriers.Cells(Rows.Count, 4).End(xlUp).Row
  47. InvoicesRow = Invoices.Cells(Rows.Count, 1).End(xlUp).Row
  48.  
  49. 'row you want the macro to start on in 'Quotes'
  50. StartRow_Q = 2
  51. 'column where your load numbers are in 'Quotes'
  52. Col_Q = 11
  53. 'row you want the macro to start on in 'Loads'
  54. StartRow_L = 2
  55. 'column where load#'s are in 'Loads'
  56. Col_L = 3
  57.  
  58. '''''Added by Ben, please uncomment if it is still neccesary
  59. 'Loads.Rows("2:" & Loads.Rows.Count).ClearContents
  60.  
  61. On Error Resume Next
  62.  
  63. 'Look for load# from QUOTES in LOADS, if it doesn't exist in LOADS, add it. (NOT adding rest of the columns, that is done in the next for loop)
  64. For i = StartRow_Q To Quotes.Cells(Rows.Count, Col_Q).End(xlUp).Row + 1
  65.  
  66. 'Quotes.Cells(i, 13).Formula = "=if(h" & i & "<>"""",IF(K" & i & "<>"""",1,0),"")" ' add a 1 or 0 to a help col to calculate quote win %
  67.  
  68.     If Quotes.Cells(i, 8) <> "" And Quotes.Cells(i, 11) <> "" Then ' if there is a quote and order number ( won the quote)
  69.        Quotes.Cells(i, 13).Value = "1"
  70.     End If
  71.  
  72.     If Quotes.Cells(i, 8) <> "" And Quotes.Cells(i, 11) = "" Then ' if there is a quote and and no order number (didnt win the quote)
  73.        Quotes.Cells(i, 13).Value = "0"
  74.     End If
  75.      
  76.     'Load # used to search
  77.    SearchVal = Quotes.Cells(i, Col_Q)
  78.     'MsgBox SearchVal
  79.    Set SearchRange = Loads.Range(Loads.Cells(StartRow_L, 3), Loads.Cells(Rows.Count, 3).End(xlUp))
  80.    
  81.     'If no match in Loads then write the Load #
  82.    If IsNumeric(Application.Match(SearchVal, SearchRange, 0)) = False Then
  83.     'load #
  84.        N = Loads.Cells(Rows.Count, 3).End(xlUp).Row + 1
  85.         Loads.Unprotect "superior"
  86.         Loads.Range("C" & N) = SearchVal
  87.         'MsgBox N
  88.    End If
  89.    
  90.  
  91. Next i
  92.  
  93. 'Search for load#'s in LOADS that don't have info in the rest of the columns, add info for the rest of the columns
  94. For j = StartRow_Q To Loads.Cells(Rows.Count, Col_L).End(xlUp).Row + 1
  95.  
  96. If Loads.Range("A" & j) = Empty Then
  97.    
  98.     IRow = 0 'row # in Invoices G:G
  99.    DRow = 0 'row # in Data D:D
  100.    CRow = 0 'row # in Carriers
  101.    DBPRow = 0 'row # in Data BP:BP
  102.  
  103.     StatusLink = Empty
  104.     StatusText = Empty
  105.     ShipDate = Empty
  106.     OfferedLane = Empty
  107.     Customer = Empty
  108.     Equipment = Empty
  109.     Ratee = Empty
  110.     Profit = Empty
  111.     Carrier = Empty
  112.     CarrierEmail = Empty
  113.     CarrierPhone = Empty
  114.     InvoiceStatus = Empty
  115.     CustomerInvoiced = Empty
  116.     InvoiceAge = Empty
  117.     NewNumber = Empty
  118.    
  119.     SearchVal = Loads.Range("C" & j)
  120.  
  121.     IRow = Application.Match(SearchVal, Invoices.Range("G1:G" & InvoicesRow), 0)
  122.     DRow = Application.Match(SearchVal, Data.Range("D1:D" & DataRow), 0)
  123.  
  124.     If DRow = 0 Then 'Find DRow by looking in Data BP:BP (Assume it is Green Bay Packaging)
  125.        DBPRow = Application.Match(SearchVal, Data.Range("BP1:BP" & DataRow), 0)
  126.         If DBPRow = 0 Then
  127.            'MsgBox "Can't find Load # anywhere, press okay to search for next Load #"
  128.           If j < Loads.Cells(Rows.Count, Col_L).End(xlUp).Row + 1 Then
  129.                 GoTo NN
  130.             Else
  131.                 Exit For
  132.             End If
  133.         End If
  134.        DRow = Data.Range("D" & DBPRow)
  135.     End If
  136.  
  137.    
  138.     If IRow > 0 Then
  139.         'Status Link
  140.        StatusLink = "https://superior.logisticallytms.com/tms/#/3pl/orders/" & SearchVal
  141.         StatusText = Invoices.Range("W" & Row)
  142.        
  143.         If StatusText = "" Then
  144.             StatusText = "Go To Load"
  145.         End If
  146.        
  147.         'Ship Date
  148.        ShipDate = Format(CDate(Invoices.Range("K" & IRow)), "mm/dd/yyyy")
  149.  
  150.         'Offered Lane
  151.       OfferedLane = Invoices.Range("o" & Row) & " " & Invoices.Range("p" & IRow) & " to " & _
  152.             Invoices.Range("s" & Row) & " " & Invoices.Range("t" & IRow)
  153.         'Customer
  154.       Customer = Invoices.Range("B" & IRow)
  155.         'Rate
  156.       Ratee = Format(CLng(Invoices.Range("v" & IRow)), "###0.00")
  157.         'Carrier
  158.       Carrier = Invoices.Range("f" & IRow)
  159.         'Invoice Aging Days
  160.        If Customer = "Belmark, Inc." Then
  161.             InvoiceAge = "Belmark"
  162.         ElseIf CustomerInvoiced <> "YES" Then
  163.             InvoiceAge = CInt(Date - ShipDate) 'in Integers
  164.        End If
  165.            
  166.         If Customer = "Belmark, Inc." Then
  167.             InvoiceStatus = "Belmark"
  168.             CustomerInvoiced = "Belmark"
  169.         Else
  170.             InvoiceStatus = Invoices.Range("Y" & IRow)
  171.             CustomerInvoiced = Invoices.Range("A" & IRow)
  172.         End If
  173.     ElseIf DRow > 0 Then
  174.         'Status Link
  175.        StatusLink = "https://superior.logisticallytms.com/tms/#/3pl/orders/" & SearchVal
  176.         'look for status text in Data M:M (or Data L:L? not sure which one is the one you want)
  177.        StatusText = Data.Range("M" & DRow)
  178.         'Ship Date
  179.        ShipDate = Format(CDate(Data.Range("N" & DRow)), "mm/dd/yyyy")
  180.         'Offered Lane
  181.       OfferedLane = Data.Range("AR" & DRow) & " " & Data.Range("AT" & DRow) & " to " & _
  182.             Data.Range("BB" & DRow) & " " & Data.Range("BC" & DRow)
  183.         'Customer
  184.       Customer = Data.Range("AB" & DRow)
  185.         'Rate
  186.       Ratee = Format(CLng(Data.Range("AG" & DRow)), "###0.00")
  187.         'Carrier
  188.       Carrier = Data.Range("AD" & DRow)
  189.         'Invoice Aging Days
  190.        If Customer = "Belmark, Inc." Then
  191.             InvoiceAge = "Belmark"
  192.         ElseIf CustomerInvoiced <> "YES" Then
  193.             InvoiceAge = CInt(Date - CDate(Data.Range("N" & DRow))) 'in Integers
  194.        End If
  195.     End If
  196.    
  197.     'Equipment (returns whatever value is in Data CL:CL)
  198.    Equipment = Data.Range("CL" & DRow)
  199.        
  200.     'Profit
  201.    If Customer = "Belmark,Inc." Then
  202.         Profit = (Ratee * 1.06) * 1.03 + 1
  203.     Else
  204.         Profit = Data.Range("AK" & DRow)
  205.     End If
  206.        
  207.     'Search in Carriers sheet
  208.    CRow = Application.Match(Carrier, Carriers.Range("D1:D" & CarriersRow), 0)
  209.  
  210.     If CRow > 0 Then
  211.         'Carrier Email
  212.       CarrierEmail = Carriers.Range("M" & CRow)
  213.         'Carrier Phone Number
  214.       CarrierPhone = Carriers.Range("N" & CRow)
  215.     End If
  216.  
  217.  
  218.     'Write everything in the sheet
  219.    Loads.Hyperlinks.Add Anchor:=Loads.Range("B" & j), Address:=StatusLink, TextToDisplay:=StatusText
  220.     Loads.Range("D" & j) = ShipDate
  221.     Loads.Range("E" & j) = OfferedLane
  222.     Loads.Range("F" & j) = Customer
  223.     Loads.Range("G" & j) = Equipment
  224.     Loads.Range("H" & j) = Ratee
  225.     Loads.Range("I" & j) = Profit
  226.     Loads.Range("J" & j) = Carrier
  227.     Loads.Range("K" & j) = CarrierEmail
  228.     Loads.Range("L" & j) = CarrierPhone
  229.     Loads.Range("M" & j) = InvoiceStatus
  230.     Loads.Range("N" & j) = CustomerInvoiced
  231.     Loads.Range("O" & j) = InvoiceAge
  232.    
  233.     'Email
  234.    If StatusText = "Go To Load" Then
  235.         Loads.Range("A" & j) = ""
  236.     Else
  237.         If IsEmpty(ShipDate) = False And IsEmpty(OfferedLane) = False And IsEmpty(CarrierEmail) = False Then
  238.             Loads.Range("A" & j) = "Send Email"
  239.         End If
  240.     End If
  241.  
  242.  
  243. End If
  244. NN:
  245. Next j
  246.  
  247.  
  248. Worksheets("Quotes").Activate
  249.  
  250.  
  251. ' add formulas to do city / state lookup based on zip code entered.
  252.  
  253.  
  254. For i = Quotes.Cells(Rows.Count, Col_Q).End(xlUp).Row + 1 To Quotes.Cells(Rows.Count, Col_Q).End(xlUp).Row + 49
  255.  
  256. Quotes.Cells(i, 2).Formula = "=IF(D" & i & "<>"""",INDEX(Zips!$B:$B,MATCH($D" & i & ",Zips!$A:$A,0)),"""")"
  257.  
  258. Quotes.Cells(i, 3).Formula = "=IF(D" & i & "<>"""",INDEX(Zips!$C:$C,MATCH($D" & i & ",Zips!$A:$A,0)),"""")"
  259.  
  260. Quotes.Cells(i, 5).Formula = "=IF(G" & i & "<>"""",INDEX(Zips!$B:$B,MATCH($G" & i & ",Zips!$A:$A,0)),"""")"
  261.  
  262. Quotes.Cells(i, 6).Formula = "=IF(G" & i & "<>"""",INDEX(Zips!$C:$C,MATCH($G" & i & ",Zips!$A:$A,0)),"""")"
  263.  
  264.  
  265. Next i
  266.  
  267.  
  268.  
  269. TheWall
  270. Worksheets("Loads").Activate
  271. TheWall
  272.  
  273.  
  274. Application.ScreenUpdating = True
  275. SecondsElapsed = Round(Timer - StartTime, 2)
  276.  
  277.  
  278. 'A little error cleanup
  279. If Rows(Loads.Cells(Rows.Count, 2).End(xlUp).Row) = "to" Then
  280. Rows(Loads.Cells(Rows.Count, 2).End(xlUp).Row).Delete
  281. End If
  282. Quotes.UsedRange.Columns.AutoFit
  283. Quotes.Range("M:M").EntireColumn.Hidden = True
  284.  
  285. ' pop runtime
  286. MsgBox "Loads sheet updated successfully in " & SecondsElapsed & " seconds", vbInformation
  287.  
  288. On Error GoTo 0
  289.  
  290. 'Lock ranges to prevent users from accidentially deleting stuff
  291.  
  292. Loads.Range("A:O").Locked = True
  293. Loads.Range("P:P").Locked = False
  294. Loads.Protect "superior"
  295. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement