Note: The other languages of the website are Google-translated. Back to English
Logga in  \/ 
x
or
x
Registrera  \/ 
x

or

Hur skickar man automatiskt e-post baserat på cellvärde i Excel?

Antar att du vill skicka ett e-postmeddelande via Outlook till en viss mottagare baserat på ett angivet cellvärde i Excel. Till exempel, när värdet på cell D7 i ett kalkylblad är större än 200, skapas ett e-postmeddelande automatiskt. Den här artikeln introducerar en VBA-metod för dig att snabbt lösa problemet.

Skicka automatiskt e-post baserat på cellvärde med VBA-kod


Skicka automatiskt e-post baserat på cellvärde med VBA-kod

Gör så här för att skicka ett e-postmeddelande baserat på cellvärde i Excel.

1. I kalkylbladet måste du skicka e-post baserat på dess cellvärde (här står cellen D7), högerklicka på arkfliken och välj Visa kod från snabbmenyn. Se skärmdump:

2. I pop-up Microsoft Visual Basic för applikationer kopiera och klistra in nedanstående VBA-kod i arkfönstret.

VBA-kod: Skicka e-post via Outlook baserat på cellvärde i Excel

Dim xRg As Range
'Update by Extendoffice 2018/3/7
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Target.Cells.Count > 1 Then Exit Sub
  Set xRg = Intersect(Range("D7"), Target)
    If xRg Is Nothing Then Exit Sub
    If IsNumeric(Target.Value) And Target.Value > 200 Then
        Call Mail_small_Text_Outlook
    End If
End Sub
Sub Mail_small_Text_Outlook()
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Hi there" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2"
    On Error Resume Next
    With xOutMail
        .To = "Email Address"
        .CC = ""
        .BCC = ""
        .Subject = "send by cell value test"
        .Body = xMailBody
        .Display   'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
End Sub

Anmärkningar:

1. I VBA-koden, D7 och värde> 200 är det cell- och cellvärde du skickar e-post baserat på.

2. Ändra e-postadressen efter behov xMailBody raden i koden.

3. Ersätt e-postadressen med mottagarens e-postadress i raden .To = "E-postadress".

4. Och ange mottagare för kopior och kopior som du behöver i .CC = "" och Bcc = "" sektioner.

5. Ändra äntligen e-postens ämne i raden .Subject = "skicka med cellvärde test".

3. tryck på andra + Q knapparna tillsammans för att stänga Microsoft Visual Basic för applikationer fönster.

Från och med nu, när värdet du anger i cell D7 är större än 200, skapas ett e-postmeddelande med angivna mottagare och kropp automatiskt i Outlook. Du kan klicka på Skicka knappen för att skicka detta e-postmeddelande. Se skärmdump:

Anmärkningar:

1. VBA-koden fungerar bara när du använder Outlook som ditt e-postprogram.

2. Om de inmatade uppgifterna i cell D7 är ett textvärde kommer e-postfönstret att visas också.


Skicka enkelt e-post via Outlook baserat på fält i den skapade e-postlistan i Excel:

Du har nu möjlighet Skicka e-mail nytta av Kutools för Excel hjälper användare att skicka e-post via Outlook baserat på skapad e-postlista i Excel.
Ladda ner och prova det nu! (30-dagars gratis spår)


Relaterade artiklar:


De bästa Office-produktivitetsverktygen

Kutools för Excel löser de flesta av dina problem och ökar din produktivitet med 80%

  • återanvändning: Sätt snabbt i komplexa formler, diagram och allt som du har använt tidigare; Kryptera celler med lösenord; Skapa e-postlista och skicka e-post ...
  • Super Formula Bar (enkelt redigera flera rader med text och formel); Läslayout (enkelt läsa och redigera ett stort antal celler); Klistra in i filtrerat intervall...
  • Sammanfoga celler / rader / kolumner utan att förlora data; Delat cellinnehåll; Kombinera duplicerade rader / kolumner... Förhindra duplicerade celler; Jämför intervall...
  • Välj Duplicera eller Unikt Rader; Välj tomma rader (alla celler är tomma); Super Find och Fuzzy Find i många arbetsböcker; Slumpmässigt val ...
  • Exakt kopia Flera celler utan att ändra formelreferens; Skapa referenser automatiskt till flera ark; Sätt in kulor, Kryssrutor och mer ...
  • Extrahera text, Lägg till text, ta bort efter position, Ta bort mellanslag; Skapa och skriva ut personsökningstalsatser; Konvertera mellan celler innehåll och kommentarer...
  • Superfilter (spara och tillämpa filterscheman på andra ark); Avancerad sortering efter månad / vecka / dag, frekvens och mer; Specialfilter av fet, kursiv ...
  • Kombinera arbetsböcker och arbetsblad; Sammanfoga tabeller baserat på nyckelkolumner; Dela data i flera ark; Batchkonvertera xls, xlsx och PDF...
  • Mer än 300 kraftfulla funktioner. Stöder Office / Excel 2007-2019 och 365. Stöder alla språk. Enkel distribution i ditt företag eller organisation. Fullständiga funktioner 30-dagars gratis provperiod. 60-dagars pengarna tillbaka-garanti.
kte-flik 201905

Fliken Office ger ett flikgränssnitt till Office och gör ditt arbete mycket enklare

  • Aktivera flikredigering och läsning i Word, Excel, PowerPoint, Publisher, Access, Visio och Project.
  • Öppna och skapa flera dokument i nya flikar i samma fönster, snarare än i nya fönster.
  • Ökar din produktivitet med 50%och minskar hundratals musklick för dig varje dag!
officetab botten
Say something here...
symbols left.
You are guest
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Moorhen · 3 years ago
    Hi there,


    I currently having a little trouble this the coding (new to this - may have bitten off more than I can chew)


    I currently have a spreadsheet with the following that I need help to automate and send email for fault that are at our properties for our business


    I currently need a code that will do use the following data:


    1) An address and the issue ( 2 "general" cells that have been merged via ((In cell D1)) " = =CONCAT(B1," "C1,) "
    The address in B1 will allways be same (more or less)
    Whilst C1 will always be changing dependant on the fault at the property.


    2) An email to be sent by the same email adress, ( can I use $E$1 or I have to use E1 - E1 . for example) or can I just Input " TheEmailAdress@.co.uk" in the line of code


    3) The email body to be populated in the similar way to point 1) ...... ((In cell F1)) " =CONCAT(G1," ",H1)
    These will be changing constantly as they represent the company (G1) and what they are doing , fixing, quoting ect (H1)

    4) The trigger to send the email off, I would be the number 7 , the sheet gets updated daily (7 days in a week)
    as such I need the trigger to send the email on day 7, but no constantly like on day 8, 9 , 10+ ect. and not before such as 1-6, this would be in A4 : A 100+ (as we are constantly expanding


    4) I've used small snippets from other users who mentioned about using a list for the trigger to send the email, but not sure was 100 % it was correct, but i'd need it to scan though all Collum A.... A4: A100
    and if there are 47 cells that contain only " 7 " then 47 Emails will be sent


    Thank you ever so much for reading and I hope you can help :)
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear martyn,
      Sorry can't help with this.
      You can post your question in our forum: https://www.extendoffice.com/forum.html to get more Excel supports from our technical staff.
      Thank you for your comment.

      Best Regards,
      Crystal
  • To post as a guest, your comment is unpublished.
    MC · 3 years ago
    Hi

    Thank you so much for posting this VBA Code and instructions. When I found it I felt like I had won the lotto. However I am stuck on something so I'm hoping you can help (I'm new to VBA, only have very basic understanding).

    I've copied the code and changed the cell and cell value to pick from a range if a criteria is met. I have tried and tested and it works and I received an email to outlook based on the criteria.

    1) However, I cannot seem to figure out how to get the VBA code to run automatically when I open up the excel worksheet, rather than having to click on the VBA application and select run. Could you advise if there is an additional prompt to type into the VBA code above that will do this or does it have to be done separately.

    2) Also is there a way to get the VBA code to send a mail to a person if the due date is yes for a certain item as shown in example below.
    email hidden column
    Name

    Procedure
    Procedure no.1 due date yes
    Procedure no. 2 due date no

    I would have numerous people in the spreadsheet (going across horizontally in a row) and 'Yes' could be highlighted for various overdue procedures (listed vertically in column A. Is there a way to create a VBA code that runs for something like this - if 'Yes' for 'Person 1', then email 'person 1' with 'procedure no #' (or numbers) and due date(s). Being able to list in the email all the procedures and their subsequent due dates.

    I wouldn't mind if I had to set a separate VBA code for each person as long as it sent a mail of all the documents overdue for that person and the due dates.

    Hoping you can help
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear Ann,
      Please try the below VBA code. Thank you for your comment.

      Sub Mail_small_Text_Outlook()
      Dim xRg As Range
      Dim xCell As Range
      Dim I As Long
      Dim xRows As Long
      Dim xCols As Long
      Dim xVal As String
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      On Error Resume Next
      Set xRg = Application.InputBox("Select the range contains the cell value you will send emails based on:", "Kutools for Excel", Selection.Address, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xRows = xRg.Rows.Count
      xCols = xRg.Columns.Count
      For I = 1 To xRows
      Set xCell = xRg(I, xCols)
      If xCell.Value = "Yes" Then
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is your information: " & vbNewLine & xCell.Offset(0, -1).Text & vbNewLine & xCell.Offset(0, -2).Text
      With xOutMail
      .To = xCell.Offset(0, -4).Text
      .Subject = "send by cell value test"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      On Error GoTo 0
      Set xOutMail = Nothing
      Set xOutApp = Nothing
      End If
      Next
      End Sub
      • To post as a guest, your comment is unpublished.
        Jacob · 3 years ago
        Where exactly do we insert this code?
        • To post as a guest, your comment is unpublished.
          crystal · 3 years ago
          Good day,
          You need to place the code into the worksheet's code window.
          Open the Microsoft Visual Basic for Applications window, double click the sheet name in the left pane to open the code editor.
      • To post as a guest, your comment is unpublished.
        Jermaine · 3 years ago
        Crystal,

        This replaces the following code:

        Sub email()

        Dim xRg As Range

        Dim xRgEach As Range

        Dim xEmail_Subject, xEmail_Send_Form,;etc.
  • To post as a guest, your comment is unpublished.
    hanizah223@gmail.com · 3 years ago
    how to stop code from running ie don't prompt the email when condition is not met?

    even when D7 < 200, I still get prompted the email.
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Good Day,
      The code is updated in the post with the problem solved. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    Savy · 3 years ago
    How can you add Multiple Range to "Set xRg = Range("D7")". I want to edit it and add Range("D7:F7"). However i am getting an error of Run Time Error 13, Type Mismatch and it is taking me to If xRg = Target And Target.Value > 2 Then.


    How can i solve this proble?
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Good Day,
      Please try below VBA code to solve the problem.

      Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Cells.Count > 1 Then Exit Sub
      If (Not Intersect(Target, Range("D7:F7")) Is Nothing) And (Target.Value > 200) Then
      Call Mail_small_Text_Outlook
      End If
      End Sub
      Sub Mail_small_Text_Outlook()
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is line 1" & vbNewLine & _
      "This is line 2"
      On Error Resume Next
      With xOutMail
      .To = "Your recipient's email address"
      .CC = ""
      .BCC = ""
      .Subject = "send by cell value test"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      On Error GoTo 0
      Set xOutMail = Nothing
      Set xOutApp = Nothing
      End Sub
      • To post as a guest, your comment is unpublished.
        Nitol · 3 years ago
        It is not working for me as the value in D7 is a result of a formual. What if cell D7 contains a formula, e.g. D7 =2*120? It still meets the condition but nothing is happening. Please help
      • To post as a guest, your comment is unpublished.
        Savy · 3 years ago
        worked perfectly fine.. Thank you..:):)
  • To post as a guest, your comment is unpublished.
    Doug · 3 years ago
    How can I edit the code to send an email based on a date in the cell. For example, I need a document reviewed every 15 months and I want to kick out an email at 12 months to an email address saying the document needs to be reviewed. I've got it now to auto-send an email by changing .Display to .Send and it works great as written, but what do I need to change to use a date function instead of a whole number??
  • To post as a guest, your comment is unpublished.
    New2Excel · 3 years ago
    Hello what code would I use if I am trying to send an email to a manager that has a list of the fruit that has a quantity > 200 once per month (based on your example) or expires soon( based on dates)
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Good Day
      May be the method in this article "How to send email if due date has been met in Excel?" can help you.
      Please follow this link: https://www.extendoffice.com/documents/excel/4664-excel-send-email-if-due-date-has-been-met.html
  • To post as a guest, your comment is unpublished.
    vj.mayank@gmail.com · 3 years ago
    I am having trouble sending mail through outlook. I receive the error saying "A program is trying to send an email on your behalf. If it is unexpected, please deny and verify your anti-virus software is up to date"
    Please help as I am not able to automate it.
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Sorry mayank,
      The code works well in my case. It seems that something about "send on behalf" function is configured in your Outlook. Pease check for it.
  • To post as a guest, your comment is unpublished.
    Dhruv · 3 years ago
    I have a list of email addresses already in an excel file, how can I modify the code to automatically choose the email address of the person if his cell D7 is >200?
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Good Day,
      The following VBA code can help you solve the problem. Please place the VBA script into your worksheet module. When value in the specified cell meet the condition, a Kutools for Excel dialog box will pop up, please select the cells which contain the recipients' email addresses and then click the OK button. Then emails with specified recipients are opening. Please send them as you need.

      Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Cells.Count > 1 Then Exit Sub
      Set xRg = Range("D7")
      If xRg = Target And Target.Value > 200 Then
      Call Mail_small_Text_Outlook
      End If
      End Sub
      Sub Mail_small_Text_Outlook()
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Dim xRgMsg As Range
      Dim xCell As Range
      Set xRgMsg = Application.InputBox("Please select the address cells:", "Kutools for Excel", , , , , , 8)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is line 1" & vbNewLine & _
      "This is line 2"
      On Error Resume Next
      For Each xCell In xRgMsg
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      With xOutMail
      .To = xCell.Value
      .CC = ""
      .BCC = ""
      .Subject = "send by cell value test"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      xOutApp = Nothing
      xOutMail = Nothing
      Next
      On Error GoTo 0
      End Sub
  • To post as a guest, your comment is unpublished.
    fdh1201 · 3 years ago
    How could I change this code for sending student grades to parents. Where if column A is the grade and Column B is the parent email. I want to populate an email for each student with an F as a grade.
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear Frank,
      The below VBA code can help you solve the problem. Thank you.

      Sub Mail_small_Text_Outlook()
      Dim xRg As Range
      Dim I As Long
      Dim xRows As Long
      Dim xVal As String
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      On Error Resume Next
      Set xRg = Application.InputBox("Please select grade column and the email column (two columns)", "Kutools for Excel", Selection.Address, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xRows = xRg.Rows.Count
      Set xRg = xRg(2)
      For I = 1 To xRows
      xVal = xRg.Offset(I, -1).Text
      If xVal = "F" Then
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is your child's grade " & xRg.Offset(I, -1).Text
      With xOutMail
      .to = xRg.Offset(I, 0).Text
      .Subject = "send by cell value test"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      On Error GoTo 0
      Set xOutMail = Nothing
      Set xOutApp = Nothing
      End If
      Next
      End Sub
  • To post as a guest, your comment is unpublished.
    Jose Manuel · 3 years ago
    Hello, how would you modify this code to check wether a group of cells have the string "No match" and send an email if it has.
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear Jose,
      Please try below VBA code. When running the code, a dialog box pops up, please select the range you will check for string, and click the OK button. if the string does not exist, you will get a prompt dialog box. If the string exists in the range, an email with specified recipient, subject and body will display.

      Sub SendEmail()
      Dim I As Long
      Dim J As Long
      Dim xRg As Range
      Dim xArr
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Dim xFlag As Boolean
      On Error Resume Next
      Set xRg = Application.InputBox("Please select range", "Kutools for Excel", Selection.Address, , , , , 8)
      If xRg Is Nothing Then Exit Sub
      xArr = xRg.Value
      xFlag = False
      For I = 1 To UBound(xArr)
      For J = 1 To UBound(xArr, 2)
      If xArr(I, J) = "No Match" Then
      xFlag = True
      End If
      Next
      Next
      If xFlag Then
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is line 1" & vbNewLine & _
      "This is line 2"
      With xOutMail
      .To = "Email address"
      .CC = ""
      .BCC = ""
      .Subject = "Match"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      Else
      MsgBox "Found No matched value", vbInformation, "KuTools for Excel"
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    basil · 3 years ago
    Hi I put the same script but it is not working please help me in the 1st part

    Dim xRg As Range

    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    Set xRg = Range("D7")
    If xRg = Target And Target.Value = 200 Then
    Call Mail_small_Text_Outlook
    End If

    End Sub
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear basil,
      Is there any warning when running the code?
  • To post as a guest, your comment is unpublished.
    Brahma · 3 years ago
    will it be sent automatically mail, without any manual interruption
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear Brahma,
      If you want to directly send the email without displaying, please replace the line ".Display" with ".Send" in the above VBA code.
  • To post as a guest, your comment is unpublished.
    Shawn Henry · 3 years ago
    Hello

    I am having trouble because Email recipient has to be added again and again one by one. Please guide if list of email recipients can be added to this function so the the function will select the email address from the list of email addresses provided or list upload and the function sends the email, already composed to the desired recipient.
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear Henry,
      The following VBA code can help you solve the problem. Please place the VBA script into your worksheet module. When value in the specified cell meet the condition, a Kutools for Excel dialog box will pop up, please select the cells which contain the recipients' email addresses and then click the OK button. Then emails with specified recipients are opening. Please send them as you need.

      Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Cells.Count > 1 Then Exit Sub
      Set xRg = Range("D7")
      If xRg = Target And Target.Value > 200 Then
      Call Mail_small_Text_Outlook
      End If
      End Sub
      Sub Mail_small_Text_Outlook()
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Dim xRgMsg As Range
      Dim xCell As Range
      Set xRgMsg = Application.InputBox("Please select the address cells:", "Kutools for Excel", , , , , , 8)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is line 1" & vbNewLine & _
      "This is line 2"
      On Error Resume Next
      For Each xCell In xRgMsg
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      With xOutMail
      .To = xCell.Value
      .CC = ""
      .BCC = ""
      .Subject = "send by cell value test"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      xOutApp = Nothing
      xOutMail = Nothing
      Next
      On Error GoTo 0
      End Sub
  • To post as a guest, your comment is unpublished.
    Jordan · 4 years ago
    I am having trouble getting this code to prompt if the value in the cell is changed indirectly. For example, if I have Sum equation changing this value automatically. When the equation runs and the value goes above the set value to prompt the email, it does not do so, unless I physically change the number myself. Is there a way to make the email prompt even if changed indirectly?
    • To post as a guest, your comment is unpublished.
      crystal · 3 years ago
      Dear Jordan,
      The following VBA code can help you solve the problem. Please don't forget to replace the "Email Address" with the recipient's email address in the code. Thank you.

      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRgPre As Range
      On Error Resume Next
      If Target.Cells.Count > 1 Then Exit Sub
      Set xRg = Range("D7")
      Set xRgPre = xRg.Precedents
      If xRg.Value > 200 Then
      If Target.Address = xRg.Address Then
      Call Mail_small_Text_Outlook
      ElseIf (Not xRgPre Is Nothing) And (Intersect(Target, xRgPre).Address = Target.Address) Then
      Call Mail_small_Text_Outlook
      End If
      End If
      End Sub
      Sub Mail_small_Text_Outlook()
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is line 1" & vbNewLine & _
      "This is line 2"
      On Error Resume Next
      With xOutMail
      .To = "Email Address"
      .CC = ""
      .BCC = ""
      .Subject = "send by cell value test"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      On Error GoTo 0
      Set xOutMail = Nothing
      Set xOutApp = Nothing
      End Sub
      • To post as a guest, your comment is unpublished.
        Jim · 2 years ago
        I used this code with the only change being I have applied it to an entire column [Set xRg = Range("D4:D13")]. Now the event triggers whenever a calculation is made regardless of whether the valve in Column D is below the target value. Any idea's why that is?


        Dim Xrg As Range
        Private Sub Worksheet_Change(ByVal Target As Range)
        Dim xRgPre As Range
        On Error Resume Next
        If Target.Cells.Count > 1 Then Exit Sub
        Set Xrg = Range("D4:D13")
        Set xRgPre = Xrg.Precedents
        If Xrg.Value < 1200 Then
        If Target.Address = Xrg.Address Then
        Call Mail_small_Text_Outlook
        ElseIf (Not xRgPre Is Nothing) And (Intersect(Target, xRgPre).Address = Target.Address) Then
        Call Mail_small_Text_Outlook
        End If
        End If
        End Sub

        Sub Mail_small_Text_Outlook()
        Dim xOutApp As Object
        Dim xOutMail As Object
        Dim xMailBody As String
        Set xOutApp = CreateObject("Outlook.Application")
        Set xOutMail = xOutApp.CreateItem(0)
        xMailBody = "Hi" & vbNewLine & _
        "Test vba" _
        & vbNewLine & _
        "Line 2."
        On Error Resume Next
        With xOutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Auto Email Test"
        .Body = xMailBody
        .Display
        End With
        On Error GoTo 0
        Set xOutMail = Nothing
        Set xOutApp = Nothing

        End Sub


        Thanks.
      • To post as a guest, your comment is unpublished.
        Herrera5238 · 3 years ago
        I've modified suggested code to try to make it work for my application.
        Changed xRg = Range("C2:C40") and If xRg.Value = -1.

        The issue that I'm having is anytime there is a change to any cell and as long as one of the cells in my range is = -1 it will call Mail_small_Text_Outlook.
        I'm trying to only call if any cell in my range is changed indirectly to -1.
        I was also wondering if and how it would be possible to have it meet two criteria.
        Like check range A and range B and if they meet criteria call function.

        Thanks in advance for the help. I'm new to all this but reading through this thread has me about 90% there.


        Private Sub Worksheet_Change(ByVal Target As Range)
        Dim xRgPre As Range
        On Error Resume Next
        If Target.Cells.Count > 1 Then Exit Sub
        Set xRg = Range("C2:C40")
        Set xRgPre = xRg.Precedents
        If xRg.Value = -1 Then
        If Target.Address = xRg.Address Then
        Call Mail_small_Text_Outlook
        ElseIf (Not xRgPre Is Nothing) And (Intersect(Target, xRgPre).Address = Target.Address) Then
        Call Mail_small_Text_Outlook
        End If
        End If
        End Sub
  • To post as a guest, your comment is unpublished.
    Debbie · 4 years ago
    How should the code be modified, to apply to an entire range of cells?
    • To post as a guest, your comment is unpublished.
      crystal · 4 years ago
      Dear Debbie,
      Please try below VBA code to solve the problem.

      Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Cells.Count > 1 Then Exit Sub
      If (Not Intersect(Target, Range("A1:D4")) Is Nothing) And (Target.Value > 200) Then
      Call Mail_small_Text_Outlook
      End If
      End Sub
      Sub Mail_small_Text_Outlook()
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      xMailBody = "Hi there" & vbNewLine & vbNewLine & _
      "This is line 1" & vbNewLine & _
      "This is line 2"
      On Error Resume Next
      With xOutMail
      .To = "Your recipient's email address"
      .CC = ""
      .BCC = ""
      .Subject = "send by cell value test"
      .Body = xMailBody
      .Display 'or use .Send
      End With
      On Error GoTo 0
      Set xOutMail = Nothing
      Set xOutApp = Nothing
      End Sub