+ Reply to Thread
Results 1 to 8 of 8

Worsheet_Change with 4 condition to send mail

  1. #1
    Registered User
    Join Date
    08-28-2015
    Location
    paris
    MS-Off Ver
    2010
    Posts
    5

    Worsheet_Change with 4 condition to send mail

    Hi, I make macro to send automatic mail when in column there are different number. I would like to upgrade this macro and add one condition to send mail.
    This is all condition to send my mail.

    IF column 21 = 31 or 18 or 36 or 34 or 99 and Column 24 = not empty => send mail
    IF column 25 = 31 or 18 or 36 or 34 or 99 and Column 28 = not empty => send mail
    IF column 29 = 31 or 18 or 36 or 34 or 99 and Column 32 = not empty => send mail
    IF column 33 = 31 or 18 or 36 or 34 or 99 and Column 36 = not empty => send mail

    I tried with this one but is working only when all column are not empty. But I need just condition as write above.
    Any idea?


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TablCode
    Dim Email_Subject, Email_Send_From, Email_Send_To, _
    Email_Cc, Email_Bcc, Email_Body As String
    Dim Mail_Object, Mail_Single As Variant

    TablCode = Array(31, 34, 36, 18, 99)
    TablTargetColumns Array(21, 25, 29, 33)
    TablNoemptyColumns Array(24, 28, 32, 36)

    notEmpty = False
    For Each c In TablNoemptyColumns
    If Not IsEmpty(Target.Parent.Cells(Target.Row, c).Value) Then
    notEmpty = True
    Exit For
    End If
    Next
    If InStr(Join(TablTargetColumns, " ") & " ", Target.Column & " ") > 0 And _
    InStr(Join(TablCode, " ") & " ", Target.Value & " ") > 0 And _
    notEmpty Then

    'Macro email
    '--------------------------------------------------------
    If OutlookOuvert = False Then o = Shell("Outlook", vbNormalNoFocus)
    Email_Subject = " DL " & TablCode(I)
    Email_Send_From = "[email protected]"
    Email_Send_To = "[email protected]"
    Email_Cc = "[email protected]"
    Email_Bcc = "[email protected]"
    Email_Body = "Auto-mail" & vbCr & _
    "" & vbCr & _
    "Un code " & " a été attribué aujourd'hui" & vbCr & _
    vbCr & _
    "Date : " & Cells(Target.Row, 1) & vbCr & _
    "Nom agent: " & Cells(Target.Row, 2) & vbCr & _
    "Vol Départ: " & Cells(Target.Row, 13) & vbCr & _
    "STD: " & Format(Cells(Target.Row, 18), "hh:mm") & vbCr & _
    "ATD: " & Format(Cells(Target.Row, 19), "hh:mm") & vbCr & vbCr & _
    "DR1: " & Cells(Target.Row, 21) & vbCr & _
    "Time: " & Format(Cells(Target.Row, 23), "hh:mm") & vbCr & _
    "Explication: " & Cells(Target.Row, 24) & vbCr & vbCr & _
    "DR2: " & Cells(Target.Row, 25) & vbCr & _
    "Time: " & Format(Cells(Target.Row, 27), "hh:mm") & vbCr & _
    "Explication: " & Cells(Target.Row, 28) & vbCr & vbCr & _
    "DR3: " & Cells(Target.Row, 29) & vbCr & _
    "Time: " & Format(Cells(Target.Row, 31), "hh:mm") & vbCr & _
    "Explication: " & Cells(Target.Row, 32) & vbCr & vbCr & _
    "DR4: " & Cells(Target.Row, 33) & vbCr & _
    "Time: " & Format(Cells(Target.Row, 35), "hh:mm") & vbCr & _
    "Explication: " & Cells(Target.Row, 36) & vbCr & vbCr & _
    "@TT"

    On Error GoTo debugs
    Set Mail_Object = CreateObject("Outlook.Application")
    Set Mail_Single = Mail_Object.CreateItem(0)
    With Mail_Single
    .Subject = Email_Subject
    .To = Email_Send_To
    .cc = Email_Cc
    .BCC = Email_Bcc
    .Body = Email_Body
    .send
    End With
    debugs:
    If Err.Description <> "" Then MsgBox Err.Description
    '----------------------------------------------------------------
    End If
    End Sub







  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: Worsheet_Change with 4 condition to send mail

    Here:
    Please Login or Register  to view this content.
    you check if Traget (changed cell) column is between these listed in your TablTargetColumns and Value you just entered in the changed cell is between these listed in TablCode. It is probably not_excactly :-P what you want to achieve.

    May be try:

    Please Login or Register  to view this content.
    PS. as all columns Noempty are 3 right from TargetColumns only one table of columns numbers could be used.
    Last edited by Kaper; 08-28-2015 at 05:59 AM.
    Best Regards,

    Kaper

  3. #3
    Registered User
    Join Date
    08-28-2015
    Location
    paris
    MS-Off Ver
    2010
    Posts
    5

    Re: Worsheet_Change with 4 condition to send mail

    PERFECT its exactly that I need thank you so much I spend to much time on this macro thx thx

    Last question; the only bug I got now is on MAIL SUBJECT: before I put this one Email_Subject = " DL " & TablCode(i) it was working good;
    Example; When I put number 31, mail subject was DL31 When I put number 36, mail subject was DL36
    Now, I don't know why, if I put number 31 mail subject is DL18..... do youknow why?

  4. #4
    Registered User
    Join Date
    08-28-2015
    Location
    paris
    MS-Off Ver
    2010
    Posts
    5

    Re: Worsheet_Change with 4 condition to send mail

    I do not know if its possible too, to add one last condition
    If write number 99 mail will be send.
    Is it possible du put If column 22 or 26 or or 30 or 34 = 99A => do not send mail

    ?

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: Worsheet_Change with 4 condition to send mail

    a): before you had
    Please Login or Register  to view this content.
    but variable i was not used (so by default =0) thus you always had 31 because it is first element (or in other words: index = 0 element) of the matrix.
    I've not noticed this use of variable i, because you have not defined it. Use it here:

    Please Login or Register  to view this content.

    b) as it is now:
    Please Login or Register  to view this content.
    add second condition here:
    Please Login or Register  to view this content.
    PS. I insist: please do use code tags. Edit your posts and apply them. Read in http://www.excelforum.com/forum-rule...rum-rules.html how to do it.

  6. #6
    Registered User
    Join Date
    08-28-2015
    Location
    paris
    MS-Off Ver
    2010
    Posts
    5

    Re: Worsheet_Change with 4 condition to send mail

    I try both but Is not working; the first still DL31 in email subject iso 18 for example; et for the second they are error messages all the sentence in yellow

  7. #7
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,616

    Re: Worsheet_Change with 4 condition to send mail

    No file => no test.

    I can only see obvoius mistake here:
    Please Login or Register  to view this content.
    sorry for that, but as I said - no file - no option to test the code.
    Last edited by Kaper; 08-28-2015 at 10:03 AM.

  8. #8
    Registered User
    Join Date
    08-28-2015
    Location
    paris
    MS-Off Ver
    2010
    Posts
    5

    Re: Worsheet_Change with 4 condition to send mail

    Sorry I was at work, I joint my test file with.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] cdo send send mail - getting my external .vbs script directly into a an excel worksheet
    By PieterBlan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-09-2014, 08:10 AM
  2. Replies: 1
    Last Post: 03-11-2014, 12:24 PM
  3. Send to mail recipient, 1 worksheet, 2 cell ranges containing e-mail addresses
    By caladai in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-28-2013, 02:26 PM
  4. Find duplicated cell, with certain condition and send mail under the same address.
    By EugeneE in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-28-2013, 11:49 PM
  5. Automatic send email (without even click send in mail software) with excel vba ??
    By alexnkc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2013, 08:31 PM
  6. How to e-mail selected row and use e-mail address in a cell to send e-mail from excel
    By syedalamgir in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-27-2010, 02:15 AM
  7. Send e-mail based on condition
    By LPC in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-10-2009, 10:57 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1