Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default help with inner loop to search for matching strings

I have a string in each cell in column R. I want to see if there is a match
in each cell in column J. In column J there are one or more strings
delimited by a comma.
I am having trouble with the inner loop how to set the stringsearch. IF
there is a match I want to mark it in red and copy it to a column in another
sheet. tia,


Sub SDelete()
Dim ColumnJ As Range
Dim ColumnR As Range
Dim stringToSearch As String

Dim I As Integer
Dim C As Cell

Set J = ActiveSheet.Range("J2:end.xl(down)")

Set R = ActiveSheet.Range("R2:end.xl(down)")


I = 2

For Each C In ColumnR

'compare cell in ColumnR with each string in columnJ deliminated by commas
Set C = ("R" & I)


'compare cell in ColumnR with each string in ColumnJ
For Each stringToSearch In ColumnJ
Set stringToSearch = XXXXX
If stringToSearch = C Then

'if it matches color font red
C.Font.ColorIndex = 3
C.Copy Destination:=Worksheets("Sheet2").Range("5")

Else
Next




End If
I = I + 1


Next C


End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default help with inner loop to search for matching strings

Here is some code for you. Instead of traversing column J looking for the
values in R it uses the Find function which will be a pile more efficient.
Here is how it works It traverses thour the cells in Column R and passes the
cell and the range to be searched (Column J) to the function FoundCells. the
function FoundCells performs the Search and returns a Range object made up of
all of the cells that it found. That range object is coloured red and copied
to sheet 2...

Sub FindStuff()
Dim rngStringsToFind As Range
Dim rng As Range
Dim rngToSearch As Range
Dim rngFoundCells As Range

Set rngStringsToFind = Range(Range("R2"), Cells(Rows.Count,
"R").End(xlUp))
Set rngToSearch = Columns("J")

For Each rng In rngStringsToFind
Set rngFoundCells = FoundCells(rngToSearch, rng)
If Not rngFoundCells Is Nothing Then
rngFoundCells.Font.ColorIndex = 3
rngFoundCells.Copy _
Destination:=Worksheets("Sheet2").Cells(Rows.Count ,
"A").End(xlUp).Offset(1, 0)
Set rngFoundCells = Nothing
End If
Next rng
End Sub

Private Function FoundCells(ByVal rngToSearch As Range, _
ByVal rngStringToFind As Range) As Range
Dim rngFound As Range
Dim strFirstAddress As String

Set FoundCells = Nothing
Set rngFound = rngToSearch.Find(What:=rngStringToFind.Value, _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
MatchCase:=False)
If Not rngFound Is Nothing Then
strFirstAddress = rngFound.Address
Set FoundCells = rngFound
Do
Set FoundCells = Union(rngFound, FoundCells)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
End If

End Function
--
HTH...

Jim Thomlinson


"Janis" wrote:

I have a string in each cell in column R. I want to see if there is a match
in each cell in column J. In column J there are one or more strings
delimited by a comma.
I am having trouble with the inner loop how to set the stringsearch. IF
there is a match I want to mark it in red and copy it to a column in another
sheet. tia,


Sub SDelete()
Dim ColumnJ As Range
Dim ColumnR As Range
Dim stringToSearch As String

Dim I As Integer
Dim C As Cell

Set J = ActiveSheet.Range("J2:end.xl(down)")

Set R = ActiveSheet.Range("R2:end.xl(down)")


I = 2

For Each C In ColumnR

'compare cell in ColumnR with each string in columnJ deliminated by commas
Set C = ("R" & I)


'compare cell in ColumnR with each string in ColumnJ
For Each stringToSearch In ColumnJ
Set stringToSearch = XXXXX
If stringToSearch = C Then

'if it matches color font red
C.Font.ColorIndex = 3
C.Copy Destination:=Worksheets("Sheet2").Range("5")

Else
Next




End If
I = I + 1


Next C


End Sub

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default help with inner loop to search for matching strings

this was very urgent, thanks for the rescue.

"Jim Thomlinson" wrote:

Here is some code for you. Instead of traversing column J looking for the
values in R it uses the Find function which will be a pile more efficient.
Here is how it works It traverses thour the cells in Column R and passes the
cell and the range to be searched (Column J) to the function FoundCells. the
function FoundCells performs the Search and returns a Range object made up of
all of the cells that it found. That range object is coloured red and copied
to sheet 2...

Sub FindStuff()
Dim rngStringsToFind As Range
Dim rng As Range
Dim rngToSearch As Range
Dim rngFoundCells As Range

Set rngStringsToFind = Range(Range("R2"), Cells(Rows.Count,
"R").End(xlUp))
Set rngToSearch = Columns("J")

For Each rng In rngStringsToFind
Set rngFoundCells = FoundCells(rngToSearch, rng)
If Not rngFoundCells Is Nothing Then
rngFoundCells.Font.ColorIndex = 3
rngFoundCells.Copy _
Destination:=Worksheets("Sheet2").Cells(Rows.Count ,
"A").End(xlUp).Offset(1, 0)
Set rngFoundCells = Nothing
End If
Next rng
End Sub

Private Function FoundCells(ByVal rngToSearch As Range, _
ByVal rngStringToFind As Range) As Range
Dim rngFound As Range
Dim strFirstAddress As String

Set FoundCells = Nothing
Set rngFound = rngToSearch.Find(What:=rngStringToFind.Value, _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
MatchCase:=False)
If Not rngFound Is Nothing Then
strFirstAddress = rngFound.Address
Set FoundCells = rngFound
Do
Set FoundCells = Union(rngFound, FoundCells)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
End If

End Function
--
HTH...

Jim Thomlinson


"Janis" wrote:

I have a string in each cell in column R. I want to see if there is a match
in each cell in column J. In column J there are one or more strings
delimited by a comma.
I am having trouble with the inner loop how to set the stringsearch. IF
there is a match I want to mark it in red and copy it to a column in another
sheet. tia,


Sub SDelete()
Dim ColumnJ As Range
Dim ColumnR As Range
Dim stringToSearch As String

Dim I As Integer
Dim C As Cell

Set J = ActiveSheet.Range("J2:end.xl(down)")

Set R = ActiveSheet.Range("R2:end.xl(down)")


I = 2

For Each C In ColumnR

'compare cell in ColumnR with each string in columnJ deliminated by commas
Set C = ("R" & I)


'compare cell in ColumnR with each string in ColumnJ
For Each stringToSearch In ColumnJ
Set stringToSearch = XXXXX
If stringToSearch = C Then

'if it matches color font red
C.Font.ColorIndex = 3
C.Copy Destination:=Worksheets("Sheet2").Range("5")

Else
Next




End If
I = I + 1


Next C


End Sub

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default help with inner loop to search for matching strings

Jim
That really got me out of hot water. I do okay if I don't have any last
minute requests. wow thanks again. Janis

"Jim Thomlinson" wrote:

Here is some code for you. Instead of traversing column J looking for the
values in R it uses the Find function which will be a pile more efficient.
Here is how it works It traverses thour the cells in Column R and passes the
cell and the range to be searched (Column J) to the function FoundCells. the
function FoundCells performs the Search and returns a Range object made up of
all of the cells that it found. That range object is coloured red and copied
to sheet 2...

Sub FindStuff()
Dim rngStringsToFind As Range
Dim rng As Range
Dim rngToSearch As Range
Dim rngFoundCells As Range

Set rngStringsToFind = Range(Range("R2"), Cells(Rows.Count,
"R").End(xlUp))
Set rngToSearch = Columns("J")

For Each rng In rngStringsToFind
Set rngFoundCells = FoundCells(rngToSearch, rng)
If Not rngFoundCells Is Nothing Then
rngFoundCells.Font.ColorIndex = 3
rngFoundCells.Copy _
Destination:=Worksheets("Sheet2").Cells(Rows.Count ,
"A").End(xlUp).Offset(1, 0)
Set rngFoundCells = Nothing
End If
Next rng
End Sub

Private Function FoundCells(ByVal rngToSearch As Range, _
ByVal rngStringToFind As Range) As Range
Dim rngFound As Range
Dim strFirstAddress As String

Set FoundCells = Nothing
Set rngFound = rngToSearch.Find(What:=rngStringToFind.Value, _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
MatchCase:=False)
If Not rngFound Is Nothing Then
strFirstAddress = rngFound.Address
Set FoundCells = rngFound
Do
Set FoundCells = Union(rngFound, FoundCells)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
End If

End Function
--
HTH...

Jim Thomlinson


"Janis" wrote:

I have a string in each cell in column R. I want to see if there is a match
in each cell in column J. In column J there are one or more strings
delimited by a comma.
I am having trouble with the inner loop how to set the stringsearch. IF
there is a match I want to mark it in red and copy it to a column in another
sheet. tia,


Sub SDelete()
Dim ColumnJ As Range
Dim ColumnR As Range
Dim stringToSearch As String

Dim I As Integer
Dim C As Cell

Set J = ActiveSheet.Range("J2:end.xl(down)")

Set R = ActiveSheet.Range("R2:end.xl(down)")


I = 2

For Each C In ColumnR

'compare cell in ColumnR with each string in columnJ deliminated by commas
Set C = ("R" & I)


'compare cell in ColumnR with each string in ColumnJ
For Each stringToSearch In ColumnJ
Set stringToSearch = XXXXX
If stringToSearch = C Then

'if it matches color font red
C.Font.ColorIndex = 3
C.Copy Destination:=Worksheets("Sheet2").Range("5")

Else
Next




End If
I = I + 1


Next C


End Sub

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default help with inner loop to search for matching strings

Glad to help and glad to see your code is improving. Pretty soon you will be
handling the last minute request in stride...

Things to note in your code. You do not need to use the Set key word on
string variables (or other regular variables). Set is only required with
objects like ranges and worksheets. When you are creating ranges with
variables or using methods like xlDown don't include the variable/method in
quotes.

--
HTH...

Jim Thomlinson


"Janis" wrote:

Jim
That really got me out of hot water. I do okay if I don't have any last
minute requests. wow thanks again. Janis

"Jim Thomlinson" wrote:

Here is some code for you. Instead of traversing column J looking for the
values in R it uses the Find function which will be a pile more efficient.
Here is how it works It traverses thour the cells in Column R and passes the
cell and the range to be searched (Column J) to the function FoundCells. the
function FoundCells performs the Search and returns a Range object made up of
all of the cells that it found. That range object is coloured red and copied
to sheet 2...

Sub FindStuff()
Dim rngStringsToFind As Range
Dim rng As Range
Dim rngToSearch As Range
Dim rngFoundCells As Range

Set rngStringsToFind = Range(Range("R2"), Cells(Rows.Count,
"R").End(xlUp))
Set rngToSearch = Columns("J")

For Each rng In rngStringsToFind
Set rngFoundCells = FoundCells(rngToSearch, rng)
If Not rngFoundCells Is Nothing Then
rngFoundCells.Font.ColorIndex = 3
rngFoundCells.Copy _
Destination:=Worksheets("Sheet2").Cells(Rows.Count ,
"A").End(xlUp).Offset(1, 0)
Set rngFoundCells = Nothing
End If
Next rng
End Sub

Private Function FoundCells(ByVal rngToSearch As Range, _
ByVal rngStringToFind As Range) As Range
Dim rngFound As Range
Dim strFirstAddress As String

Set FoundCells = Nothing
Set rngFound = rngToSearch.Find(What:=rngStringToFind.Value, _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
MatchCase:=False)
If Not rngFound Is Nothing Then
strFirstAddress = rngFound.Address
Set FoundCells = rngFound
Do
Set FoundCells = Union(rngFound, FoundCells)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
End If

End Function
--
HTH...

Jim Thomlinson


"Janis" wrote:

I have a string in each cell in column R. I want to see if there is a match
in each cell in column J. In column J there are one or more strings
delimited by a comma.
I am having trouble with the inner loop how to set the stringsearch. IF
there is a match I want to mark it in red and copy it to a column in another
sheet. tia,


Sub SDelete()
Dim ColumnJ As Range
Dim ColumnR As Range
Dim stringToSearch As String

Dim I As Integer
Dim C As Cell

Set J = ActiveSheet.Range("J2:end.xl(down)")

Set R = ActiveSheet.Range("R2:end.xl(down)")


I = 2

For Each C In ColumnR

'compare cell in ColumnR with each string in columnJ deliminated by commas
Set C = ("R" & I)


'compare cell in ColumnR with each string in ColumnJ
For Each stringToSearch In ColumnJ
Set stringToSearch = XXXXX
If stringToSearch = C Then

'if it matches color font red
C.Font.ColorIndex = 3
C.Copy Destination:=Worksheets("Sheet2").Range("5")

Else
Next




End If
I = I + 1


Next C


End Sub



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 360
Default help with inner loop to search for matching strings

thanks for this note.

"Jim Thomlinson" wrote:

Glad to help and glad to see your code is improving. Pretty soon you will be
handling the last minute request in stride...

Things to note in your code. You do not need to use the Set key word on
string variables (or other regular variables). Set is only required with
objects like ranges and worksheets. When you are creating ranges with
variables or using methods like xlDown don't include the variable/method in
quotes.

--
HTH...

Jim Thomlinson


"Janis" wrote:

Jim
That really got me out of hot water. I do okay if I don't have any last
minute requests. wow thanks again. Janis

"Jim Thomlinson" wrote:

Here is some code for you. Instead of traversing column J looking for the
values in R it uses the Find function which will be a pile more efficient.
Here is how it works It traverses thour the cells in Column R and passes the
cell and the range to be searched (Column J) to the function FoundCells. the
function FoundCells performs the Search and returns a Range object made up of
all of the cells that it found. That range object is coloured red and copied
to sheet 2...

Sub FindStuff()
Dim rngStringsToFind As Range
Dim rng As Range
Dim rngToSearch As Range
Dim rngFoundCells As Range

Set rngStringsToFind = Range(Range("R2"), Cells(Rows.Count,
"R").End(xlUp))
Set rngToSearch = Columns("J")

For Each rng In rngStringsToFind
Set rngFoundCells = FoundCells(rngToSearch, rng)
If Not rngFoundCells Is Nothing Then
rngFoundCells.Font.ColorIndex = 3
rngFoundCells.Copy _
Destination:=Worksheets("Sheet2").Cells(Rows.Count ,
"A").End(xlUp).Offset(1, 0)
Set rngFoundCells = Nothing
End If
Next rng
End Sub

Private Function FoundCells(ByVal rngToSearch As Range, _
ByVal rngStringToFind As Range) As Range
Dim rngFound As Range
Dim strFirstAddress As String

Set FoundCells = Nothing
Set rngFound = rngToSearch.Find(What:=rngStringToFind.Value, _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
MatchCase:=False)
If Not rngFound Is Nothing Then
strFirstAddress = rngFound.Address
Set FoundCells = rngFound
Do
Set FoundCells = Union(rngFound, FoundCells)
Set rngFound = rngToSearch.FindNext(rngFound)
Loop Until rngFound.Address = strFirstAddress
End If

End Function
--
HTH...

Jim Thomlinson


"Janis" wrote:

I have a string in each cell in column R. I want to see if there is a match
in each cell in column J. In column J there are one or more strings
delimited by a comma.
I am having trouble with the inner loop how to set the stringsearch. IF
there is a match I want to mark it in red and copy it to a column in another
sheet. tia,


Sub SDelete()
Dim ColumnJ As Range
Dim ColumnR As Range
Dim stringToSearch As String

Dim I As Integer
Dim C As Cell

Set J = ActiveSheet.Range("J2:end.xl(down)")

Set R = ActiveSheet.Range("R2:end.xl(down)")


I = 2

For Each C In ColumnR

'compare cell in ColumnR with each string in columnJ deliminated by commas
Set C = ("R" & I)


'compare cell in ColumnR with each string in ColumnJ
For Each stringToSearch In ColumnJ
Set stringToSearch = XXXXX
If stringToSearch = C Then

'if it matches color font red
C.Font.ColorIndex = 3
C.Copy Destination:=Worksheets("Sheet2").Range("5")

Else
Next




End If
I = I + 1


Next C


End Sub

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
Matching Two Text Strings to see if Like caldog Excel Worksheet Functions 4 December 29th 06 09:53 PM
matching columns using text strings! via135 via OfficeKB.com Excel Worksheet Functions 2 November 14th 06 04:00 AM
How to check matching strings in 3 columns Demitre Excel Discussion (Misc queries) 2 December 17th 05 11:40 PM
Compare cells/columns and highlight matching text strings luxbelle Excel Worksheet Functions 1 February 25th 05 06:34 PM
How can I compare a list of strings, allowing positive matching b. Joe Southin Excel Programming 2 December 8th 04 09:45 AM


All times are GMT +1. The time now is 11:15 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"