Pedy,
Your .Find statements are using xlPrevious, which means that they search backwards through the range. This is the reason it is always the 'last' pair that get affected.
Also there is no looping in the macro above, (other than to colour & border the rows of a single pair), which is why it does not operate on any other pairs. Try the code below, I have tried to explain it in comments, but please ask if there is anything you are unsure about:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
'Dim lRow As Long
'Dim LR1 As Long Now using the 'word1' range instead
'Dim LR2 As Long Now using the 'word2' range instead
Dim LC As Long
Dim word1 As Range
Dim word2 As Range
Dim word1_first_addr
Dim word2_first_addr
' Stop screen flicker
Application.ScreenUpdating = False
' Clear old formatting
With ActiveSheet.Cells
.Interior.ColorIndex = xlNone
.Borders.LineStyle = xlNone
.Borders(xlEdgeLeft).LineStyle = xlNone
.Borders(xlEdgeTop).LineStyle = xlNone
.Borders(xlEdgeBottom).LineStyle = xlNone
.Borders(xlEdgeRight).LineStyle = xlNone
' Find the last column used
LC = .Find(what:="*", after:=.Cells(1, 1), LookIn:=xlFormulas, lookat:=xlPart, searchdirection:=xlPrevious, searchorder:=xlByColumns).Column
' Find our first 'pair' of words
Set word1 = .Find(what:="word1", after:=.Cells(1, 1), LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows)
Set word2 = .Find(what:="word2", after:=.Cells(1, 1), LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows)
' As long as we found both words, carry on
If Not word1 Is Nothing And Not word2 Is Nothing Then
' Record the cell row & column address for each of the words, so that we know where we started
word1_first_addr = word1.Address
word2_first_addr = word2.Address
Do
With Range(.Cells(word1.Row + 1, 1), .Cells(word2.Row - 1, LC))
' Apply the colouring
.Interior.ColorIndex = 24
' Apply the borders
With .Borders
.LineStyle = xlDot
.ColorIndex = xlAutomatic
End With
End With
' Search for a pair of words again, AFTER the occurance we just dealt with
Set word1 = .Find(what:="word1", after:=word1, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows)
Set word2 = .Find(what:="word2", after:=word2, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows)
' If we find a pair, and they aren't at the same address as the very first pair we found then repeat the loop.
Loop While Not word1 Is Nothing And word1.Address <> word1_first_addr And _
Not word2 Is Nothing And word2.Address <> word2_first_addr
End If
End With
Application.ScreenUpdating = True
End Sub
Incidently why do you have the macro in the selection change macro? This means it runs every time you move the cursor on the sheet, clearing all the formatting and redoing it...
Bookmarks