Testing for a Cell within a range

B

Bruce A. Julseth

I an using a "Worksheet_Change" event. I want to test is "Target" is within
a range of Cells.

My range is K7:p7.

I tried: If (Target.Address(False, False) >= Range("K7").Address(False,
False) _
And Target.Address(False, False) <= Range("P7").Address(False, False))

When I click on a Cell within, K7 and P7, it get a true response. Good.

But when I click on "M8", I also get a true response, Not good.

What am I doing wrong?

Thank you...
 
P

Patrick Molloy

IF Not Intersect(Target, Range("K7:p7") ) Is Nothing Then
'target is inside the range
End If
 
R

Robert Flanagan

Try the following (untestedP

Dim anyR as range
on error resume next
set anyr = intersect(target, range("K7:p7"))
on error goto 0
if anyr is nothing then
'not in range
else
'in range
end if

Its easier to do an Intersect and see if a cell is in a range than other
approaches.

Robert Flanagan
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel
 
M

Mike H

Hi,

Try one of these

For a contiguous range
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then

or a none contiguous range
If Not Intersect(Target, Range("A1,C1,E1")) Is Nothing Then

Mike
 

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