I am write an excel VBA code to italicize part of string, using find and find next, Instr
, and characters objects. It runs perfect for the first 12 line, and all the lines below - full strings are italicized. I have checked my ranges and looks they are all fine.
Any idea what has caused this? Thanks.
The following is the code:
Option Explicit
Dim MySearch As Variant
Dim SearchLine As Range
Dim SearchResult As Range
Dim FirstAddress As String
Dim i As Long
Sub Copy_Column_U()
Worksheets("Reading List").Activate
Range("Full_Reference_In_Harvard_Format_Formula").Copy
Range("Full_Reference_In_Harvard_Format").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub
Sub Format_Harvard_Reference()
ThisWorkbook.Worksheets("Reading List").Activate
Call Copy_Column_U
Range("S5").Select
MySearch = ActiveCell.Value
Set SearchLine = Range("Full_Reference_In_Harvard_Format")
For Each MySearch In Range("Journal_Title")
Set SearchResult = SearchLine.Find(What:=MySearch, MatchCase:=False, LookAt:=xlPart)
If Not SearchResult Is Nothing Then
FirstAddress = SearchResult.Address
Do
i = InStr(1, SearchResult, MySearch, vbTextCompare)
SearchResult.Characters(i, Len(MySearch)).Font.Italic = True
Set SearchResult = SearchLine.FindNext(SearchResult)
Loop While Not SearchResult Is Nothing And SearchResult.Address <> FirstAddress
End If
ActiveCell.Offset(1, 0).Select
Next MySearch
End Sub