1

I have 3 If statements that always result in true no matter the value of "dept".
If I remove two of the If statements leaving just one it works. What am I doing wrong?

Dim Dept As String
Dim wb As Workbook

Set wb = ThisWorkbook


Dept = Application.WorksheetFunction.VLookup(Name, Sheet1.Range("F10"), 1)

If Dept = "Dept1" Or "Dept2" Then
wb.SendMail "email address", Subject:="Subject heading 1"

End If

If Dept = "Dept3" Or "Dept4" Then
wb.SendMail "email address", Subject:="Subject heading 2"

End If

If Dept = "Dept5" Or "Dept6" Then
wb.SendMail "email address", Subject:="Subject heading 3"

End If
1
  • I've been programming for > 20 years and just learned something new from you with that Application.WorksheetFunction.VLookup(.... Didn't know the built in formula functions were available like that. Thanks! Commented Sep 25, 2014 at 15:08

3 Answers 3

2

You can't use Or like that here. You have to repeat the variable:

If Dept = "Dept1" Or Dept = "Dept2" Then
wb.SendMail "email address", Subject:="Subject heading 1"

End If

If Dept = "Dept3" Or Dept = "Dept4" Then
wb.SendMail "email address", Subject:="Subject heading 2"

End If

If Dept = "Dept5" Or Dept = "Dept6" Then
wb.SendMail "email address", Subject:="Subject heading 3"

End If

or you could use Select Case:

Select Case Dept
  case "Dept1", "Dept2"
wb.SendMail "email address", Subject:="Subject heading 1"
case "Dept3", "Dept4"
wb.SendMail "email address", Subject:="Subject heading 2"
case "Dept5", "Dept6"
wb.SendMail "email address", Subject:="Subject heading 3"

End Select
Sign up to request clarification or add additional context in comments.

Comments

0

I assume Dept is a String variable.

Then the code should look like:

If Dept = "Dept1" Or Dept = "Dept2" Then
   wb.SendMail "email address", Subject:="Subject heading 1"
End If

VBA interprtates Your boolean Condition as (Dept = "Dept1") or CBool("Dept2") VBA propably has a hard time converting Your String to a boolean.

Comments

0

Did you try adding

If Dept = "Dept1" Or Dept = "Dept2" Then

Probably since the Or is Dept, it is always true. All IF condition get executed

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.