#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default intersect

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default intersect

You have these declared as type Range:

Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")

but the construction of the set statement does not appear to equate to a
range reference, but I can't be sure because "estsubvenrng" and the other two
are not declared anywhere. Also, costid in this code could be a variable
declared on the fly or it could be a range name on the worksheet, again
undetermined by info provided.

"ranswert" wrote:

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default intersect

estsubvenrng and the other two are named ranges in my worksheet. Costid is
part of the name. For example: when costid = cst01 it refers to the named
range "cst01estsubvenrng". cst01 identifies which "estsubvenrng" to look at.
I am trying to make sure that a procedure doesn't execute unless a cell in
one of the three ranges is selected.

"JLGWhiz" wrote:

You have these declared as type Range:

Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")

but the construction of the set statement does not appear to equate to a
range reference, but I can't be sure because "estsubvenrng" and the other two
are not declared anywhere. Also, costid in this code could be a variable
declared on the fly or it could be a range name on the worksheet, again
undetermined by info provided.

"ranswert" wrote:

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default intersect

Ok, for the Set isect statement to return a true value, one of the arguments
must intersect with one of the other arguments. So if the three arguments
that you are using are ranges that parallel each other and do not cross at
some point, you will get a nothing. I am trying to determine exactly what
you want to return to suggest what other argument to add to the Intersect
function, but there is not enough info to do that. But maybe you can figure
that out.

"ranswert" wrote:

estsubvenrng and the other two are named ranges in my worksheet. Costid is
part of the name. For example: when costid = cst01 it refers to the named
range "cst01estsubvenrng". cst01 identifies which "estsubvenrng" to look at.
I am trying to make sure that a procedure doesn't execute unless a cell in
one of the three ranges is selected.

"JLGWhiz" wrote:

You have these declared as type Range:

Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")

but the construction of the set statement does not appear to equate to a
range reference, but I can't be sure because "estsubvenrng" and the other two
are not declared anywhere. Also, costid in this code could be a variable
declared on the fly or it could be a range name on the worksheet, again
undetermined by info provided.

"ranswert" wrote:

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default intersect

P.S. In the last Message Box you should use the variable isect instead of
"Intersect" to return the value of the Intersect.

"ranswert" wrote:

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default intersect

After further review, it looks like the original ActiveCell is the cell you
want to check, so if you add that to your Intersect arguments, it should give
you a return.

Set isect = Application.Intersect(ActiveCell, rng1, rng2, rng3)


"ranswert" wrote:

estsubvenrng and the other two are named ranges in my worksheet. Costid is
part of the name. For example: when costid = cst01 it refers to the named
range "cst01estsubvenrng". cst01 identifies which "estsubvenrng" to look at.
I am trying to make sure that a procedure doesn't execute unless a cell in
one of the three ranges is selected.

"JLGWhiz" wrote:

You have these declared as type Range:

Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")

but the construction of the set statement does not appear to equate to a
range reference, but I can't be sure because "estsubvenrng" and the other two
are not declared anywhere. Also, costid in this code could be a variable
declared on the fly or it could be a range name on the worksheet, again
undetermined by info provided.

"ranswert" wrote:

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default intersect

So it could be in one of named ranges or any two of the named ranges or all
three of the named ranges (if those names overlap)??

You really don't care which range the activecell is in--you just want to know if
it's in one of them?

Option Explicit
Sub intersct()

Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim CostId As String

CostId = "hi"

With ActiveSheet
Set rng1 = .Range(CostId & "estsubvenrng")
Set rng2 = .Range(CostId & "estdescriptrng")
Set rng3 = .Range(CostId & "estamountrng")
End With

If Intersect(ActiveCell, rng1) Is Nothing _
And Intersect(ActiveCell, rng2) Is Nothing _
And Intersect(ActiveCell, rng3) Is Nothing Then
MsgBox "Not in any of the 3 ranges!"
Else
MsgBox "activecell is in at least one of those ranges!"
End If

End Sub

You could also use:

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then
MsgBox "Not in any of the 3 ranges!"
Else
MsgBox "activecell is in at least one of those ranges!"
End If

ranswert wrote:

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks


--

Dave Peterson
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default intersect

If Intersect(ActiveCell, rng1) Is Nothing _
And Intersect(ActiveCell, rng2) Is Nothing _
And Intersect(ActiveCell, rng3) Is Nothing Then


I've not had much occasion to use these functions, but I'm guessing from
their help file write that the above could be replaced with this...

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then

Rick

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default intersect

The help file is misleading in that it states that you can have two or more
range arguments. You can, so long as they have a common intersection.
Otherwise, you get a block variable not set message and the Intersect
variable equals nothing.
This is one that is useful in worksheet change events for a cell in a range
of cells, but gets a little unweildly outside of that.

"Rick Rothstein (MVP - VB)" wrote:

If Intersect(ActiveCell, rng1) Is Nothing _
And Intersect(ActiveCell, rng2) Is Nothing _
And Intersect(ActiveCell, rng3) Is Nothing Then


I've not had much occasion to use these functions, but I'm guessing from
their help file write that the above could be replaced with this...

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then

Rick


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default intersect

Excellent point.

That's why I included this portion at the end:

You could also use:

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then
MsgBox "Not in any of the 3 ranges!"
Else
MsgBox "activecell is in at least one of those ranges!"
End If

Or did you just copy that from my post??? <vbg

"Rick Rothstein (MVP - VB)" wrote:

If Intersect(ActiveCell, rng1) Is Nothing _
And Intersect(ActiveCell, rng2) Is Nothing _
And Intersect(ActiveCell, rng3) Is Nothing Then


I've not had much occasion to use these functions, but I'm guessing from
their help file write that the above could be replaced with this...

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then

Rick


--

Dave Peterson


  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default intersect

LOL

Note to self: You paid for a mouse with a scroll wheel... you should try
using the damn thing every now and then.

Rick


"Dave Peterson" wrote in message
...
Excellent point.

That's why I included this portion at the end:

You could also use:

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then
MsgBox "Not in any of the 3 ranges!"
Else
MsgBox "activecell is in at least one of those ranges!"
End If

Or did you just copy that from my post??? <vbg

"Rick Rothstein (MVP - VB)" wrote:

If Intersect(ActiveCell, rng1) Is Nothing _
And Intersect(ActiveCell, rng2) Is Nothing _
And Intersect(ActiveCell, rng3) Is Nothing Then


I've not had much occasion to use these functions, but I'm guessing from
their help file write that the above could be replaced with this...

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then

Rick


--

Dave Peterson


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default intersect

I know the feeling. I hate when I respond to a message and then scroll down to
see 18 other, er, better, replies.

But you should get a mouse with a scroll wheel! <gd&r

"Rick Rothstein (MVP - VB)" wrote:

LOL

Note to self: You paid for a mouse with a scroll wheel... you should try
using the damn thing every now and then.

Rick

"Dave Peterson" wrote in message
...
Excellent point.

That's why I included this portion at the end:

You could also use:

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then
MsgBox "Not in any of the 3 ranges!"
Else
MsgBox "activecell is in at least one of those ranges!"
End If

Or did you just copy that from my post??? <vbg

"Rick Rothstein (MVP - VB)" wrote:

If Intersect(ActiveCell, rng1) Is Nothing _
And Intersect(ActiveCell, rng2) Is Nothing _
And Intersect(ActiveCell, rng3) Is Nothing Then

I've not had much occasion to use these functions, but I'm guessing from
their help file write that the above could be replaced with this...

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then

Rick


--

Dave Peterson


--

Dave Peterson
  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default intersect

Thanks for all the help. I know where the mistake was.

"Dave Peterson" wrote:

So it could be in one of named ranges or any two of the named ranges or all
three of the named ranges (if those names overlap)??

You really don't care which range the activecell is in--you just want to know if
it's in one of them?

Option Explicit
Sub intersct()

Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range
Dim CostId As String

CostId = "hi"

With ActiveSheet
Set rng1 = .Range(CostId & "estsubvenrng")
Set rng2 = .Range(CostId & "estdescriptrng")
Set rng3 = .Range(CostId & "estamountrng")
End With

If Intersect(ActiveCell, rng1) Is Nothing _
And Intersect(ActiveCell, rng2) Is Nothing _
And Intersect(ActiveCell, rng3) Is Nothing Then
MsgBox "Not in any of the 3 ranges!"
Else
MsgBox "activecell is in at least one of those ranges!"
End If

End Sub

You could also use:

If Intersect(ActiveCell, Union(rng1, rng2, rng3)) Is Nothing Then
MsgBox "Not in any of the 3 ranges!"
Else
MsgBox "activecell is in at least one of those ranges!"
End If

ranswert wrote:

I have been trying to write a procedure that checks if a selected cell is in
a range of cells.
I wrote the following code:

Sub intersct()
Dim xcell As Range
Dim a As String
Dim rng1 As Range
Dim rng2 As Range
Dim rng3 As Range

Set xcell = ActiveCell.EntireRow.Cells(1)
a = xcell.Text
Do While a = ""
Set xcell = xcell.Offset(-1, 0)
a = xcell.Text
Loop
costid = a
MsgBox (costid)
Set rng1 = Range(costid & "estsubvenrng")
Set rng2 = Range(costid & "estdescriptrng")
Set rng3 = Range(costid & "estamountrng")
Set isect = Application.Intersect(rng1, rng2, rng3)
If isect Is Nothing Then
MsgBox "nothing"
Else
MsgBox "Intersect"
End If



End Sub

When I run it I always get msgbox "Nothing" even when the selected cell is
within the range of cells.

What am I doing wrong?
Thanks


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Intersect Arne Hegefors Excel Programming 1 July 25th 06 09:38 AM
Intersect [email protected] Excel Programming 2 July 19th 06 10:41 PM
Intersect Formula??? scrabtree[_2_] Excel Programming 3 October 15th 03 08:30 PM
Intersect Formula VBA Help scrabtree Excel Programming 1 October 15th 03 02:08 PM
Help with If Not Intersect derek Excel Programming 6 July 11th 03 04:39 PM


All times are GMT +1. The time now is 01:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"