?
Solved

VBA: Range inside a Range

Posted on 2004-03-30
7
Medium Priority
?
574 Views
Last Modified: 2006-11-17
Hi,
i need to find a way to know if a given range is inside other.
In example:
If my range is "F8" and my main target range is "A1:F7", my range is not inside but if my range is "C4" it is.
It would be done without a loop since that would be called several times in a minute and user shoul never know that this function is performed.
If you need more explanation or knows a better approach, i am open to any tip that can solve this matter.
Thanks in advance.
0
Comment
Question by:Richie_Simonetti
  • 4
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
[ fanpages ] earned 300 total points
ID: 10714188
Hi,

You need to use "Intersect".

Option Explicit
Public Sub Q_20937308()

  Dim objRange                                          As Range
 
  Set objRange = Intersect(Range("A1:F7"), Range("F8"))
 
  If Not (objRange Is Nothing) Then
     MsgBox "Intersects"
  Else
     MsgBox "Does not Intersect"
  End If
 
  Set objRange = Nothing
 
End Sub


BFN,

fp.

0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 10714286
Wow, i never heard that function before!
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 10714308
Many Thanks.
It would be nice if you do a little explanation of how that function works (more than the poor VBA help text ;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 10714413
Well...

Each of the parameters specify ranges in the worksheet and the function (object) is set to the range where all the parameters intersect.

For example,

... = Intersect(Range("A1:Z65535"), Range("B2:G10"), Range("C4:D5"))

will return $C$4:$D$5 as the Address of the Intersect range as this is the range that is present in all three parameter ranges.

Hope that made sense.

Thanks for the points/grading.

BFN,

fp.
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 10714437
many thanks again. I never grade less than an "A", otherwise i directly ask for deletion/PAQ/refund points since if solution works, deserves an "A" otherwise, problem not solved so why to put any grade at all, don't you think so?
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 10721439
Hi,

Sometimes links to sites or previous answers with relevance are posted as comments rather than the code or comment relevant to the question, and therefore they may only be worthy of a "B", but yes, I see your point.

HTH.

BFN,

fp.
[http://NigelLee.info]
0
 
LVL 16

Author Comment

by:Richie_Simonetti
ID: 10723931
To me, it doesn't matters if it is genuine code or a link to it.
If it is a solution to the problem, enougth to me.
I posted links several times when answer questions, many times due to links contains code that not belongs to me so, i don't like to 'stole' code from other programmers and pass them as mine.
Cheers
Have a nice day.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question