VBA Excel include a IF statement in email form

excel vba if then multiple statements
excel vba if statement multiple conditions
excel vba if cell contains value then
excel vba if statement based on cell value
access vba if statement multiple conditions
vba if statement multiple lines
vba if column contains value then
vba nested if then else in a for next loop

I can't seem to include a IF formula in my code, in the part where the email is being sent to some people included in column R2 of my Excel file

I would like to exclude from .CC the email yyy@gmail.com if this user is already in column R2 (so in .To). How could I write the code? Otherwise, my user yyy@gmail.com is receiving the email twice, once because the user is in .To and once because he is in .CC

.To = Worksheets("MyData").Range("R2").Value
.CC = "yyy@gmail.com; zzz@gmail.com; aaa@gmail.com"
.BCC = ""
.Subject = "TEST"
.Body = "Hello"
.Attachments.Add ThisWorkbook.Path & "\ME\" & Check & " " & Format(Now, "dd-mmm-yy") & ".xlsx"
.Display
'.Send

What could be the code to do that?


I wouldn't use an if statement for this. Instead I would remove each recipient from the cc line after setting both

.to = "yyy@gmail.com"
.cc = "yyy@gmail.com; zzz@gmail.com; aaa@gmail.com"
For Each addr In Split(.to, ";")
    .cc = Replace(.cc, addr, vbNullString)
Next addr
.BCC = ""
.Subject = "TEST"
.Body = "Hello"
.Attachments.Add ThisWorkbook.Path & "\ME\" & Check & " " & Format(Now, "dd-mmm-yy") & ".xlsx"
.Display

The for loop splits the to line on the semi-colon character ; and then loops through each one removing it from the .cc This will handle both single and multiple recipients in both the .To and .CC line

MS Excel: How to use the IF-THEN-ELSE Statement (VBA), How do I write an if statement in Excel VBA? The block If must end with an End If statement. To determine whether or not a statement is a block If, examine what follows the Then keyword. If anything other than a comment appears after Then on the same line, the statement is treated as a single-line If statement. The Else and ElseIf clauses are both optional.


You mean like this:

If Worksheets("MyData").Range("R2").Value2 = "yyy@gmail.com" Then
    .CC = "zzz@gmail.com; aaa@gmail.com"
Else
    .CC = "yyy@gmail.com; zzz@gmail.com; aaa@gmail.com"
End If

Excel VBA Range Object: 18 Useful Ways Of Referring To Cell Ranges, . In both of these, one or more conditions are evaluated and a block of code is executed depending on the result of the evaluation. Excel VBA IF Statements. IF statement in VBA code is one of the most frequently used which allows you to make a choice. IF function in VBA is different when compared with EXCEL IF function i.e. in Excel IF function or formula checks whether the given condition or criteria evaluates to TRUE or FALSE and then returns a value based on the evaluation, Whereas VBA IF statement will perform only the first half of statement, i.e. check if the condition evaluates to TRUE or FALSE, additionally THEN


Just use Replace to remove the address in .To from your list in .CC

.To = Worksheets("MyData").Range("R2").Value
.CC = Replace("yyy@gmail.com; zzz@gmail.com; aaa@gmail.com", .To, vbNullString)

Conditional Statements in Excel VBA, Hi all, I am creating a user form within Excel file that is used to input data into VBA - Using if statement within a string for outlook email from Excel Here is code you can add to button on the UserForm to display the option  Using Else With the VBA If Statement. The VBA Else statement is used as a catch all. It basically means “if no conditions were true” or “everything else”. In the previous code example, we didn’t include a print statement for a fail mark. We can add this using Else.


By this the addresses are trimmed and compared case insensitive:

    .To = Worksheets("MyData").Range("R2").Value
    .CC = ""
    Dim check As Variant
    For Each check In Split("yyy@gmail.com; zzz@gmail.com; aaa@gmail.com", ";")
        If Not InStr(1, .To, Trim(check), vbTextCompare) > 0 Then _
            .CC = .CC & "; " & check
    Next check

VBA, A block form If statement must be the first statement on a line. This example shows both the block and single-line forms of the IfThenElse  IF is one of the most popular and frequently used statement in VBA. IF statement in VBA is sometimes also called as IF THEN ELSE Statement. The task of IF Statement is to check if a particular condition is met or not. If you have followed my earlier posts, then you would remember that we discussed If Function in Excel.


VBA IF Statement - A Complete Guide, To run more than one line of code, you must use the multiple-line syntax. This syntax includes the End If statement, as shown in the following  Automatically send email based on cell value with VBA code. Please do as follows to send an email based on cell value in Excel. 1. In the worksheet you need to send email based on its cell value (here says the cell D7), right-click the sheet tab, and select View Code from the context menu.


IfThenElse statement (VBA), In Excel VBA, IF Then Else statement allows you to check for a condition, and perform an action accordingly. construct can be used in Excel VBA, and some practical examples in action. you will see a warning (as the vba codes are lost when you save it in .xlsx format). This will insert a module object for your workbook. 4. Change the names and captions of the controls according to the table below. Names are used in the Excel VBA code. Captions are those that appear on your screen. It is good practice to change the names of controls. This will make your code easier to read.


Using IfThenElse statements (VBA), I'm using the email code from http://support.microsoft.com/kb/209948 to generate Next step, I'd like to include IF-THEN logic in the email body to display one or the So either there are escape characters and/or a particular statement format  One line statement is perfect if you are using IF-Then statement. The basic to use one line statement is to write your entire code in one line. If A1 = 10 Then Msgbox("Cell A1 has value 10") In above statement, we have written IF statement to evaluate if cell A1 has value 10 then it will show a message box.