Hi everyone,
I am new here, just starting out with VBA and would like to get an advice from the experts here in this forum.
I am trying to manage stock inventory using excel sheet, which will be monitored by the "Alerting Limit" in column AF; "Safe" means our stock levels are good while "Critical" means we will need to purchase more of the stock.
What I would like to achieve is to be able to create a macro that can send an email(in outlook) for those stocks that are only listed as "CRITICAL" and send email to each unique email address in column C. As for those that have the same email address, additional rows are added to the same email draft in order to prevent sending 100 emails for 100 "critical" stock.
I have managed to generate the macro via referencing/copy-pasting from other posts but I am unable to pin-point what went wrong with the code. There are several rows that were added for "c@hotmail.com" even though the alerting limit was indicated as "SAFE". The only one that was correct was the "d@hotmail.com"(not shown below)
Heres my code
Appreciate your help!!
I am new here, just starting out with VBA and would like to get an advice from the experts here in this forum.
I am trying to manage stock inventory using excel sheet, which will be monitored by the "Alerting Limit" in column AF; "Safe" means our stock levels are good while "Critical" means we will need to purchase more of the stock.
What I would like to achieve is to be able to create a macro that can send an email(in outlook) for those stocks that are only listed as "CRITICAL" and send email to each unique email address in column C. As for those that have the same email address, additional rows are added to the same email draft in order to prevent sending 100 emails for 100 "critical" stock.
I have managed to generate the macro via referencing/copy-pasting from other posts but I am unable to pin-point what went wrong with the code. There are several rows that were added for "c@hotmail.com" even though the alerting limit was indicated as "SAFE". The only one that was correct was the "d@hotmail.com"(not shown below)
Heres my code
VBA Code:
Sub Macro12()
'Set email address as range for first loop to run down
Set Rng = Range(Range("J6"), Range("J" & Rows.Count).End(xlUp))
'Get a row count to clear column v at the end
x = Rng.Rows.Count
'Create the html table and header from the first row
tableHdr = "<table border=1><tr><th>" & Range("C5").Value & "</th>" _
& "<th>" & Range("M5").Value & "</th>" _
& "<th>" & "Quantity" & "</th>" _
'Check to see if column v = 'yes' and skip mail if it does
For Each cell In Rng
If ((cell.Value <> "") And (cell.Offset(0, 25).Value = "CRITICAL")) Then
If Not cell.Offset(0, 23).Value = "Quotation Requested" Then
NmeRow = cell.Row
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.createitem(0)
MailTo = cell.Value 'column J
MailSubject = "Quotation Request"
'Create MailBody table row for first row
MailBody = "<tr>" _
& "<td>" & cell.Offset(0, -7).Value & "</td>" _
& "<td>" & cell.Offset(0, 3).Value & "</td>" _
& "</tr>"
'Second loop checks the email addresses of all cells following the current cell in the first loop.
'Yes will be appended on any duplicate finds and another row added to the mailbody table
For Each dwn In Rng.Offset(NmeRow, 0)
If ((cell.Offset(0, 25).Value = "CRITICAL") And (dwn.Value = cell.Value)) Then
'Create additional table row for each extra row found"
AddRow = "<tr>" _
& "<td>" & dwn.Offset(0, -7).Value & "</td>" _
& "<td>" & dwn.Offset(0, 3).Value & "</td>" _
& "</tr>"
dwn.Offset(0, 23).Value = "Quotation Requested"
MailBody = MailBody & AddRow 'column A
Else
End If
' Clear additional table row variable ready for next
AddRow = ""
Next
With OutMail
.To = MailTo
.Subject = MailSubject
.HTMLBody = tableHdr & MailBody & "</table>"
.Display
'send
End With
cell.Offset(0, 23).Value = "Quotation Requested"
End If
End If
MailTo = ""
MailSubject = ""
MailBody = ""
'Clear 'Quotation Requested' from all appended cells in column v
Range("Aj6:Aj" & x).ClearContents
Next
End Sub
Appreciate your help!!