×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Using "OR" in an "IF" statement

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

RE: Using "OR" in an "IF" statement

(OP)
Thanks prex

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

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
http://www.xcalcs.com
Online tools for structural design

RE: Using "OR" in an "IF" statement

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


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources