MS Excel - Please explain the Target property in the Intersect Function

I found the below formula:

If Intersect(Target,Range(strRange)) IsNothing then Exit Sub

How does the function know to pick up the cell address for Target if the default is the value ?
Can someone explain the mechanics of the Intersect Function.  I have googled it by I am struggling on why you would use this function...
upobDaPlayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:
Hi,

The default property for a Range object is the hidden _Default(RowIndex, ColumnIndex) property (not Value)!  RowIndex & ColumnIndex are both optional here so I am not surprised you may have found the use of Target confusing.

Think of _Default() like the Item method, or Cells property (although this may make an explanation more confusing).  The Item method requires at least one parameter (RowIndex) of the two available (RowIndex, ColumnIndex).  Cells, again has two parameters (RowIndex, ColumnIndex), but both are optional.  With me so far? :)

With the default property, _Default(), use of the Range object without any further qualification is equivalent to Range.Item(1) or Range.Cells(1) & this is the first cell in the range; the default property of that is, as you said, Value.

So, if Target is referring to a single cell (not more than one cell), & you queried the value of Target in the "Immediate" window, you would see the value of this cell:

Debug.Print Target

You would use the code statement you mentioned (actually separating IsNothing, see below) in a worksheet function associated with an event, such as one of these two event code functions within a worksheet code module:

a) Private Sub Worksheet_Change(ByVal Target As Range)
b) Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Alternatively, in a workbook code module:

a) Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
b) Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

For example, the following code would be entered within the code module of a worksheet:

Private Sub Worksheet_Change(ByVal Target As Range)

  Dim strRange                                          As String
  
  strRange = "C3:G7"
  
  If (Intersect(Target, Range(strRange)) Is Nothing) Then
     Exit Sub
  End If
  
  MsgBox "The cell just changed is within the range [" & strRange & "].", _
         vbInformation Or vbOKOnly, _
         ThisWorkbook.Name
         
End Sub

Open in new window


Here, if a cell in the worksheet is changed, the Intersect() method (of the Application object, that is omitted as this is default object in this case) determines if the cell is within an explicit range ([C3:G7]).

If it is, then a Message box is shown.

The Application.Intersect() method returns a Range object & the code is simply checking if the return is a valid Range, or not (Is Nothing).

Simplying the code will, I hope, demonstrate the return object better:

Private Sub Worksheet_Change(ByVal Target As Range)

  MsgBox Intersect(Target, [C3:G7]).Address
  
End Sub

Open in new window


If the cell changed is within the range [G3:G7], then the address of the cell is displayed in a Message box.  However, if the cell changed is outside the range, a run-time error (91) is encountered as the return object from Intersect is Nothing.

Application.Intersect() is used to determine if the address of two (or more) ranges intersect each other.  That is, if all ranges share (at least) one common cell.

One of these ranges could be Target in the above examples, but it need not be.  Intersect() is just commonly used within the routines I mentioned because of the usefulness of determining if a cell has been changed, or selected, during normal operations upon a worksheet.

Another example (not using Target):

  Dim objRange                                          As Range

  Set objRange = Intersect([A:G], [3:7], [D4])
  
  If Not (objRange Is Nothing) Then
     ' [D4] intersects columns [A:G] & rows 3 to 7.
  End If

Open in new window



BFN,

fp.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
upobDaPlayaAuthor Commented:
fp,   this is making sense...I am reviewing your reply and then doing further reading at msdn.  Let me have some more time prior to closing...thx for all the detail and well layed out response !
0
[ fanpages ]IT Services ConsultantCommented:
You're very welcome.

I did consider going further & writing the code example(s) without using the Intersect() statement to show what was happening, but thought that may be overwhelming (initially).

If, after your further research, you are still unclear, & you would like to see that though, please ask.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

upobDaPlayaAuthor Commented:
What you have explained does make sense, but then why does MSDN have this on there site....

.... if an object has a Value property, then this property is the default value for the object. If an object does not have the Value property, then that object does not have a default value.

This was found at the MS Office site

Since the Item property is the default property of the Range object, it is not necessary to explicitly reference it.
0
[ fanpages ]IT Services ConsultantCommented:
If I understand the mismatch you were trying to highlight, I believe this sentence is incorrect:

If an object does not have the Value property, then that object does not have a default value.
0
upobDaPlayaAuthor Commented:
I felt like I had my own private tutor....Great tutorial on Target
0
[ fanpages ]IT Services ConsultantCommented:
:) You're very welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.