Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using "OR" in an "IF" statement

Status
Not open for further replies.

mgp

Mechanical
May 30, 2001
224
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

 
Replies continue below

Recommended for you

You made a mistake: you should write
if testselection=44 Or testselection=46 then...
prex
motori@xcalcsREMOVE.com
Online tools for structural design
 
Thanks prex

Its only that I have about 250 numbers (cell adresses) to check.
Is there a shorter code to test this?

Mogens

 
Do you mean that you want to write 250 ORed tests in your code?
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
Online tools for structural design
 
Prex is right

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


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor