Send email when one or multiple cells change to specific name

djonik1234

New Member
Joined
Mar 30, 2022
Messages
29
Office Version
  1. 365
Platform
  1. Windows
I am trying to set up my inventory worksheet to automatically send emails. when items in the inventory get low. I found a code that I modified and everything work but the code is based on one specific cell "G13". Couple things that I am trying to change and achieve in that code.
1. Change the code to look for multiple cells in column H, I and J that have words typed in those cells and not numbers. Specifically send email when those cells in column H, I and J say "2 Tools Worth"
2. In the body of the email to include all caster type that have changed their status to "2 Tools Worth".

See attachments for reference and Here is my code.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 6 Then Exit Sub
If Not Application.Intersect(Range("G13"), Target) Is Nothing Then
If IsNumeric(Target.Value) And Target.Value = 5 Then
Call Mail_small_Text_Outlook
End If
End If
End Sub



Sub Mail_small_Text_Outlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

strbody = "This is automated email to inform you that inventory status for casters/fixtures described below has change to '2 Tools Worth':" & vbNewLine & vbNewLine & _
"MPB/STB: Docking Casters" & vbNewLine & _
"Confirm there are enough for tools that are on schedule to be moved out."

On Error Resume Next
With OutMail
.To = "email goes here"
.CC = ""
.BCC = ""
.Subject = "Inventory low on some Casters/Fixtures!"
.Body = strbody
.Attachments.Add ("My Attachment link")
'You can add a file like this
'.Attachments.Add ("C:\test.txt")
.Send 'or use .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 

Attachments

  • Worksheet.JPG
    Worksheet.JPG
    214.1 KB · Views: 34
OK little confused as to why you have the same formula in three columns all checking the same cell.?? If H I & J all have same formula they will all produce the same result so cant see the need to check all three in the code as unless missing something will all change at the same time.
But here you go

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count >= 6 Then Exit Sub
 
If Not Application.Intersect(Range("G:G"), target) Is Nothing Then
   If target.Offset(0, 1) = "2 Tools Worth" Or target.Offset(0, 1) = "1 Tool Worth" Or target.Offset(0, 1) = "Limited Stock" Or target.Offset(0, 1) = "Out of Stock" Then
     Cells(target.row, 11) = "Y"
   ElseIf target.Offset(0, 2) = "2 Tools Worth" Or target.Offset(0, 2) = "1 Tool Worth" Or target.Offset(0, 2) = "Limited Stock" Or target.Offset(0, 2) = "Out of Stock" Then
     Cells(target.row, 12) = "Y"
   ElseIf target.Offset(0, 3) = "2 Tools Worth" Or target.Offset(0, 3) = "1 Tool Worth" Or target.Offset(0, 3) = "Limited Stock" Or target.Offset(0, 3) = "Out of Stock" Then
     Cells(target.row, 13) = "Y"
   Else
     End
   End If
 
      Dim OutApp As Object
      Dim OutMail As Object
      Dim strbody As String
    
      Set OutApp = CreateObject("Outlook.Application")
      Set OutMail = OutApp.CreateItem(0)
    
      strbody = "This is automated email to inform you that inventory status for " & Cells(Target.Row, 2) & " has change to '2 Tools Worth' or less." & vbNewLine & vbNewLine & _
                "Confirm there are enough " & Cells(Target.Row, 2) & " for tools that are on schedule to be moved out."

      On Error Resume Next
       
         With OutMail
           .To = "[EMAIL]djon.podvalnii@us.tel.com[/EMAIL]"
           .cc = ""
           .Bcc = ""
           .Importance = 2
           .Subject = "Low Casters/Fixture Inventory!"
           .Body = strbody
          ' .Attachments.Add ("My Attachment link")
          .Send '.Display
         End With
      On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End If
End Sub
 
Upvote 0
Solution

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
OK little confused as to why you have the same formula in three columns all checking the same cell.?? If H I & J all have same formula they will all produce the same result so cant see the need to check all three in the code as unless missing something will all change at the same time.
But here you go

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count >= 6 Then Exit Sub
 
If Not Application.Intersect(Range("G:G"), target) Is Nothing Then
   If target.Offset(0, 1) = "2 Tools Worth" Or target.Offset(0, 1) = "1 Tool Worth" Or target.Offset(0, 1) = "Limited Stock" Or target.Offset(0, 1) = "Out of Stock" Then
     Cells(target.row, 11) = "Y"
   ElseIf target.Offset(0, 2) = "2 Tools Worth" Or target.Offset(0, 2) = "1 Tool Worth" Or target.Offset(0, 2) = "Limited Stock" Or target.Offset(0, 2) = "Out of Stock" Then
     Cells(target.row, 12) = "Y"
   ElseIf target.Offset(0, 3) = "2 Tools Worth" Or target.Offset(0, 3) = "1 Tool Worth" Or target.Offset(0, 3) = "Limited Stock" Or target.Offset(0, 3) = "Out of Stock" Then
     Cells(target.row, 13) = "Y"
   Else
     End
   End If
 
      Dim OutApp As Object
      Dim OutMail As Object
      Dim strbody As String
   
      Set OutApp = CreateObject("Outlook.Application")
      Set OutMail = OutApp.CreateItem(0)
   
      strbody = "This is automated email to inform you that inventory status for " & Cells(Target.Row, 2) & " has change to '2 Tools Worth' or less." & vbNewLine & vbNewLine & _
                "Confirm there are enough " & Cells(Target.Row, 2) & " for tools that are on schedule to be moved out."

      On Error Resume Next
      
         With OutMail
           .To = "[EMAIL]djon.podvalnii@us.tel.com[/EMAIL]"
           .cc = ""
           .Bcc = ""
           .Importance = 2
           .Subject = "Low Casters/Fixture Inventory!"
           .Body = strbody
          ' .Attachments.Add ("My Attachment link")
          .Send '.Display
         End With
      On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End If
End Sub

I have the same formula checking the same cell because those 3 Columns (H, I and J) represent 3 different tool configurations. Half of inventory requires same amount of casters/fixtures for all 3 types and therefore formulas are 100% same thought half of inventory but the other half of the inventory uses the same formula but the amount of the casters/fixtures required for each tool type in different. Some require more, some require less and therefore I set it up to 3 different columns.

In your past post I don't see the code that would look for that Y in targeted row 11, 12, and 13. Right now the Y files in but the code never stops sending emails after Y was registered in columns 11, 12, and 13 for the first time.
 
Upvote 0
just place an AND statement after each of the IFS. Obviously change the 11 to 12 and 13 as required

eg
VBA Code:
If target.Offset(0, 1) = "2 Tools Worth" Or target.Offset(0, 1) = "1 Tool Worth" Or target.Offset(0, 1) = "Limited Stock" Or target.Offset(0, 1) = "Out of Stock" AND cells(target.row,11)="" Then
     Cells(target.row, 11) = "Y"
 
Upvote 0
just place an AND statement after each of the IFS. Obviously change the 11 to 12 and 13 as required

eg
VBA Code:
If target.Offset(0, 1) = "2 Tools Worth" Or target.Offset(0, 1) = "1 Tool Worth" Or target.Offset(0, 1) = "Limited Stock" Or target.Offset(0, 1) = "Out of Stock" AND cells(target.row,11)="" Then
     Cells(target.row, 11) = "Y"
If Not Application.Intersect(Range("G:G"), Target) Is Nothing Then
If Target.Offset(0, 1) = "2 Tools Worth" Or Target.Offset(0, 1) = "1 Tool Worth" Or Target.Offset(0, 1) = "Limited Stock" Or Target.Offset(0, 1) = "Out of Stock" And Cells(Target.Row, 14) <> "Y" Then
Cells(Target.Row, 14) = "Y"
ElseIf Target.Offset(0, 2) = "2 Tools Worth" Or Target.Offset(0, 2) = "1 Tool Worth" Or Target.Offset(0, 2) = "Limited Stock" Or Target.Offset(0, 2) = "Out of Stock" And Cells(Target.Row, 15) <> "Y" Then
Cells(Target.Row, 15) = "Y"
ElseIf Target.Offset(0, 3) = "2 Tools Worth" Or Target.Offset(0, 3) = "1 Tool Worth" Or Target.Offset(0, 3) = "Limited Stock" Or Target.Offset(0, 3) = "Out of Stock" And Cells(Target.Row, 16) <> "Y" Then
Cells(Target.Row, 16) = "Y"
Else
End
End If

Thank you. It still continues sending emails after Y is registered in a corresponding column. Also, with ElseIf statements it does not place Y's for all the "2 Tools Worth" in Columns H, I, J. I Prioritizes only the first one that has "2 Tools Worth" statements.

I also don't know what to construct the code so it removes Y's after the inventory got back to 3 Tools Worth so that way the email will start sending again if that same caster/fixture will get to 2 Tools Worth mark. Thank you for your patience with me.
 
Upvote 0
If Not Application.Intersect(Range("G:G"), Target) Is Nothing Then
If Target.Offset(0, 1) = "2 Tools Worth" Or Target.Offset(0, 1) = "1 Tool Worth" Or Target.Offset(0, 1) = "Limited Stock" Or Target.Offset(0, 1) = "Out of Stock" And Cells(Target.Row, 14) <> "Y" Then
Cells(Target.Row, 14) = "Y"
ElseIf Target.Offset(0, 2) = "2 Tools Worth" Or Target.Offset(0, 2) = "1 Tool Worth" Or Target.Offset(0, 2) = "Limited Stock" Or Target.Offset(0, 2) = "Out of Stock" And Cells(Target.Row, 15) <> "Y" Then
Cells(Target.Row, 15) = "Y"
ElseIf Target.Offset(0, 3) = "2 Tools Worth" Or Target.Offset(0, 3) = "1 Tool Worth" Or Target.Offset(0, 3) = "Limited Stock" Or Target.Offset(0, 3) = "Out of Stock" And Cells(Target.Row, 16) <> "Y" Then
Cells(Target.Row, 16) = "Y"
Else
End
End If

Thank you. It still continues sending emails after Y is registered in a corresponding column. Also, with ElseIf statements it does not place Y's for all the "2 Tools Worth" in Columns H, I, J. I Prioritizes only the first one that has "2 Tools Worth" statements.

I also don't know what to construct the code so it removes Y's after the inventory got back to 3 Tools Worth so that way the email will start sending again if that same caster/fixture will get to 2 Tools Worth mark. Thank you for your patience with me.

Pasting the correct code this time.


If Not Application.Intersect(Range("G:G"), Target) Is Nothing Then
If Target.Offset(0, 1) = "2 Tools Worth" Or Target.Offset(0, 1) = "1 Tool Worth" Or Target.Offset(0, 1) = "Limited Stock" Or Target.Offset(0, 1) = "Out of Stock" And Cells(Target.Row, 14) = "" Then
Cells(Target.Row, 14) = "Y"
ElseIf Target.Offset(0, 2) = "2 Tools Worth" Or Target.Offset(0, 2) = "1 Tool Worth" Or Target.Offset(0, 2) = "Limited Stock" Or Target.Offset(0, 2) = "Out of Stock" And Cells(Target.Row, 15) = "" Then
Cells(Target.Row, 15) = "Y"
ElseIf Target.Offset(0, 3) = "2 Tools Worth" Or Target.Offset(0, 3) = "1 Tool Worth" Or Target.Offset(0, 3) = "Limited Stock" Or Target.Offset(0, 3) = "Out of Stock" And Cells(Target.Row, 16) = "" Then
Cells(Target.Row, 16) = "Y"
Else
End
End If
 
Upvote 0
It places the Y individually with the IF statement because that is what you asked for. If you wanted them all done at once then like I said at the very beginning there is no need to check all three columns. given you have the same formula in H I J all checking column G and you say there is no variation (only different within different rows) there would only be a need to check one column.

It would also be helpful if you set out all your requirements in one go. Each time I have provided a code that works you add another requirement.
If you require further help then please list your FULL requirements, attach a copy of the workbook OR list all formulas for one row in H I J and confirm if within the same row they all change to 2 tools worth at the same time.
 
Upvote 0
I am sorry for misunderstanding and miscommunication that is happening on my part.

There is variation in the formula in maybe half of the rows for Column H, I, and J but not all of them.
For Example:
Row 43 Column H formula: =IF(G43=0,"Out of Stock",IF(G43<=5,"Limited Stock",IF(AND(G43<12,G43>=6),"1 Tool Worth",IF(AND(G43<18,G43>=12),"2 Tools Worth",IF(AND(G43<24,G43>=18),"3 Tools Worth",IF(AND(G43<30,G43>=24),"4 Tools Worth","5 or more"))))))

Row 43 Column I formula: =IF(G43=0,"Out of Stock",IF(G43<=6,"Limited Stock",IF(AND(G43<14,G43>=7),"1 Tool Worth",IF(AND(G43<21,G43>=14),"2 Tools Worth",IF(AND(G43<28,G43>=21),"3 Tools Worth",IF(AND(G43<35,G43>=28),"4 Tools Worth","5 or more"))))))

Row 43 Column I formula: =IF(G43=0,"Out of Stock",IF(G43<=3,"Limited Stock",IF(AND(G43<8,G43>=4),"1 Tool Worth",IF(AND(G43<12,G43>=8),"2 Tools Worth",IF(AND(G43<16,G43>=12),"3 Tools Worth",IF(AND(G43<20,G43>=16),"4 Tools Worth","5 or more"))))))

Currently H is 3 Tools Worth, I is 2 Tools Worth, and J 4 Tools Worth. (see attached image for reference. But on some rows column H, I and J all have 2 tools worth. (using same exact formula. I am not going to have anymore requests other than just helping get this part done so it functions.
 

Attachments

  • Capture.JPG
    Capture.JPG
    17.4 KB · Views: 4
Upvote 0
OK so had another go based on all your requirements. Make sure you copy exactly.
VBA Code:
Option Explicit
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Long

Application.EnableEvents = False

If Target.Cells.count < 6 Then
 
   If Not Application.Intersect(Range("G:G"), Target) Is Nothing Then
      
      If left(Target.Offset(0, 1), 1) <= 2 Or Target.Offset(0, 1) Like "*Stock*" Then
         If Cells(Target.row, 11) = "" Then
            x = x + 1
            Cells(Target.row, 11) = "Y"
         Else
            x = x + 0
         End If
      Else
         Cells(Target.row, 11) = ""
      End If
      
      If left(Target.Offset(0, 2), 1) <= 2 Or Target.Offset(0, 1) Like "*Stock*" Then
         If Cells(Target.row, 12) = "" Then
            x = x + 1
            Cells(Target.row, 12) = "Y"
         Else
            x = x + 0
         End If
      Else
         Cells(Target.row, 12) = ""
      End If
      
      If left(Target.Offset(0, 3), 1) <= 2 Or Target.Offset(0, 1) Like "*Stock*" Then
         If Cells(Target.row, 13) = "" Then
            x = x + 1
            Cells(Target.row, 13) = "Y"
         Else
            x = x + 0
         End If
      Else
         Cells(Target.row, 13) = ""
      End If
         
      If x > 0 Then
   
            Dim OutApp As Object
            Dim OutMail As Object
            Dim strbody As String
          
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
          
            strbody = "This is automated email to inform you that inventory status for " & Cells(Target.row, 2) & " has change to '2 Tools Worth' or less." & vbNewLine & vbNewLine & _
                      "Confirm there are enough " & Cells(Target.row, 2) & " for tools that are on schedule to be moved out."
      
            On Error Resume Next
             
               With OutMail
                 .To = "[EMAIL]djon.podvalnii@us.tel.com[/EMAIL]"
                 .cc = ""
                 .Bcc = ""
                 .Importance = 2
                 .subject = "Low Casters/Fixture Inventory!"
                 .Body = strbody
                ' .Attachments.Add ("My Attachment link")
                .Send '.Display
               End With
            On Error GoTo 0
      
            Set OutMail = Nothing
            Set OutApp = Nothing
      Else
      End If

   End If
Else
End If
Application.EnableEvents = False
End Sub
 
Upvote 0
OK so had another go based on all your requirements. Make sure you copy exactly.
VBA Code:
Option Explicit
Option Compare Text
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Long

Application.EnableEvents = False

If Target.Cells.count < 6 Then
 
   If Not Application.Intersect(Range("G:G"), Target) Is Nothing Then
     
      If left(Target.Offset(0, 1), 1) <= 2 Or Target.Offset(0, 1) Like "*Stock*" Then
         If Cells(Target.row, 11) = "" Then
            x = x + 1
            Cells(Target.row, 11) = "Y"
         Else
            x = x + 0
         End If
      Else
         Cells(Target.row, 11) = ""
      End If
     
      If left(Target.Offset(0, 2), 1) <= 2 Or Target.Offset(0, 1) Like "*Stock*" Then
         If Cells(Target.row, 12) = "" Then
            x = x + 1
            Cells(Target.row, 12) = "Y"
         Else
            x = x + 0
         End If
      Else
         Cells(Target.row, 12) = ""
      End If
     
      If left(Target.Offset(0, 3), 1) <= 2 Or Target.Offset(0, 1) Like "*Stock*" Then
         If Cells(Target.row, 13) = "" Then
            x = x + 1
            Cells(Target.row, 13) = "Y"
         Else
            x = x + 0
         End If
      Else
         Cells(Target.row, 13) = ""
      End If
        
      If x > 0 Then
  
            Dim OutApp As Object
            Dim OutMail As Object
            Dim strbody As String
         
            Set OutApp = CreateObject("Outlook.Application")
            Set OutMail = OutApp.CreateItem(0)
         
            strbody = "This is automated email to inform you that inventory status for " & Cells(Target.row, 2) & " has change to '2 Tools Worth' or less." & vbNewLine & vbNewLine & _
                      "Confirm there are enough " & Cells(Target.row, 2) & " for tools that are on schedule to be moved out."
     
            On Error Resume Next
            
               With OutMail
                 .To = "[EMAIL]djon.podvalnii@us.tel.com[/EMAIL]"
                 .cc = ""
                 .Bcc = ""
                 .Importance = 2
                 .subject = "Low Casters/Fixture Inventory!"
                 .Body = strbody
                ' .Attachments.Add ("My Attachment link")
                .Send '.Display
               End With
            On Error GoTo 0
     
            Set OutMail = Nothing
            Set OutApp = Nothing
      Else
      End If

   End If
Else
End If
Application.EnableEvents = False
End Sub
I copied the code exactly but no emails are sent when any rows for H, I, or J changes to "2 Tools Worth" and Y's don't generate. I would apricate if you could help me troubleshoot this.
 

Attachments

  • Testing.JPG
    Testing.JPG
    96.7 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,214,829
Messages
6,121,827
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top