Solved

VBA: Range inside a Range

Posted on 2004-03-30
7
536 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 75 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS Access Search and Replace Using VBA 6 71
Hide vba in gp 7 94
Set email body to html using vbscript 6 41
Automatic Email Reminder 4 37
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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