NOT Operator

D

Drake

Hi again -

I am trying to use the NOT operator to perform an action if it finds that
text in a certain cell is different than stated. What am I doing wrong?

--------------------------

If ComboTest.Value = "Test1" And _
rcell1.Text = Not "Test1" Then

MsgBox "Wrong!"
 
G

Greg Maxey

Drake,

I don't think you need the NOT operator. Try:

If ComboTest.Value = "Test1" And rcell1.Text <> "Test1" Then
MsgBox "Wrong!"

I tested the follwing in a Word table and it worked fine:

Sub Test()

Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)

If Left(oTbl.Cell(1, 1).Range, Len(oTbl.Cell(1, 1).Range) - 2) _
= "Test1" And _
Left(oTbl.Cell(1, 2).Range, Len(oTbl.Cell(1, 2).Range) - 2) _
<> "Test1" Then

MsgBox "Wrong"

End If

End Sub
 
J

Jezebel

NOT is a unary operator that returns the logical opposite of what follows.
Its argument must be something that VBA can evaluate as a boolean (ie a
logical value or a number) and the result is always a boolean. You could use

If ComboTest.Value = "Test1" And Not rcell1.Text = "Test1" then ....

because (rcell1.Text = "Test1") evaluates to true or false.


But Greg's suggestion is better.
 
D

Drake

Greg -

this is perfect! Thank you!


Greg Maxey said:
Drake,

I don't think you need the NOT operator. Try:

If ComboTest.Value = "Test1" And rcell1.Text <> "Test1" Then
MsgBox "Wrong!"

I tested the follwing in a Word table and it worked fine:

Sub Test()

Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(1)

If Left(oTbl.Cell(1, 1).Range, Len(oTbl.Cell(1, 1).Range) - 2) _
= "Test1" And _
Left(oTbl.Cell(1, 2).Range, Len(oTbl.Cell(1, 2).Range) - 2) _
<> "Test1" Then

MsgBox "Wrong"

End If

End Sub
 
D

Drake

Greg,

I am getting a Code 424 Error - Object Required. I think I did exactly as
you specified. It appeared to work when the text was one word only, but it
is having problems with the multiple words. Any thoughts?
------------------
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables(2)

Combobox.Value = "test1" And _
Left(oTbl.Cell(1, 4).Range, Len(oTbl.Cell(1,4).Range) - 2) <> "This is
another test" Or _

Combobox.Value = "test2" And _
Left(oTbl.Cell(1, 4).Range, Len(oTbl.Cell(1,4).Range) - 2) <> "This is
another test 2" Or _
 
G

Greg

Drake,

The only thing obvious is that you code appears to be missing the "IF"
condition.

I don't have a document with a Combobox so it is hard to help figure
out what might be going on.

Sorry.
 
D

Drake

Okay - did some more testing and was able to get beyond the error. Now, it
just keeps acting as if the two don't match, even when they do.
 

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