Jak automaticky odesílat e-maily na základě hodnoty buňky v aplikaci Excel?
Předpokládejme, že chcete odeslat e-mail prostřednictvím aplikace Outlook určitému příjemci na základě zadané hodnoty buňky v aplikaci Excel. Například když je hodnota buňky D7 v listu větší než 200, automaticky se vytvoří e-mail. Tento článek představuje metodu VBA pro rychlé vyřešení tohoto problému.
Automaticky odesílat e-maily na základě hodnoty buňky s kódem VBA
Automaticky odesílat e-maily na základě hodnoty buňky s kódem VBA
Chcete-li odeslat e-mail na základě hodnoty buňky v aplikaci Excel, postupujte takto.
1. V listu musíte odeslat e-mail na základě jeho hodnoty buňky (zde se uvádí buňka D7), klepněte pravým tlačítkem na kartu listu a vyberte Zobrazit kód z kontextové nabídky. Viz snímek obrazovky:
2. Ve vyskakovacím okně Microsoft Visual Basic pro aplikace zkopírujte a vložte níže uvedený kód VBA do okna kódu listu.
Kód VBA: Odesílejte e-maily prostřednictvím Outlooku na základě hodnoty buňky v aplikaci 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
Poznámky:
1. V kódu VBA, D7 a hodnota> 200 jsou hodnota buňky a buňky, na které budete posílat e-maily.
2. Změňte prosím tělo e-mailu tak, jak potřebujete xMailBody řádek v kódu.
3. Vyměňte e-mailovou adresu za řádek s e-mailovou adresou příjemce .To = "E-mailová adresa".
4. A zadejte příjemce Cc a Bcc, jak potřebujete .CC = “” a Skrytá kopie = “” sekce.
5. Nakonec změňte předmět e-mailu v řádku .Subject = "odeslat testem hodnoty buňky".
3. zmáčkni Další + Q klávesy společně zavřete Microsoft Visual Basic pro aplikace okno.
Od této chvíle, když je hodnota zadaná v buňce D7 větší než 200, bude v aplikaci Outlook automaticky vytvořen e-mail se zadanými příjemci a tělem. Můžete kliknout na Poslat tlačítko pro odeslání tohoto e-mailu. Viz screenshot:
Poznámky:
1. Kód VBA funguje, pouze když používáte Outlook jako svůj e-mailový program.
2. Pokud jsou zadaná data v buňce D7 textovou hodnotou, otevře se také e-mailové okno.
Snadno odesílejte e-maily prostřednictvím aplikace Outlook na základě polí vytvořeného seznamu adres v aplikaci Excel:
Společnost Odeslat e-maily užitečnost Kutools pro Excel pomáhá uživatelům odesílat e-maily prostřednictvím Outlooku na základě vytvořeného seznamu adres v aplikaci Excel.
Stáhněte si a vyzkoušejte hned! (30denní trasa zdarma)
Související články:
- Jak odeslat e-mail prostřednictvím aplikace Outlook, když je sešit uložen v aplikaci Excel?
- Jak odeslat e-mail, pokud je určitá buňka upravena v aplikaci Excel?
- Jak poslat e-mail, když kliknete na tlačítko v aplikaci Excel?
- Jak poslat e-mail, pokud byl v aplikaci Excel splněn termín splatnosti?
- Jak poslat e-mailem připomenutí nebo oznámení, pokud je sešit aktualizován v aplikaci Excel?
Nejlepší kancelářské nástroje produktivity
Kutools pro Excel řeší většinu vašich problémů a zvyšuje vaši produktivitu o 80%
- Opakované použití: Rychle vložte složité vzorce, grafy a cokoli, co jste dříve používali; Šifrovat buňky s heslem; Vytvořte seznam adresátů a posílat e-maily ...
- Super Formula Bar (snadno upravit více řádků textu a vzorce); Rozložení pro čtení (snadno číst a upravovat velké množství buněk); Vložit do filtrovaného rozsahu...
- Sloučit buňky / řádky / sloupce bez ztráty dat; Rozdělit obsah buněk; Zkombinujte duplicitní řádky / sloupce... Zabraňte duplicitním buňkám; Porovnat rozsahy...
- Vyberte možnost Duplikovat nebo Jedinečný Řádky; Vyberte prázdné řádky (všechny buňky jsou prázdné); Super hledání a fuzzy hledání v mnoha sešitech; Náhodný výběr ...
- Přesná kopie Více buněk beze změny odkazu na vzorec; Automaticky vytvářet reference do více listů; Vložte odrážky, Zaškrtávací políčka a další ...
- Extrahujte text, Přidat text, Odebrat podle pozice, Odebrat mezeru; Vytváření a tisk mezisoučtů stránkování; Převod mezi obsahem buněk a komentáři...
- Super filtr (uložit a použít schémata filtrů na jiné listy); Rozšířené řazení podle měsíce / týdne / dne, frekvence a dalších; Speciální filtr tučnou kurzívou ...
- Kombinujte sešity a pracovní listy; Sloučit tabulky na základě klíčových sloupců; Rozdělte data do více listů; Dávkový převod xls, xlsx a PDF...
- Více než 300 výkonných funkcí. Podporuje Office / Excel 2007-2019 a 365. Podporuje všechny jazyky. Snadné nasazení ve vašem podniku nebo organizaci. Kompletní funkce 30denní bezplatná zkušební verze. 60denní záruka vrácení peněz.

Office Tab přináší do Office rozhraní s kartami a usnadňuje vám práci
- Povolte úpravy a čtení na kartách ve Wordu, Excelu, PowerPointu, Publisher, Access, Visio a Project.
- Otevřete a vytvořte více dokumentů na nových kartách ve stejném okně, nikoli v nových oknech.
- Zvyšuje vaši produktivitu o 50% a omezuje stovky kliknutí myší každý den!

You are guest
or post as a guest, but your post won't be published automatically.
-
To post as a guest, your comment is unpublished.· 22 days agoHi, could anyone help I'm trying to send emails to a different address when a name is entered into a particular cell do I need to adjust this code or have a new piece of code for each address i.e if cell = AA then send to AA@hotmail.com or if cell = BB then send to BB@hotmail.com etc.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hi, I've been able to create an email with content, but now I'm trying to extract data from a specified cell related to the initial query. How do I get that data to show in the email Body with the below code? I've tried linking with a vLookUp function to extract that data. So basically, how do I get in the main example above, "This is line 1" to show the Data being pulled for xData below
Private Sub Worksheet_Change(ByVal Target As Range)
'Updated by Extendoffice 2017/9/12
Dim xRgSel As Range
Dim xOutApp As Object
Dim xMailItem As Object
Dim xMailBody As String
Dim xData As Object
On Error Resume Next
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set xRg = Range("J:J")
Set xRgSel = Intersect(Target, xRg)
Set xData = Application.WorksheetFunction.VLookup(xRgSel, Range("Data_Table"), 6, False)
ActiveWorkbook.Save
If Not xRgSel Is Nothing Then
Set xOutApp = CreateObject("Outlook.Application")
Set xMailItem = xOutApp.CreateItem(0)
xMailBody = xData & "Cell(s) " & xRgSel.Address(False, False) & _
" in the worksheet '" & Me.Name & "' were modified on " & _
Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
" by " & Environ$("username") & " in https://textmgmt.sharepoint.com/:x:/r/sites/EmailManagedServiceCollaboration/Documents/(%20Test)%20Shared%20TM%20Tracking%20Document%20-%20Local%20Email%20Campaign.xlsm?d=w7ad2f1404d574b9abb529ec248453f42&csf=1&web=1&e=zg9REc ."
With xMailItem
.To = "jason.thompson@textmanagement.co.uk"
.Subject = "Worksheet modified in Brand Approve Column "
.Body = xMailBody
.Send
End With
Set xRgSel = Nothing
Set xOutApp = Nothing
Set xMailItem = Nothing
End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
-
To post as a guest, your comment is unpublished.Hi, I am stuck with a piece of Code. I have combined a few comments below but can't get it to workI have added the xtarget part of the code in the Mail_small_text_Outlook to select a specific cell to be included as subjectAs a result, nothing works anymore. Could someone help pleaseDim xRg As Range
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("AC9"), Target)
If xRg Is Nothing Then Exit Sub
If Target.Value = 1 Then
ActiveSheet.Calculate
Call Mail_small_Text_Outlook
End If
End Sub
Sub Mail_small_Text_Outlook(xTarget As Range)
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim xR
xR = xTarget.Row
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = ""
On Error Resume Next
With xOutMail
.To = "email"
.CC = ""
.BCC = ""
.Subject = Cells(xR, 1).Value
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Private Sub Worksheet_Calculate()
Dim xI As Integer
Dim xRg As Range
Set xRg = Range("AC9")
On Error GoTo Err01
xI = Int(xRg.Value)
If xI = 1 Then
Call Mail_small_Text_Outlook
End If
Err01:
End Sub -
To post as a guest, your comment is unpublished.Good day,
Thank you for teaching me something new, I am trying to get excel to send out an email to different sales reps.
So if row X is an error code of "999" then it needs to send an email to the rep I placed the email addresses in row K as it differs for every line, but if row X is "0" it should not send an email.
is this possible? -
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.@Corben Baxter Hi,Supposing cell D7 is the email trigger, you just need to include this line Range("D7") & vbNewLine & _ in the xMailBody line of the code as follows:
xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
Range("D7") & vbNewLine & _
"This is line 2" -
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Thank you for the fantastic VBA coding, i am learning quite a bit. As I am a little "green" with this skill, i am currently trying to have the macro choose between doing nothing, or sending one of two different email to send based on a cell value. If the cell has noting in it then "do nothing", if the cell has a "1" or a "2" in it choose either "Mail_small_Text_Outlook()" or "Mail_small_Text_Outlook_2()". I can not figure this out and i am reaching out to see if there are some brighter minds that could help me with this.Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("N5"), Target)
If xRg Is Nothing Then Exit Sub
If IsNumeric(Target.Value) And Target.Value = 1 Then
Call Mail_small_Text_Outlook
Exit Sub
ElseIf IsNumeric(Target.Value) And Target.Value = 2 Then
Call Mail_small_Text_Outlook_2
Exit Sub
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 = "" & vbNewLine & vbNewLine & _
On Error Resume Next
With xOutMail
' .SentOnBehalfOfName = ""
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Sub Mail_small_Text_Outlook_2()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = "" & vbNewLine & vbNewLine & _
On Error Resume Next
With xOutMail
' .SentOnBehalfOfName = ""
.To = ""
.CC = ""
.BCC = ""
.Subject = ""
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Private Sub Worksheet_Calculate()
Dim xI As Integer
Dim xRg As Range
Set xRg = Range("N5")
On Error GoTo Err01
xI = Int(xRg.Value)
If xI = 1 Then
Call Mail_small_Text_Outlook
End If
Err01:
End Sub -
To post as a guest, your comment is unpublished.@crystal Hi Crystal,When targeting a range of cells... how do you get the automated email to call out "identify" the particular cell that generated the email. I am working on a usage based forklift PM spreadsheet that changes column E to read PM NEEDED after 300hrs. We currently have 27 forklifts (cells E3;E49), your code has worked for me in triggering the email but it would be ideal for the email to identify which cell was the email trigger.
-
To post as a guest, your comment is unpublished.@reeti jaswal Good day,The method in below article may do you a favor. Please have a look:How To Send Email If Due Date Has Been Met In Excel?
-
To post as a guest, your comment is unpublished.helloi want to send automatic emails when due date come to the concern departments.i have to apply this to 3 or 4 columnscan you please send me code
-
To post as a guest, your comment is unpublished.@Janelle Hi crystal,I have below requirement , My excel sheet has below columns Query,reported by,Answer,Answered by.So whenever new row has been added/Query or Answer column updated email should be triggered to many people automatically after saving that excelsheet.In that mail body- modified rows column values should display as Query was reported by that user and Query was answered by that column value. please help me to achieve this.
-
To post as a guest, your comment is unpublished.@Thais Hi Thais,I got your point. It just like sending an email on behalf of someone.Please add the below line to the code, and don't forget to specify the email address. (After running the code, you will see the From field has a fixed email address).SentOnBehalfOfName = "Email Address"The whole VBA code is as follows.Dim xRg As Range 'Update by Extendoffice 20120/8/28 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 .SentOnBehalfOfName = "Email Address" .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.@crystalHi Crystal!Thank you for the tip :-)The issue is that the code for sending the email is on a shared excel. People who mighty choose to send the automated email from this shared excel, are most often not the ones who might be able to answer if someone replies to the automated email. Therefore I would like the automated email to always be from a fixed email, in that way we avoid peopel who send the automated email to get questions. Do you know if this can be fixed? I mean, in the code you can choose the to: and subject:, shouldnt we be able to design the :from?
-
To post as a guest, your comment is unpublished.@Ckey1990 Hi Ckey1990,The below code can do you a favor, please have a try. Thank you.Dim xRg As Range 'Update by Extendoffice 2020/8/28 Private Sub Worksheet_Change(ByVal Target As Range) On Error Resume Next If Target.Cells.Count > 1 Then Exit Sub Set xRg = Intersect(Range("D:D"), Target) If xRg Is Nothing Then Exit Sub If Target.Value = "Done" Then Call Mail_small_Text_Outlook(Target) End If End Sub Sub Mail_small_Text_Outlook(xTarget As Range) Dim xOutApp As Object Dim xOutMail As Object Dim xMailBody As String Dim xR xR = xTarget.Row Set xOutApp = CreateObject("Outlook.Application") Set xOutMail = xOutApp.CreateItem(0) xMailBody = "Hi there" & vbNewLine & vbNewLine & _ Cells(xR, 1).Value & vbNewLine & _ Cells(xR, 2).Value & vbNewLine & _ Cells(xR, 3).Value & vbNewLine 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.Hello,If for example a cell in column D is filled in to meet the criteria to auto generate an email. Can this be adapted to pull the information from the row in which has been filled in.ie D7 is filled in so email the contents of A7, B7, C7ie D25 is filled in so email the contents of A25, B25, C25Thanks
-
To post as a guest, your comment is unpublished.@Saybier@gmail.com Hi Mladen,
I can only help to solve the formula problem for you. Please have a try. Thank you!
Dim xRg As Range
'Update by Extendoffice 2020/08/21
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
Private Sub Worksheet_Calculate()
Dim xI As Integer
Dim xRg As Range
Set xRg = Range("D7")
On Error GoTo Err01
xI = Int(xRg.Value)
If xI > 200 Then
Call Mail_small_Text_Outlook
End If
Err01:
End Sub -
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.I posted before and got a great answer...but moving forward i need more info. I have coulmn "C" listed with 25 different emails. I have column "D" "E" & "F" used for date completions (ex. August 17 = 081720). I need the email body to be different for each of the DEF cells but only go to ONE email in that coresponding row once entered
D3..(EMAIL A) to c3 address, E3(EMAIL B) to c3 address, F3 (EMAIL C) to c3 address
D4..(EMAIL A) to c4 address, E4(EMAIL B) to c4 address, F4 (EMAIL C) to c4 address...
Does this make sense?
-
To post as a guest, your comment is unpublished.@crystal This is great but I need it to pull information from other cells on the same row that is triggering the email... Can anyone help?
-
To post as a guest, your comment is unpublished.@crystal That only pulled the information for that cell. If you are only sending one email for the D7 cell this works but how do you get it to change to another cell for the next value in column D? As in if the next value over 200 is D10 how do you get the email to auto pull the information from F10 instead of F7? That's the issue I'm running into right now.Thank you!
-
To post as a guest, your comment is unpublished.Thank you for the code, this code works when I enter the value and press enter. But in my case the cell is filling automatically with formula, and when the value is reached it doesn't open the email (the code do not works in this case). I would also like the email body to populate data from the other cells for that line item how would I get it to pull that cell data in reference to the cell that created the email?Any help would be fantastic!
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.@Nicolas Molina Hi Nicolas Molina,
Maybe the code in this tutorial can help you solve the problem:
How To Send Email If Due Date Has Been Met In Excel?
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.@Mladen Hi Mladen,
Try the below code.
Dim xRg As Range
'Update by Extendoffice 2012/08/07
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
Private Sub Worksheet_Calculate()
Dim xI As Integer
Dim xRg As Range
Set xRg = Range("D7")
On Error GoTo Err01
xI = Int(xRg.Value)
If xI > 200 Then
Call Mail_small_Text_Outlook
End If
Err01:
End Sub
-
To post as a guest, your comment is unpublished.@Kyle To better clarify, I am hoping to auto-populate the column and row headers into the email, along with text.
Subject "(A3:A50) - (Q1:AC1) - Pending"
Mail Body "(P1:AB1) complete."
"Please prepare the (Q1:AC1)"
(i.e. C12, the headers for the subject line would be A12 and C1. C12 for the 1st line in the "Mail Body" and D12 for the 2nd.)
This way, any cell I enter data into will prompt the email and pull the headers in pertaining to that cell.
Thank you! -
To post as a guest, your comment is unpublished.Thank you for the code ! I'm having a small issue where the code will only select the first cell that meets my criteria
( I want to flag any tasks that are late so were due before the current date, however the code is only taking the first value and not all the values in my excle"
the following is my code, i would be forever grateful if you could help!
Public Sub Late_Task_Email()
'Updated by Extendoffice 2018/11/22
Dim xRgDate As Range
Dim xRgSend As Range
Dim xRgText As Range
Dim xRgDone As Range
Dim xOutApp As Object
Dim xMailItem As Object
Dim xLastRow As Long
Dim vbCrLf As String
Dim xMailBody As String
Dim xRgDateVal As String
Dim xRgSendVal As String
Dim xMailSubject As String
Dim xStrRang As String
Dim i As Long
On Error Resume Next
Set xRgDate = Range("F12:F500")
Set xRgDate = Range(xStrRang)
Set xRgSend = Range("B12:B500")
Set xRgSend = Range(xStrRang)
Set xRgText = Range("A12:A500")
Set xRgText = Range(xStrRang)
xLastRow = xRgDate.Rows.Count
Set xRgDate = xRgDate(1)
Set xRgSend = xRgSend(1)
Set xRgText = xRgText(1)
Set xOutApp = CreateObject("Outlook.Application")
For i = 1 To xLastRow
xRgDateVal = ""
xRgDateVal = xRgDate.Offset(i - 1).Value
If xRgDateVal <> "" Then
If CDate(xRgDateVal) - Date < 0 Then
xRgSendVal = xRgSend.Offset(i - 1).Value
xMailSubject = xRgText.Offset(i - 1).Value & " on " & xRgDateVal
vbCrLf = "
"
xMailBody = ""
xMailBody = xMailBody & "Dear DOE,"
xMailBody = xMailBody & "This task is OVERDUE!"
xMailBody = xMailBody & ""
Set xMailItem = xOutApp.CreateItem(0)
With xMailItem
.Subject = xMailSubject
.To = xRgSendVal
.HTMLBody = xMailBody
.Display
'.Send
End With
Set xMailItem = Nothing
End If
End If
Next
Set xOutApp = Nothing
End Sub
-
To post as a guest, your comment is unpublished.Hi Crystal, thank you for sharing this awesome code! I changed the code to function with text so when a cell has anything typed in, it prompts the email function. My question is, how can I code the email to auto-populate the row and column header info for a specific cell along with text? Here are the ranges of data with text:
Subject "(A3:A50) - (Q1:AC1) - Pending"
Mail Body "(P1:AB1) complete."
"Please prepare the (Q1:AC1)"
'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("P3:AB50"), Target)
If xRg Is Nothing Then Exit Sub
If Target.Value > 0 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 = "Hello," & vbNewLine & vbNewLine & _
"Create Bid Comparison step complete." & vbNewLine & _
"Please prepare the Recommendation of Award." & vbNewLine & vbNewLine & _
"Thank you"
On Error Resume Next
With xOutMail
.To = ""
.CC = ""
.BCC = ""
.Subject = ("Project 20-20")
.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.@crystal HelloI need a help. I use VB code for automatically to send email based on cell value in Excel. The thing is that this code works when I enter the value and press enter. But in my case the cell is filling automatically with formula, and when the value is reached it doesn't open the email (the code do not works in this case).
-
To post as a guest, your comment is unpublished.Hello
I need a help. I use VB code for automatically to send email based on cell value in Excel. The thing is that this code works when I enter the value and press enter. But in my case the cell is filling automatically with formula, and when the value is reached it doesn't open the email (the code do not works in this case). Ex.
-
To post as a guest, your comment is unpublished.Hey Crystal,
I'd like to set automatic emails based on drop options of Update Request, Complete or More Info in Cell I. Update Request will send an email to three different email addresses and Complete or More Info will send an email to one email address. Also, how do to I write each Range to equal data that corresponds to the row with the drop down option.
My data below only triggers off Update Request for a specific range (b3). Help!
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("I:I"), Target)
If xRg Is Nothing Then Exit Sub
If Target.Value = "Update Request" 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 = "All," & vbNewLine & vbNewLine & _
"Updates have been entered into the maintenance log:" & vbNewLine & _
Range("b3") & vbNewLine & _
Range("c3") & vbNewLine & _
Range("d3") & vbNewLine & vbNewLine & _
"Thanks," & vbNewLine & _
"Training Team"
With xOutMail
.To = "ShondaX@yahoo.com"
.CC = ""
.BCC = ""
.Subject = "Log Update Requests"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
Thanks, Shonda -
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.@Tyson nold Hi Tyson nold,
Supposing the recipient's email address is in F7, please apply the below code.
Dim xRg As Range
'Update by Extendoffice 2020/7/17
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 = Range("F7")
.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.Is there a way to send emails to individual recipients that can be pulled from another cell in the same row?
-
To post as a guest, your comment is unpublished.@crystal I have the exact situation but only difference is ...I have got around 100 rows to check. So it has to check each row on column D if any cell on column D meets the criteria it has to add info from column A B and C for respective D cell and after checking all 100 rows it has to send one email.
-
To post as a guest, your comment is unpublished.@Brittany Hi Brittany,
Supposing there are drop down lists in column H, and you want to trigger an email when selecting "Done" from the drop-down, please try the below VBA to get it down.
Dim xRg As Range
'Update by Extendoffice 2020/6/12
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Cells.Count > 1 Then Exit Sub
Set xRg = Intersect(Range("H:H"), Target)
If xRg Is Nothing Then Exit Sub
If Target.Value = "Done" 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
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.@thumsri@gmail.com Hi,
The below VBA code can help you solve the problem. Please have a try. Thank you.
Dim xRg As Range
'Update by Extendoffice 20120/5/22
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
Private Sub Worksheet_Calculate()
Dim xI As Integer
Dim xRg As Range
Set xRg = Range("D7")
On Error GoTo Err01
xI = Int(xRg.Value)
If xI > 200 Then
Call Mail_small_Text_Outlook
End If
Err01:
End Sub
-
To post as a guest, your comment is unpublished.@darkgyft Hi darkgyft.
Try the below VBA code. Hope I can help. thank you.
Dim xRg As Range
'Update by Extendoffice 2020/05/22
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)
On Error Resume Next
With xOutMail
.Display 'or use .Send
.To = "Email Address"
.CC = ""
.BCC = ""
.Subject = "send by cell value test"
.HTMLBody = "This is a test email sending in Excel" & "
" & .HTMLBody
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
-
To post as a guest, your comment is unpublished.Cystal,
This is definitely a time saver. I want to know what I would have to add to the vba to include my signature line on the outgoing email. On my usual emails, I have a signature line with company logo and contact info. I want to include that so the email looks just like it would if I had sent myself manually from Outlook. Many thanks!
Mike -
To post as a guest, your comment is unpublished.Hi - I tried to use the code, it works fine when cell value D7 is changed manually. if D7 value changed using any formula i.e vlookup or IF .code doesnt trigger. Code trigger only when any cell on the sheet is change keeping D7 value above 200. is there any wayout for the problem . regards srini
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.
-
To post as a guest, your comment is unpublished.Hi all - I am trying to use this code but when I exit the developer and test the cell- it is over the specifications, but nothing happens. I don't receive a pop up to send the email? But when I run the code in developer it comes up - its just when I'm within the spreadsheet and change the cell, nothing happens. I have copied and edited the code as per instructions.