Petua: Bahasa lain diterjemahkan Google. Anda boleh melawat English versi pautan ini.
Log masuk
x
or
x
x
Daftar Sekarang
x

or

Bagaimana untuk menghantar e-mel secara automatik berdasarkan nilai sel dalam Excel?

Sekiranya anda ingin menghantar e-mel melalui Outlook kepada penerima tertentu berdasarkan nilai sel tertentu dalam Excel. Sebagai contoh, apabila nilai sel D7 dalam lembaran kerja lebih besar daripada 200, maka e-mel dibuat secara automatik. Artikel ini memperkenalkan kaedah VBA untuk anda dengan cepat menyelesaikan masalah ini.

Hantar e-mel secara automatik berdasarkan nilai sel dengan kod VBA


Mudah menghantar e-mel melalui Outlook berdasarkan bidang senarai surat yang dibuat dalam Excel:

Platform Menghantar emel utiliti Kutools untuk Excel membantu pengguna menghantar e-mel melalui Outlook berdasarkan senarai mel yang dibuat dalam Excel. Muat turun ciri penuh 60-hari percuma jejak Kutools untuk Excel sekarang!

  • Buat senarai mel dengan semua medan mel yang diperlukan dan pilih semuanya.
  • Dayakan fungsi Hantar E-mel dan isi badan e-mel anda.
  • Hantar melalui pandangan.

Kutools untuk Excel: dengan lebih daripada 200 berguna Excel tambahan, bebas untuk mencuba tanpa had pada hari 60. Muat turun percubaan percuma Sekarang!


Hantar e-mel secara automatik berdasarkan nilai sel dengan kod VBA

Sila lakukan seperti berikut untuk menghantar e-mel berdasarkan nilai sel dalam Excel.

1. Dalam lembaran kerja anda perlu menghantar e-mel berdasarkan nilai selnya (di sini kata sel D7), klik kanan tab helaian, dan pilih Lihat Kod dari menu konteks. Lihat tangkapan skrin:

2. Dalam bermunculan Microsoft Visual Basic untuk Aplikasi tetingkap, sila salin dan tampal di bawah kod VBA ke dalam tetingkap kod kunci.

Kod VBA: Hantar e-mel melalui Outlook berdasarkan nilai sel dalam 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

Nota:

1. Dalam kod VBA, D7 dan nilai> 200 adalah sel dan nilai sel anda akan menghantar e-mel berdasarkan.

2. Sila tukar badan e-mel seperti yang anda perlukan xMailBody baris dalam kod.

3. Ganti Alamat E-mel dengan alamat e-mel penerima dalam talian .To = "Alamat E-mel".

4. Dan tentukan penerima Cc dan Bcc yang anda perlukan .CC = "" dan Bcc = "" bahagian.

5. Akhir sekali tukar mata pelajaran dalam talian .Subject = "hantar dengan ujian nilai sel".

3. Tekan butang Alt + Q kunci bersama untuk menutup Microsoft Visual Basic untuk Aplikasi tingkap.

Mulai sekarang, apabila nilai yang anda masukkan dalam sel D7 lebih besar daripada 200, e-mel dengan penerima dan badan yang ditentukan akan dibuat secara automatik di Outlook. Anda boleh klik menghantar butang untuk menghantar e-mel ini. Lihat tangkapan skrin:

Nota:

1. Kod VBA hanya berfungsi apabila anda menggunakan Outlook sebagai program e-mel anda.

2. Jika data yang dimasukkan dalam sel D7 adalah nilai teks, tetingkap e-mel akan muncul juga.


Tab Pejabat - Pelayaran Tab, Penyuntingan, dan Urusan buku kerja dalam Excel:

Tab Pejabat membawa antara muka tab seperti yang dilihat dalam pelayar web seperti Google Chrome, versi baru Internet Explorer dan Firefox ke Microsoft Excel. Ia akan menjadi alat penjimatan masa dan tidak merosot dalam kerja anda. Lihat demo di bawah:

Klik untuk percubaan percuma Tab Pejabat!

Tab Pejabat untuk Excel


Related articles:


Cadangan Alat Produktiviti untuk Excel

kte tab 201905

Kutools untuk Excel Membantu Anda Selalu Menyelesaikan Kerja di Hadapan Masa, dan Berdiri Daripada Orang

  • Lebih daripada ciri-ciri canggih 300 yang canggih, yang direka untuk senario kerja 1500, meningkatkan produktiviti oleh 70%, memberi anda lebih banyak masa untuk menjaga keluarga dan menikmati kehidupan.
  • Tidak perlu lagi menghafal formula dan kod VBA, berikan rehat dari otak anda sekarang.
  • Menjadi pakar Excel dalam minit 3, Operasi yang rumit dan berulang boleh dilakukan dalam beberapa saat,
  • Mengurangkan beribu-ribu operasi papan kekunci & tetikus setiap hari, mengucapkan selamat tinggal kepada penyakit pekerjaan sekarang.
  • 110,000 orang yang sangat berkesan dan pilihan syarikat 300 + yang terkenal di dunia.
  • Ciri-ciri penuh 60-hari adalah percubaan percuma. Jaminan wang balik 60 hari. Tahun 2 peningkatan dan sokongan percuma.

Membawa Browsing Tab dan Editing ke Microsoft Office, Jauh Lebih Berkuasa Daripada Tab Penyemak Imbas

  • Tab Pejabat direka untuk Aplikasi Pejabat Word, Excel, PowerPoint dan Lain-lain: Penerbit, Akses, Visio dan Projek.
  • Buka dan buat beberapa dokumen dalam tab baharu pada tetingkap yang sama, dan bukannya dalam tetingkap baru.
  • Meningkatkan produktiviti anda oleh 50%, dan mengurangkan beratus-ratus klik tetikus untuk anda setiap hari!
Say something here...
symbols left.
You are guest ( Sign Up? )
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.
    wu · 11 months ago
    Hi.

    I am wondering whether it is possible to tailor the first line of each email. For example, in my excel sheet I have a column with title such as Mr., a column with last name such as Black.
    I want to send out emails automatically with the first line: Dear Mr. Black
    for the others the first line will be accordingly dear ms. White....etc..


    Will that be possible to change the wording in the body of the email. That is my issue here.


    Thanks a lot
  • To post as a guest, your comment is unpublished.
    ExcelEnthusiast · 11 months ago
    I have a requirement to send over 50 email. Below are conditions:-


    1. Each workbook will be send as a separate email.
    2. Each email will have different recipient for To, CC and BCC.
    3. Each email will have different subject based on workbook/worksheet name.
    4. Each email will have different body based on date & subject.


    Now, a table can be made in excel which will list all points from 1-4 in different columns as a master data table. I can achieve this part.


    Can there be a macro which can read this table and create separate emails by adding attachments from a path?


    This will be a great help and save tremendous amount of time.
  • To post as a guest, your comment is unpublished.
    Justin Patterson · 1 years ago
    I have a worksheet that has about 20 columns of data, but I only have two that I want to focus on here. Column D is lot number and Column Z is priority. I would like excel to send and email when Column Z has a priority of “HOT” and send an email including the associated lot number in Column D. It will either be HOT or blank so nothing needs to be done unless it is HOT. I would like the Email to go to about 5 people and say something along the lines of “Hello Team, Lot “xxxx’s” Priority has changed to HOT, please prioritize this lot.


    Is it possible to have it look up the lot number associated to the priority and place it in the text like that. If not maybe have the subject of the email be the associated lot number and then have the text just be as it was without the lot number.
    • To post as a guest, your comment is unpublished.
      crystal · 11 months ago
      Hi Patterson,
      The below VBA code can help you solve the problem. Please have a try. Thank you for your comment.

      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRg As Range
      On Error Resume Next
      If (Target.Count > 1) Then Exit Sub
      Set xRg = Intersect(Target, Range("Z:Z"))
      If xRg Is Nothing Then Exit Sub
      If UCase(Target.Value) = "HOT" Then
      Call Mail_small_Text_Outlook(Target)
      End If
      End Sub
      Sub Mail_small_Text_Outlook(ByVal xCell As Range)
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      xMailBody = "Hello Team" & vbNewLine & vbNewLine & _
      "Lot " & Range("D" & xCell.Row) & "'s Priority has changed to HOT, please prioritize this lot."
      On Error Resume Next
      With xOutMail
      .To = "Email Address1; Email Address2; Email Address3; Email Address4; Email Address5"
      .CC = ""
      .BCC = ""
      .Subject = "Lot number is " & Range("D" & xCell.Row)
      .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.
    Vignesh · 1 years ago
    I am trying to mail the content which is present in A1:G5 columns in excel ,but its not working. Can you help me with that?
    • To post as a guest, your comment is unpublished.
      crystal · 11 months ago
      Hi Vignesh,
      The following VBA code can help you solve the problem. Thank you for your comment.

      Dim 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("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 I, J As Long
      Dim xRg As Range
      Dim xStr As String
      Dim xOutApp As Object
      Dim xOutMail As Object
      Dim xMailBody As String
      Set xOutApp = CreateObject("Outlook.Application")
      Set xOutMail = xOutApp.CreateItem(0)
      Set xRg = Range("A1:G5")
      For I = 1 To xRg.Rows.Count
      For J = 1 To xRg.Columns.Count
      xStr = xStr & xRg.Rows(I).Cells(J) & " "
      Next
      xStr = xStr & vbNewLine
      Next
      xMailBody = "Hi there" & vbNewLine & vbNewLine & xStr
      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.
    Pavel · 1 years ago
    I would like to ask if it is possible to send one email when closing the application - each change means a large number of emails.


    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If (Not Intersect(Target, Range("B2:B9")) Is Nothing) And (Target.Value < 3) 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 = "Ahoj," & vbNewLine & vbNewLine & _
    "xxx." & vbNewLine & _
    ""
    On Error Resume Next
    With xOutMail
    .To = "xxx"
    .CC = ""
    .BCC = ""
    .Subject = "xxx"
    .Body = xMailBody
    .Send 'or use .Send
    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing
    End Sub


    Thank you