If Not Or looking at Text

December 7, 2010 at 21:15:35
Specs: Windows XP
How can I use an If statement, with not and or conditions, when looking at text in a cell?  I want to call a function if the contents of the cell are not equal to "Atlantis" or "Eldorado".  Here's what I have:

If Not ActiveCell.Value = "Atlantis" Or ActiveCell.Value = "Eldorado" Then
Totals
Else
ActiveCell.Offset(1, 0).Range("A1").Select
End If



See More: If Not Or looking at Text

Report •

#1
December 8, 2010 at 05:17:48
You are missing one word:

Not

Try and figure out where it goes.

Posting Tip: Before posting Data or VBA Code, read this How-To.


Report •

#2
December 8, 2010 at 06:30:10
Before the second ActiveCell. Do I need that .Range("A1") in there if I'm selecting a single cell?

Report •

#3
December 8, 2010 at 07:39:35
Correct on the placement of "Not".

No, you don't need the .Range("A1") to select a cell offset from the ActiveCell. You know, I've seen that syntax before and I don't know where it comes from. Why did you include .Range("A1") in that statement? Just curious.

BTW Rarely do you need to Select a cell in VBA to perform an operation on it. In fact, it's pretty inefficient to do so.

I guess it depends on what you are trying to do. If all you are doing is Selecting the cell when the macro ends so that you can type something into it, I guess it's OK, but if you are performing VBA operations on it and then continuing the code, there is no need to Select it.

e.g.

ActiveCell.Offset(1, 0).Select
Selection = "Hello"

can be replaced with:

ActiveCell.Offset(1, 0) = "Hello"

and

ActiveCell.Offset(1, 0).Select
Selection.Interior.ColorIndex = 3

can be replaced with:

ActiveCell.Offset(1, 0).Interior.ColorIndex = 3

In other words, there is no need to have Excel Select the cell and then have VBA do something with it. We can just let VBA do what it needs to do in the background.

Posting Tip: Before posting Data or VBA Code, read this How-To.


Report •

Related Solutions

#4
December 8, 2010 at 07:50:47
Thank you for the information on the ActiveCell. I got the .Range("A1") from recording a macro, then using most of it's code. Basically I performed the steps I could and used the code. I'm new at this so I realize my code is inefficient. I'm working on that.

Report •

Ask Question