Note: The other languages of the website are Google-translated. Back to English
Se connecter  \/ 
x
or
x
Inscrivez-vous  \/ 
x

or

Comment envoyer un e-mail si une certaine cellule est modifiée dans Excel?

Cet article parle de l'envoi d'e-mails via Outlook si une cellule d'une certaine plage est modifiée dans Excel.

Envoyer un e-mail si la cellule dans une certaine plage est modifiée avec le code VBA


Envoyer un e-mail si la cellule dans une certaine plage est modifiée avec le code VBA


Si vous devez créer automatiquement un nouvel e-mail avec un classeur actif joint lorsque les cellules de la plage A2: E11 sont modifiées dans une certaine feuille de calcul, le code VBA suivant peut vous aider.

1. Dans la feuille de calcul dont vous avez besoin pour envoyer un e-mail en fonction de sa cellule modifiée dans une certaine plage, cliquez avec le bouton droit sur l'onglet de la feuille, puis cliquez sur le bouton Voir le code dans le menu contextuel. Voir la capture d'écran:

2. Dans le pop-up Microsoft Visual Basic pour applications , veuillez copier et coller ci-dessous le code VBA dans la fenêtre Code.

Code VBA: envoyer un e-mail si la cellule d'une plage spécifiée est modifiée dans Excel

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
    On Error Resume Next
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set xRg = Range("A2:E11")
    Set xRgSel = Intersect(Target, xRg)
    ActiveWorkbook.Save
    If Not xRgSel Is Nothing Then
        Set xOutApp = CreateObject("Outlook.Application")
        Set xMailItem = xOutApp.CreateItem(0)
        xMailBody = "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") & "."

        With xMailItem
            .To = "Email Address"
            .Subject = "Worksheet modified in " & ThisWorkbook.FullName
            .Body = xMailBody
            .Attachments.Add (ThisWorkbook.FullName)
            .Display
        End With
        Set xRgSel = Nothing
        Set xOutApp = Nothing
        Set xMailItem = Nothing
    End If
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

Notes:

1). Dans le code, A2: E11 est la plage sur laquelle vous enverrez un e-mail.
2). Veuillez modifier le corps de l'e-mail selon vos besoins dans xMailCorps ligne dans le code.
3). Remplace le E mail avec l'adresse e-mail du destinataire en ligne .To = "Adresse e-mail".
4). Changer l'objet de l'e-mail en ligne .Subject = "Feuille de travail modifiée dans" & ThisWorkbook.FullName.

3. appuie sur le autre + Q touches simultanément pour fermer le Microsoft Visual Basic pour applications fenêtre.

À partir de maintenant, toute cellule de la plage A2: E11 est modifiée, un nouvel e-mail sera créé avec le classeur mis à jour en pièce jointe. Et tous les champs spécifiés tels que l'objet, le destinataire et le corps de l'e-mail seront répertoriés dans l'e-mail. Merci d'envoyer l'e-mail.

Notes: Le code VBA ne fonctionne que lorsque vous utilisez Outlook comme programme de messagerie.


Articles Liés:


Les meilleurs outils de productivité de bureau

Kutools for Excel résout la plupart de vos problèmes et augmente votre productivité de 80%

  • Réutilisation: Insérer rapidement formules complexes, graphiques et tout ce que vous avez utilisé auparavant; Crypter les cellules avec mot de passe; Créer une liste de diffusion et envoyer des e-mails ...
  • Barre Super Formula (modifiez facilement plusieurs lignes de texte et de formule); Disposition de lecture (lire et modifier facilement un grand nombre de cellules); Coller dans la plage filtrée...
  • Fusionner les cellules / lignes / colonnes sans perdre de données; Contenu des cellules divisées; Combiner des lignes / colonnes en double... Empêcher les cellules en double; Comparer les gammes...
  • Sélectionnez Dupliquer ou Unique Lignes; Sélectionnez les lignes vides (toutes les cellules sont vides); Super Find et Fuzzy Find dans de nombreux classeurs; Sélection aléatoire ...
  • Copie exacte Plusieurs cellules sans changer la référence de formule; Créer automatiquement des références à plusieurs feuilles; Insérer des puces, Cases à cocher et plus encore ...
  • Extrait du texte, Ajouter du texte, Supprimer par position, Supprimer l'espace; Créer et imprimer des sous-totaux de pagination; Conversion entre le contenu et les commentaires des cellules...
  • Super filtre (enregistrer et appliquer des schémas de filtrage à d'autres feuilles); Tri avancé par mois / semaine / jour, fréquence et plus; Filtre spécial par gras, italique ...
  • Combiner des classeurs et des feuilles de travail; Fusionner les tableaux en fonction des colonnes clés; Diviser les données en plusieurs feuilles; Conversion par lots xls, xlsx et PDF...
  • Plus de 300 fonctionnalités puissantes. Prend en charge Office / Excel 2007-2019 et 365. Prend en charge toutes les langues. Déploiement facile dans votre entreprise ou organisation. Essai gratuit de 30 jours. Garantie de remboursement de 60 jours.
onglet kte 201905

Office Tab apporte une interface à onglets à Office et simplifie considérablement votre travail

  • Activer l'édition et la lecture par onglets dans Word, Excel, PowerPoint, Publisher, Access, Visio et Project.
  • Ouvrez et créez plusieurs documents dans de nouveaux onglets de la même fenêtre, plutôt que dans de nouvelles fenêtres.
  • Augmente votre productivité de 50% et réduit des centaines de clics de souris chaque jour!
bas de cabine
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.
    HumAnCK · 6 months ago
    Hello, i was trying to send emails on my sheet using one value changed on the sheet. If on column H the status will be changed to ="4" the Order ID in the left should be send to one user. The sheet works dynamicly so i have a Range from D9:D140 where order id's are stored and status changes are made in the same range on H9:H140. How can i reach the goal to do so and send the Order ID to my customer when status has been changed to ="4" ?
  • To post as a guest, your comment is unpublished.
    james2706 · 1 years ago
    Hello. How do I modify the code so that an e-mail is sent to another e-mail address if another range of cells is edited?
    • To post as a guest, your comment is unpublished.
      joseph · 1 years ago
      Any help on this request? I am having same issue. I want to add multiple email address per row, but when I change one row, the entire worksheet changes. How can I limit the changes only to one row?
      • To post as a guest, your comment is unpublished.
        Pawel · 1 years ago
        Edit linie:
        1). In the code, A2:E11 is the range you will send email based on.
        and
        3). Replace the Email Address with the recipient email address in line .To = "Email Address".

        Works fine.
        • To post as a guest, your comment is unpublished.
          Owen · 10 months ago
          Can you please explain this further. How do you repeat the code to send to a different email based on another range being modified. I have tried copy and pasting the code below and changing as per your comment, but still only the first range seems to execute the command and write the email.
  • To post as a guest, your comment is unpublished.
    Ioana · 2 years ago
    hi; is there a way to change text displayed using information from other cells (from first row and first column)? for example, if I change cell K15, I want to include in the message info on cells A15 and K1? what should I change in the code? thank you very much
    • To post as a guest, your comment is unpublished.
      laya · 4 months ago
      hi Laona. do u find out how can do this?

  • To post as a guest, your comment is unpublished.
    jimmy@globeways.com · 2 years ago
    I have tried above VBA code: Send email if cell in a specified range is modified in Excel. This VBA works for me except sending email. When the data is modified in the given range an email is automatically generated with modified cell details. However, the email is not automatically sending to the recipient and the user has to click send button in the email. What I am looking here is, the email has to send to the recipients automatically when it is generated. Please help me to provide a code for this. Many thanks
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Jimmy Joseph,
      Please replace the line ".Display" with ".Send". Hope I can help. Thanks for commenting.
  • To post as a guest, your comment is unpublished.
    sagar · 2 years ago
    if i have to send that entire row then?
  • To post as a guest, your comment is unpublished.
    Brad · 2 years ago
    Great information.
    Question regarding the information that can be added to the email.
    Using your example above....

    If you had a value in F4, how would you include the F4 Value in the email that was generated when D4 was modified??
  • To post as a guest, your comment is unpublished.
    Sonu · 2 years ago
    If i want to send the cell value instead of the address..then what shall I change in the code?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi,
      You can try the below VBA code.

      Private Sub Worksheet_Change(ByVal Target As Range)
      Dim xRgSel As Range
      Dim xOutApp As Object
      Dim xMailItem As Object
      Dim xMailBody As String
      On Error Resume Next
      Application.ScreenUpdating = False
      Application.DisplayAlerts = False
      Set xRg = Range("A2:E11")
      Set xRgSel = Intersect(Target, xRg)
      ActiveWorkbook.Save
      If Not xRgSel Is Nothing Then
      Set xOutApp = CreateObject("Outlook.Application")
      Set xMailItem = xOutApp.CreateItem(0)
      xMailBody = "Cell(s) " & xRgSel.Address(False, False) & _
      xRgSel.Value & _
      " in the worksheet '" & Me.Name & "' were modified on " & _
      Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
      " by " & Environ$("username") & "."

      With xMailItem
      .To = "Email Address"
      .Subject = "Worksheet modified in " & ThisWorkbook.FullName
      .Body = xMailBody
      .Attachments.Add (ThisWorkbook.FullName)
      .Display
      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.
        Gurpreet singh · 5 months ago
        What if we only want the updated comments in that cell an not the entire cell value
        It should show only the latest comments added in the cell

  • To post as a guest, your comment is unpublished.
    Mahadev · 2 years ago
    Dear Admin,


    I need your help,



    I have an excel to monitor daily work done details by our worker from the field, so is this possible to trigger a mail from excel sheet if that guy failed to update the data in that excel sheet on given time.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi,
      Can't help with this.
  • To post as a guest, your comment is unpublished.
    sjones · 2 years ago
    I was able to create the macro, however I'm having a problem. I would like to automatically send an email when a cell reaches a certain threshold. The cell is a formula. When the calculation sum goes below the said threshold it doesn't do anything; however, if I type directly into the cell it will process the macro as planned. Does the formula mess up the macro?
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Hi Sissy Jones,
      The method in this article: How to automatically send email based on cell value in Excel?
      https://www.extendoffice.com/documents/excel/4656-excel-send-email-based-on-cell-value.html may help you solve the problem.
  • To post as a guest, your comment is unpublished.
    pakxrohn@gmail.com · 3 years ago
    I am stuck in below VB code. I am trying to get email notification to the user where the data has been changed. Email is working but when I make any change email initiated at once but I want email when excel sheet is saved and closed after making all the changes to all users who impacted. Also this should be working for any of the sheets in entire excel workbook.

    Please help...

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    '****Declaration of objects and variables******

    Dim xRgSel As Range Dim xOutApp As Object Dim xMailItem As Object Dim xMailBody As String Dim mailTo As String

    On Error Resume Next

    Sheets("TargetSheet").Range("TargetRange").Select

    Application.ScreenUpdating = False Application.DisplayAlerts = False

    'Set xRg = Range("A" & Rows.Count).End(xlUp).Row

    Set xRg = Range("A2:DA1000")
    Set xRgSel = Intersect(Target, xRg)


    ActiveWorkbook.Save
    '**********Outlook Application Opening***********

    If Not xRgSel Is Nothing Then

    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailItem = xOutApp.CreateItem(0)

    xMailBody = "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") & "."
    '***********Finding recipient List************

    If Cells(xRgSel.Row, "A").Value = "Pankaj" Then

    mailTo = "pank12***@gmail.com"

    End If

    If Cells(xRgSel.Row, "A").Value = "Nitin" Then

    mailTo = "pank****@gmail.com"

    End If

    If Cells(xRgSel.Row, "A").Value = "Chandan" Then

    mailTo = "pakxro**@gmail.com"

    End If
    '*************Email composing*************

    With xMailItem

    .To = mailTo
    .Subject = "Worksheet modified in " & ThisWorkbook.FullName
    .Body = xMailBody
    '.Attachments.Add (ThisWorkbook.FullName)
    .Display

    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.
      crystal · 3 years ago
      Dear Pankaj Shukla,
      Post your Excel question to our forum: https://www.extendoffice.com/forum.html to get more supports about Excel from our Excel professional.