ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to test if a range is with in another range? (https://www.excelbanter.com/excel-programming/344501-how-test-if-range-another-range.html)

Mike Mertes

How to test if a range is with in another range?
 
Specifically, I'm using a select...case statement in a Worksheet_change()
procedure to test which cell is being changed by the user. In this example
you can see why my approach isn't suitable:

Select Target.Address(false,false)
Case "D9" to "D30"
DoSomeStuff
End Select

Alphabetically "D30" occurs before "D9" so this test fails.

I could test for each range individually like "D9","D10","D11",etc... but
that's long and sloppy. There must be an easier way to test if a range is
within or between another range? (BTW, I realize this won't work on a
selection across multiple rows and columns. I have accounted for that.)

TIA
-Mike



Ken Johnson

How to test if a range is with in another range?
 
Hi Mike,
Will this work?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target,Range("D9:D30")) is Nothing then
Exit Sub
End If
Do Something
End Sub


Mike Mertes

How to test if a range is with in another range?
 
I believe it will. Thanks for helping me expand my VB vocabulary :D

"Ken Johnson" wrote in message
oups.com...
Hi Mike,
Will this work?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target,Range("D9:D30")) is Nothing then
Exit Sub
End If
Do Something
End Sub




Ken Johnson

How to test if a range is with in another range?
 
Mike,
Thanks for the feedback and you're welcome. I'm glad I could help.
Ken Johnson



All times are GMT +1. The time now is 05:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com