Advertisement
Guest User

Bncc

a guest
Oct 17th, 2024
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. Sub BNCCMacro()
  2. Dim wsOrders As Worksheet
  3. Dim wsRegions As Worksheet
  4. Dim wsCarriers As Worksheet
  5. Dim lastRow As Long
  6. Dim i As Long
  7. Dim orderDict As Object
  8. Dim currentSCAC As String
  9. Dim currentRegionCode As String
  10. Dim carrierEmail As Variant
  11. Dim regionEmail As Variant
  12. Dim outlookApp As Object
  13. Dim outlookMail As Object
  14. Dim emailBody As String
  15. Dim emailSubject As String
  16. Dim key As Variant
  17. Dim splitKey() As String
  18. Dim dictKey As String
  19. Dim regionEmailDict As Object
  20.  
  21. ' Initialize worksheets
  22. Set wsOrders = ThisWorkbook.Sheets("Orders")
  23. Set wsRegions = ThisWorkbook.Sheets("Regions")
  24. Set wsCarriers = ThisWorkbook.Sheets("Carriers")
  25.  
  26. ' Create dictionaries
  27. Set orderDict = CreateObject("Scripting.Dictionary")
  28. Set regionEmailDict = CreateObject("Scripting.Dictionary")
  29.  
  30. ' Load region email data into dictionary
  31. lastRow = wsRegions.Cells(wsRegions.Rows.Count, 1).End(xlUp).Row
  32. For i = 2 To lastRow
  33. regionEmailDict(CStr(wsRegions.Cells(i, 1).Value)) = wsRegions.Cells(i, 2).Value
  34. Next i
  35.  
  36. ' Debug: Print all region emails
  37. Debug.Print "Region Code to Email Mapping:"
  38. For Each key In regionEmailDict.Keys
  39. Debug.Print "Region Code: " & key & ", Email: " & regionEmailDict(key)
  40. Next key
  41.  
  42. ' Read orders and group by SCAC and region email
  43. lastRow = wsOrders.Cells(wsOrders.Rows.Count, 1).End(xlUp).Row
  44. For i = 2 To lastRow
  45. currentSCAC = wsOrders.Cells(i, 3).Value
  46. currentRegionCode = CStr(wsOrders.Cells(i, 1).Value) ' Ensure region code is treated as string
  47.  
  48. ' Find carrier email
  49. carrierEmail = Application.VLookup(currentSCAC, wsCarriers.Range("A1:B300"), 2, False)
  50. If IsError(carrierEmail) Then
  51. carrierEmail = ""
  52. End If
  53.  
  54. ' Find region email
  55. If regionEmailDict.exists(currentRegionCode) Then
  56. regionEmail = regionEmailDict(currentRegionCode)
  57. Else
  58. regionEmail = ""
  59. End If
  60.  
  61. ' Debug information
  62. Debug.Print "Processing Order: " & wsOrders.Cells(i, 2).Value
  63. Debug.Print "SCAC: " & currentSCAC
  64. Debug.Print "Region Code: " & currentRegionCode
  65. Debug.Print "Carrier Email: " & carrierEmail
  66. Debug.Print "Region Email: " & regionEmail
  67.  
  68. If carrierEmail <> "" And regionEmail <> "" Then
  69. ' Create a key for SCAC and region email grouping
  70. dictKey = carrierEmail & "|" & regionEmail & "|" & currentSCAC
  71.  
  72. If Not orderDict.exists(dictKey) Then
  73. orderDict.Add dictKey, CreateObject("Scripting.Dictionary")
  74. End If
  75.  
  76. ' Append order to the list
  77. If Not orderDict(dictKey).exists(currentSCAC) Then
  78. orderDict(dictKey).Add currentSCAC, ""
  79. End If
  80.  
  81. orderDict(dictKey)(currentSCAC) = orderDict(dictKey)(currentSCAC) & "<li>" & wsOrders.Cells(i, 2).Value & "</li>"
  82. End If
  83. Next i
  84.  
  85. ' Set up Outlook application
  86. On Error Resume Next
  87. Set outlookApp = CreateObject("Outlook.Application")
  88. If outlookApp Is Nothing Then
  89. MsgBox "Outlook is not available. Please ensure Outlook is installed and accessible.", vbCritical
  90. Exit Sub
  91. End If
  92. On Error GoTo 0
  93.  
  94. ' Send emails based on grouped data
  95. For Each key In orderDict.Keys
  96. splitKey = Split(key, "|")
  97.  
  98. carrierEmail = splitKey(0)
  99. regionEmail = splitKey(1)
  100. currentSCAC = splitKey(2)
  101.  
  102. ' Build email body with HTML formatting
  103. emailBody = "<html><body>" & _
  104. "<p><strong>" & currentSCAC & " Team,</strong></p>" & _
  105. "<p>The following orders are showing billed but not picked up. Please advise status or if any issues:</p>" & _
  106. "<ul>" & orderDict(key)(currentSCAC) & "</ul>" & _
  107. "<p>Thank you,</p>" & _
  108. "<p><strong>Tyson</strong></p>" & _
  109. "</body></html>"
  110.  
  111. emailSubject = "Status Update Request"
  112.  
  113. ' Create and send email
  114. On Error GoTo ErrorHandler
  115. Set outlookMail = outlookApp.CreateItem(0)
  116. With outlookMail
  117. .To = carrierEmail
  118. .CC = regionEmail
  119. .Subject = emailSubject
  120. .HTMLBody = emailBody
  121. .Send ' Use .Send to send directly
  122. '.Display ' Use .Display to open the email before sending (commented out)
  123. End With
  124. On Error GoTo 0
  125.  
  126. ' Optional: Debug message
  127. Debug.Print "Sent email to: " & carrierEmail & " with CC: " & regionEmail
  128. Debug.Print "Subject: " & emailSubject
  129. Debug.Print "Body: " & emailBody
  130. Next key
  131.  
  132. MsgBox "Emails have been successfully grouped and sent.", vbInformation
  133. Exit Sub
  134.  
  135. ErrorHandler:
  136. MsgBox "An error occurred while sending the email. Please check the details and try again.", vbCritical
  137. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement