+ Reply to Thread
Results 1 to 3 of 3

Using IF condition in email body code using VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    09-23-2011
    Location
    Calcutta
    MS-Off Ver
    Excel 2007
    Posts
    26

    Using IF condition in email body code using VBA

    Hi,

    I am using VB to send mails. I am using html body for the same.

    My problem is that I am unable to use the IF condition within the body of the mail as it throws up a "Compile Error: Expected Expression" message.

    The code is like this, x is some loop parameter:

    . HTMLBody = "<Text>" &  _
    If Sheets(1).Cells(x,2).Value = "Yes" Then
    "Congrats!"
    End If & _
    "<Remaining Text>"
    Can anyone help?

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Using IF condition in email body code using VBA

    Maybe:
    
    If Sheets(1).Cells(x,2).Value = "Yes" Then
        .HTMLBody = "<Text>" & "Congrats!" & "<Remaining Text>"
    Else
        .HTMLBody = "<Text>" & "<Remaining Text>"
    End If
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    11-15-2008
    Location
    ph
    MS-Off Ver
    2007/2010/2016
    Posts
    479

    Re: Using IF condition in email body code using VBA

    Hi -

    or;
    .HTMLBody = "<Text>" & IIf(Sheets(1).Cells(x, 2).Value = "Yes", "Congrats!" & "<Remaining Text>", "<Remaining Text>")
    event

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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