Using "OR" in an "IF" statement
Using "OR" in an "IF" statement
(OP)
Im trying to perform an action depending on the selected cell. Only a number of cells are valid so I want to test if the cell is one of a number of valid cells.
My trial macro goes like this
Public Sub selec()
Row = Application.ActiveCell.Row
col = Application.ActiveCell.Column
testselection = Row & col
If testselection = 44 Or 46 Then
MsgBox ("right selection")
Else
MsgBox ("wrong selection")
End If
End Sub
For some reason the answer is always "right selection" no matter if I select one of the two cells.
Can anyone advice on this or is there another way of testing?
Thanks, Mogens
My trial macro goes like this
Public Sub selec()
Row = Application.ActiveCell.Row
col = Application.ActiveCell.Column
testselection = Row & col
If testselection = 44 Or 46 Then
MsgBox ("right selection")
Else
MsgBox ("wrong selection")
End If
End Sub
For some reason the answer is always "right selection" no matter if I select one of the two cells.
Can anyone advice on this or is there another way of testing?
Thanks, Mogens





RE: Using "OR" in an "IF" statement
if testselection=44 Or testselection=46 then...
prex
motori@xcalcsREMOVE.com
http://www.xcalcs.com
Online tools for structural design
RE: Using "OR" in an "IF" statement
Its only that I have about 250 numbers (cell adresses) to check.
Is there a shorter code to test this?
Mogens
RE: Using "OR" in an "IF" statement
I think you should write the addresses of your cells in a named area, then use one of the lookup functions to check if any given cell is there. This will make your code reasonably flexible and modifiable.
prex
motori@xcalcsREMOVE.com
http://www.xcalcs.com
Online tools for structural design
RE: Using "OR" in an "IF" statement
You feed the range in your code by identifying it and looping through it.
Suppose you have your values in column A
sub dothejob()
dim columnA as range
set columnA=activesheet.columns("a:a")
'which makes the code accesss all values in column A
for each cell in columnA
'insert the code for selec() here
next cell
end sub
Improvement of the code for efficiency is left for your excercise boss
regs
IJR