PDA

View Full Version : Solved: Intersect



Opv
07-19-2010, 06:10 PM
I have defined sRng as a range and set the range to a designated range of cells. However, the following receives an error:


If Not Intersect(ActiveCell, Range(sRng)) Is Nothing Then


I have tested sRng and have confirmed that it is a valid range. I can select it and change values within that defined range. Is there something within Intersect that prefers ranges like "A3:A500" as opposed to a named range comprised of those same cells?

Simon Lloyd
07-19-2010, 06:32 PM
replace your code with this:Dim sRng As Range
Set sRng = Range("B2:D10")
If Not Intersect(ActiveCell, sRng) Is Nothing Then
MsgBox ActiveCell.Address
End Ifyou should find that when clicking a cell in the range B2:D10 you will get the cell address, this is because sRng is already defined as a range so no need for Range(sRng), did you define sRng as i did above? or is it a worksheet range?

If its a worksheet defined range then you need this:If Not Intersect(ActiveCell, Range("sRng")) Is Nothing Then
MsgBox ActiveCell.Address
End If

Opv
07-19-2010, 06:44 PM
Thanks.

Opv
07-20-2010, 08:24 AM
Why would I be getting a type mismatch error with the following?


If Not Intersect(ActiveCell, Range("Age").Column) Is Nothing Or _
Not Intersect(ActiveCell, Range("KidDueDate").Column) Is Nothing Or _
Not Intersect(ActiveCell, Range("NextWormDate").Column) Or _
Not Intersect(ActiveCell, Range("Status").Column) Then

ActiveCell.Offset(0, -1).Select
End If

Simon Lloyd
07-20-2010, 10:51 AM
Why are you using .Column?, you don't need it as the intersect is referring to a range with reference to the activecell.

Opv
07-20-2010, 11:30 AM
Why are you using .Column?, you don't need it as the intersect is referring to a range with reference to the activecell.

I see that now. I guess I was braindead at the time. I was originally trying to test for whether the ActiveCell is in either of the four columns. I see now that what I wrote was way off base.

Thanks,

Opv

mdmackillop
07-21-2010, 12:22 AM
I was originally trying to test for whether the ActiveCell is in either of the four columns

To do that, use .EntireColumn (assuming that is not defined already by the range name)


Intersect(ActiveCell, Range("Age").EntireColumn)

Opv
07-21-2010, 05:40 AM
To do that, use .EntireColumn (assuming that is not defined already by the range name)


Intersect(ActiveCell, Range("Age").EntireColumn)


Thanks, as always.

Opv

Opv
07-23-2010, 08:25 AM
Well, I'm having another Intersect problem with the following script.


Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim toSale As Range

Set toSale = Range("ToSale").Offset(1, 0).Resize(gRows - hRow, 1).SpecialCells(xlCellTypeVisible)

If Not Intersect(Target, toSale) Is Nothing Then
' If Range("ToSale").Value = "Select" Then
' Target.Value = "X"
' End If

MsgBox Target.address
End If


I understood that Intersect(Target, Range) tested whether the Target cell (or activecell) was within the designated range. The above script is somehow resulting in Target.address being the same as the toSale range. How could the above be changed so that individual cells within the toSale range are changed to "X" rather than the entire range being changed?

Simon Lloyd
07-23-2010, 10:16 AM
You cannot keep giving us snippets of changed code and expect us to understand whats going on in your head!

You have now shown two other variables gRows and hRow yet they aren't declared or defined, it's about time you supplied a workbook so we can see what you are trying to acheive!

Opv
07-23-2010, 10:30 AM
You cannot keep giving us snippets of changed code and expect us to understand whats going on in your head!

You have now shown two other variables gRows and hRow yet they aren't declared or defined, it's about time you supplied a workbook so we can see what you are trying to acheive!

Sorry about that. This question has nothing to do with my earlier question, except to the extent that it also involves the Intersect statement. I withdraw the question.

Thanks