Using OR Operator in VBA Code - Problem

B

Bill Foley

Hey Gang,

Back again with another question that has me pulling out the remaining two
hairs.

I have a bunch of bookmarks that I am searching for in a document and
selecting all bookmarks that do not meet that criteria to make subdocuments.
All has been working fine, but now I need to do a search for either of two
bookmark names and delete the others.

I have been trying a couple of variations of OR, but to no avail. The
original code for a given bookmark is as follows:

For Each MyBook In ActiveDocument.Bookmarks
If InStr(1, MyBook.Name, "por_") = 0 Then
MyBook.Range.Delete
End If
Next

This looks for sequenced bookmarks that start with "por_" and deletes
anything but. I want it to look for both "por_" AND "poi_" and delete
anything else. I have tried the following to no avail:

1.
For Each MyBook In ActiveDocument.Bookmarks
If InStr(1, MyBook.Name, "por_""poi_") = 0 Then
MyBook.Range.Delete
End If
Next

2.
For Each MyBook In ActiveDocument.Bookmarks
If InStr(1, MyBook.Name, "por_") = 0 OR InStr(1, MyBook.Name,
"por_") = 0 Then
MyBook.Range.Delete
End If
Next

I have tried a few others that are too embarrassing to post.

Anyone know the correct syntax? TIA!
 
J

Jay Freedman

Bill said:
Hey Gang,

Back again with another question that has me pulling out the
remaining two hairs.

I have a bunch of bookmarks that I am searching for in a document and
selecting all bookmarks that do not meet that criteria to make
subdocuments. All has been working fine, but now I need to do a
search for either of two bookmark names and delete the others.

I have been trying a couple of variations of OR, but to no avail. The
original code for a given bookmark is as follows:

For Each MyBook In ActiveDocument.Bookmarks
If InStr(1, MyBook.Name, "por_") = 0 Then
MyBook.Range.Delete
End If
Next

This looks for sequenced bookmarks that start with "por_" and deletes
anything but. I want it to look for both "por_" AND "poi_" and delete
anything else. I have tried the following to no avail:

1.
For Each MyBook In ActiveDocument.Bookmarks
If InStr(1, MyBook.Name, "por_""poi_") = 0 Then
MyBook.Range.Delete
End If
Next

2.
For Each MyBook In ActiveDocument.Bookmarks
If InStr(1, MyBook.Name, "por_") = 0 OR InStr(1, MyBook.Name,
"por_") = 0 Then
MyBook.Range.Delete
End If
Next

I have tried a few others that are too embarrassing to post.

Anyone know the correct syntax? TIA!

Hi Bill,

Use #2, except replace OR with AND.

#1 won't work because the expression "por_""poi_" is equivalent to the
single string por_"poi_ and that doesn't appear in any bookmark name.

In the original #2, what you're saying is "if por_ doesn't occur OR if poi_
doesn't occur in the same name, then delete". But if por_ does occur in a
particular name, then poi_ is *not* present in that name, and vice versa, so
that condition is always going to be true.

What you want is "if por_ doesn't occur AND if poi_ doesn't occur in the
same name, then delete".
 
A

Andra

it seems (sorry for not using correct vba syntax)
if ...por...= 0 AND ...poi...= 0 then
equal to
if ...por...<> 0 OR ...poi...<> 0 then do nothing else delete


Bill Foley wrote
 
B

Bill Foley

Using AND was one of the options I tried. maybe I got the syntax wrong.
I'll try again. THANKS!
 
B

Bill Foley

Sure enough, it worked! Guess I had something wrong (go figure)! HA! Have
a great weekend!
 
J

Jezebel

There's also the LIKE operator --

If not Left$(MyBookmark.Name,4) like "_po[ir]" then
...
 

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