Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Intersect Function Problem

I'm trying to run this function but I'm getting an error witht the
Intersect function for my second worksheet. Any idea why?!

Function SelectRangeToChange(X As Range, ToFind1 As String, ToFind2 As
String, TabNum As Variant) As Range
Dim Selection As Range, Cell As Range, ToChange1 As Range,
ToChange2 As Range, ToChange3 As Range
For i = 1 To TabNum Step 1
Worksheets(i).Select
Set Selection = Intersect(X, ActiveSheet.UsedRange)
***************PROBLEM******
For Each Cell In Selection
If (((Cell.Value) = ToFind1) Or (InStr(1, Cell.Value,
ToFind1, vbTextCompare))) Then
If ToChange1 Is Nothing Then
Set ToChange1 = Cell
Else: Set ToChange1 = Union(ToChange1, Cell)
End If
ElseIf (((Cell.Value) = ToFind2) Or (InStr(1, Cell.Value,
ToFind2, vbTextCompare))) Then
If ToChange2 Is Nothing Then
Set ToChange2 = Cell
Else: Set ToChange2 = Union(ToChange2, Cell)
End If
Else
If ToChange3 Is Nothing Then
Set ToChange3 = Cell
Else: Set ToChange3 = Union(ToChange3, Cell)
End If
End If
Next Cell
Next i
End Function

Thanks,

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Intersect Function Problem

For something like:
Intersect(X, ActiveSheet.UsedRange).Select may be Nothing!
--
Gary''s Student


" wrote:

I'm trying to run this function but I'm getting an error witht the
Intersect function for my second worksheet. Any idea why?!

Function SelectRangeToChange(X As Range, ToFind1 As String, ToFind2 As
String, TabNum As Variant) As Range
Dim Selection As Range, Cell As Range, ToChange1 As Range,
ToChange2 As Range, ToChange3 As Range
For i = 1 To TabNum Step 1
Worksheets(i).Select
Set Selection = Intersect(X, ActiveSheet.UsedRange)
***************PROBLEM******
For Each Cell In Selection
If (((Cell.Value) = ToFind1) Or (InStr(1, Cell.Value,
ToFind1, vbTextCompare))) Then
If ToChange1 Is Nothing Then
Set ToChange1 = Cell
Else: Set ToChange1 = Union(ToChange1, Cell)
End If
ElseIf (((Cell.Value) = ToFind2) Or (InStr(1, Cell.Value,
ToFind2, vbTextCompare))) Then
If ToChange2 Is Nothing Then
Set ToChange2 = Cell
Else: Set ToChange2 = Union(ToChange2, Cell)
End If
Else
If ToChange3 Is Nothing Then
Set ToChange3 = Cell
Else: Set ToChange3 = Union(ToChange3, Cell)
End If
End If
Next Cell
Next i
End Function

Thanks,


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Intersect Function Problem

first I wouldn't define selection as a variable.

It is already defined for another purpose in Excel/VBA.

second, If you try to do intersect on ranges on different sheets, you will
get an error.
? intersect(worksheets(2).Columns(1),worksheets(2).R ows(1)) is nothing
False
' works, but changing to check two different sheets
? intersect(worksheets(1).Columns(1),worksheets(2).R ows(1)) is nothing

raises an error.

--
Regards,
Tom Ogilvy




--
Regards,
Tom Ogilvy



" wrote:

I'm trying to run this function but I'm getting an error witht the
Intersect function for my second worksheet. Any idea why?!

Function SelectRangeToChange(X As Range, ToFind1 As String, ToFind2 As
String, TabNum As Variant) As Range
Dim Selection As Range, Cell As Range, ToChange1 As Range,
ToChange2 As Range, ToChange3 As Range
For i = 1 To TabNum Step 1
Worksheets(i).Select
Set Selection = Intersect(X, ActiveSheet.UsedRange)
***************PROBLEM******
For Each Cell In Selection
If (((Cell.Value) = ToFind1) Or (InStr(1, Cell.Value,
ToFind1, vbTextCompare))) Then
If ToChange1 Is Nothing Then
Set ToChange1 = Cell
Else: Set ToChange1 = Union(ToChange1, Cell)
End If
ElseIf (((Cell.Value) = ToFind2) Or (InStr(1, Cell.Value,
ToFind2, vbTextCompare))) Then
If ToChange2 Is Nothing Then
Set ToChange2 = Cell
Else: Set ToChange2 = Union(ToChange2, Cell)
End If
Else
If ToChange3 Is Nothing Then
Set ToChange3 = Cell
Else: Set ToChange3 = Union(ToChange3, Cell)
End If
End If
Next Cell
Next i
End Function

Thanks,


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 644
Default Intersect Function Problem

Kathy, from what I can tell you are passing a range to this function,
then changing sheets and asking for an intersect. From what I
understand from Tom, Range is an object and must have a parent object,
which is the Sheet on which the Range resides, so in affect, you are
asking for the Intersection of the original sheet, and the second sheet
which does not exist. Perhaps you could look for the intersection of
Range(x.Address).

HTH

Charles Chickering
wrote:
I'm trying to run this function but I'm getting an error witht the
Intersect function for my second worksheet. Any idea why?!

Function SelectRangeToChange(X As Range, ToFind1 As String, ToFind2 As
String, TabNum As Variant) As Range
Dim Selection As Range, Cell As Range, ToChange1 As Range,
ToChange2 As Range, ToChange3 As Range
For i = 1 To TabNum Step 1
Worksheets(i).Select
Set Selection = Intersect(X, ActiveSheet.UsedRange)
***************PROBLEM******
For Each Cell In Selection
If (((Cell.Value) = ToFind1) Or (InStr(1, Cell.Value,
ToFind1, vbTextCompare))) Then
If ToChange1 Is Nothing Then
Set ToChange1 = Cell
Else: Set ToChange1 = Union(ToChange1, Cell)
End If
ElseIf (((Cell.Value) = ToFind2) Or (InStr(1, Cell.Value,
ToFind2, vbTextCompare))) Then
If ToChange2 Is Nothing Then
Set ToChange2 = Cell
Else: Set ToChange2 = Union(ToChange2, Cell)
End If
Else
If ToChange3 Is Nothing Then
Set ToChange3 = Cell
Else: Set ToChange3 = Union(ToChange3, Cell)
End If
End If
Next Cell
Next i
End Function

Thanks,


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Intersect Function Problem

Thanks guys!!



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Intersect Function Problem

This at least works - don't know how what you are really doing.

Function SelectRangeToChange(X As Range, _
ToFind1 As String, ToFind2 As String, _
TabNum As Variant) As Range
Dim Cell As Range, ToChange1 As Range
Dim ToChange2 As Range, ToChange3 As Range
Dim rng As Range
Set sh = X.Parent
Set rng = Intersect(X, sh.UsedRange)
For Each Cell In rng
If InStr(1, Cell.Value, ToFind1, vbTextCompare) Then
If ToChange1 Is Nothing Then
Set ToChange1 = Cell
Else
Set ToChange1 = Union(ToChange1, Cell)
End If
ElseIf InStr(1, Cell.Value, ToFind2, vbTextCompare) Then
If ToChange2 Is Nothing Then
Set ToChange2 = Cell
Else
Set ToChange2 = Union(ToChange2, Cell)
End If
Else
If ToChange3 Is Nothing Then
Set ToChange3 = Cell
Else: Set ToChange3 = Union(ToChange3, Cell)
End If
End If
Next Cell
' Set SelectRangeToChange = What? what do you want to return
' dummy line to display results in immediate window
Debug.Print ToChange1.Address
Debug.Print ToChange2.Address
Debug.Print ToChange3.Address
End Function

' used to test the above

Sub Tester1()
SelectRangeToChange Worksheets("Sheet1"). _
Range("A:E"), "A", "B", 5
End Sub

-' produced
$A$1,$C$5,$D$6:$D$8,$C$9:$D$9,$D$10,$D$12,$C$14
$B$1,$A$2,$E$12
$C$1:$E$1,$B$2:$E$2,$A$3:$E$4,$D$5:$E$5,$C$6:$C$8, $E$6:$E$10,$D$11:$E$11,$C$10:$C$13,$A$5:$B$14,$D$1 3:$E$14

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote:

first I wouldn't define selection as a variable.

It is already defined for another purpose in Excel/VBA.

second, If you try to do intersect on ranges on different sheets, you will
get an error.
? intersect(worksheets(2).Columns(1),worksheets(2).R ows(1)) is nothing
False
' works, but changing to check two different sheets
? intersect(worksheets(1).Columns(1),worksheets(2).R ows(1)) is nothing

raises an error.

--
Regards,
Tom Ogilvy




--
Regards,
Tom Ogilvy



" wrote:

I'm trying to run this function but I'm getting an error witht the
Intersect function for my second worksheet. Any idea why?!

Function SelectRangeToChange(X As Range, ToFind1 As String, ToFind2 As
String, TabNum As Variant) As Range
Dim Selection As Range, Cell As Range, ToChange1 As Range,
ToChange2 As Range, ToChange3 As Range
For i = 1 To TabNum Step 1
Worksheets(i).Select
Set Selection = Intersect(X, ActiveSheet.UsedRange)
***************PROBLEM******
For Each Cell In Selection
If (((Cell.Value) = ToFind1) Or (InStr(1, Cell.Value,
ToFind1, vbTextCompare))) Then
If ToChange1 Is Nothing Then
Set ToChange1 = Cell
Else: Set ToChange1 = Union(ToChange1, Cell)
End If
ElseIf (((Cell.Value) = ToFind2) Or (InStr(1, Cell.Value,
ToFind2, vbTextCompare))) Then
If ToChange2 Is Nothing Then
Set ToChange2 = Cell
Else: Set ToChange2 = Union(ToChange2, Cell)
End If
Else
If ToChange3 Is Nothing Then
Set ToChange3 = Cell
Else: Set ToChange3 = Union(ToChange3, Cell)
End If
End If
Next Cell
Next i
End Function

Thanks,


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 [email protected] Excel Programming 2 July 19th 06 10:41 PM
intersect function problem pumpbhoy Excel Programming 2 February 16th 05 08:19 AM
how to get the intersect cell data out using vba or function mango Excel Worksheet Functions 1 December 30th 04 09:46 AM
Opposite of Intersect function - an example DataFreakFromUtah Excel Programming 1 September 17th 04 11:30 PM
Syntax problem with" intersect" Imbecill[_2_] Excel Programming 2 May 29th 04 06:38 PM


All times are GMT +1. The time now is 11:36 PM.

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

About Us

"It's about Microsoft Excel"