LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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

 
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 05:06 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"