Help streamlining VBA code

L

LDMueller

I'm familiar with VBA coding, but sometimes mine is not a simple as it should
be.

Can someone help me streamline this code and get it working?

If BlankSincerely = False And BlankRegards = False And ElecSincerely = False
And ElecRegards = False And PTOSign = False And _
Enclosure = True Or Enclosures = True Or Attachment = True Or Attachments =
True Then
MsgBox "You cannot have an Enclosure or Attachment without a Signature
Block"
GoTo Exit_cmdOK_Click:
Else
End If

Any assistance or comments would be greatly appreciated.

Thanks,

LDMueller
 
G

Graham Mayor

If BlankSincerely = False _
And BlankRegards = False _
And ElecSincerely = False _
And ElecRegards = False _
And PTOSign = False Then
If Enclosure = True _
Or Enclosures = True _
Or Attachment = True _
Or Attachments = True Then
MsgBox "You cannot have an Enclosure or Attachment without a
Signature Block"
GoTo Exit_cmdOK_Click:
End If
End If


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

Jonathan West

Can be shortened a bit further

If Not (BlankSincerely Or BlankRegards Or ElecSincerely Or ElecRegards Or
PTOSign) Then
If Enclosure Or Enclosures Or Attachment Or Attachments Then
MsgBox "You cannot have an Enclosure or Attachment without a
Signature Block"
GoTo Exit_cmdOK_Click:
End If
End If

The thing to remember is that the expression in an If-Then command has to
evaluate to True or False. BlankSincerely is already (I presume) a boolean
variable, so (BlankSincerely = True) is always going to have the same value
as BlankSincerely. The same applies to all the others.

the first of the If-Then commands could also have been written like this

If Not BlankSincerely And Not BlankRegards And Not ElecSincerely And Not
ElecRegards And Not PTOSign) Then

Again, because BlankSincerely is boolean, Not BlankSincerely will always
evaluate to the same as BlankSincerely = False, and is quicker to write. But
if you have a bunch of variables, and you want to know if they are all
false, you can do that by successive And Not operations, but it is easier to
see if any of them are true, and then act accordingly. You can do that by
using Or on them all. You can then invert the overall result by putting
parentheses around the lot and then applying a Not to the whole expression.

In fact, the two IF-Then commands can be combined into one, like this

If Not (BlankSincerely Or BlankRegards Or ElecSincerely Or ElecRegards Or
PTOSign) _
And (Enclosure Or Enclosures Or Attachment Or Attachments) Then

MsgBox "You cannot have an Enclosure or Attachment without a Signature
Block"
GoTo Exit_cmdOK_Click:

End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top