word macro crashes when 'while' loop is executed - vba

I have a VBA macro(Word2010) script to highlight all the text in italics. But when executed in large file say a document with more than 10 pages the Word get crashed.
I have used the below code for this purpose.
Sub Italics_Highlight()
'
' test_italics_highlight_ Macro
'
'
Application.ScreenUpdating = False
Dim myString As Word.Range
Set myString = ActiveDocument.Content
With myString.Find
.ClearFormatting
.Text = ""
.Font.Italic = True
While .Execute
myString.HighlightColorIndex = wdTurquoise
myString.Collapse wdCollapseEnd
Wend
End With
MsgBox "Thank you!"
End Sub
Could you please help to overcome this. Thanks for your help in advance.

Your error description looks like your code is running forever and doesn't finish.
You might want to add a DoEvents inside your While loop to keep Word responsive while running the code.
With myString.Find
.ClearFormatting
.Text = ""
.Font.Italic = True
While .Execute
DoEvents 'keeps Word responsive
myString.HighlightColorIndex = wdTurquoise
myString.Collapse wdCollapseEnd
Wend
End With
I'm not sure if your code will ever stop. The loop might not stop at the end of the document but start again from beginning, and therefore always find something italic again and again, looping forever.
So you might need to set the .Wrap = wdFindStop to stop at the end of the document.
See Find.Wrap Property (Word).
With myString.Find
.ClearFormatting
.Text = ""
.Font.Italic = True
.Wrap = wdFindStop 'stop at the end of the document
While .Execute
DoEvents 'keeps Word responsive
myString.HighlightColorIndex = wdTurquoise
myString.Collapse wdCollapseEnd
Wend
End With

You don't need to stop at each "found" and apply highlighting. You can do it as part of a Find/Replace:
Sub testInfiniteLoop()
Dim myString As word.Range
Set myString = ActiveDocument.content
Options.DefaultHighlightColorIndex = wdTurquoise
With myString.Find
.ClearFormatting
.Text = ""
.Font.Italic = True
.Replacement.Text = ""
.Replacement.Highlight = wdTurquoise
.wrap = wdFindStop 'stop at the end of the document
.Execute Replace:=wdReplaceAll
End With
End Sub

The following code not only highlights but also restores whatever highlight settings were previously in force:
Sub Italics_Highlight()
Application.ScreenUpdating = False
Dim i As Long: i = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdTurquoise
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = "^&"
.Replacement.Highlight = True
.Format = True
.Font.Italic = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Options.DefaultHighlightColorIndex = i
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
As you can see, you also don't need:
Dim myString As Word.Range
Set myString = ActiveDocument.Content

Related

Remove OR replace faulty paragraph marks using VBA macro

I have some faulty paragraphs, which are causing my other macros to not work properly.
They are usually heading style 2, style 3
Empty (not sure)
before OR after table (not sure)
surrounded by dotted line
causes the heading and next table to merged together (not sure)
I tried to replace/removed those with the following macro:
Sub HeadingParaBug()
Dim H As Range
Set H = ActiveDocument.Range
LS = Application.International(wdListSeparator)
With H.Find
.Text = "^p "
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = " ^p"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^p ^p"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
.Text = "^13{2" & LS & "}"
.Replacement.Text = "^p"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.Text = ""
.Style = wdStyleHeading2
.MatchWildcards = False
Do While .Execute
If H.Text <> vbCr Then
H.Collapse 0
H.Select
H.InsertParagraph
H.Delete
End If
H.Collapse 0
Loop
End With
Set H = ActiveDocument.Range
With H.Find
.Style = wdStyleHeading3
Do While .Execute
If H.Text <> vbCr Then
H.Collapse 0
H.Select
H.InsertParagraph
H.Delete
End If
H.Collapse 0
Loop
End With
End Sub
But somehow, it do not completely removed/replace the faulty paragraph marks. The above macro finds those paragraphs, add new and then remove it. which eventually removed the dotted line.
Can anybody explain this phenomena? what is the right ways to remove/replace those paragraphs. please download and see test file with error on page 7
Update: Even I tried with the following code but it did nothing (on MacOS Video). I think it is not finding the hidden paragraphs:
Sub HidNempty()
Dim H As Range
Set H = ActiveDocument.Range
With H.Find
.Text = "^p "
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = " ^p"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^p ^p"
.Replacement.Text = "^p^p"
.Execute Replace:=wdReplaceAll
.Text = "^p"
Do While .Execute
If H.Font.Hidden = True Then
H.Font.Hidden = False
If Len(Trim(H.Paragraphs(1).Range.Text)) = 1 Then
H.Delete
End If
End If
Loop
End With
End Sub
To unhide all document paragraphs, please try the next piece of code:
Sub UnHideParagraphs()
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If para.Range.Font.Hidden Then
para.Range.Font.Hidden = False
End If
Next para
End Sub
It should work even if only part of the paragraph range is hidden...
Find/Replace won't delete duplicate paragraph breaks before a table, between tables, or after a table. Try:
Sub Demo()
Application.ScreenUpdating = False
Dim LS As String, Tbl As Table, bHid As Boolean
LS = Application.International(wdListSeparator)
bHid = ActiveWindow.View.ShowHiddenText
ActiveWindow.View.ShowHiddenText = True
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Forward = True
.Replacement.Font.Hidden = False
.Wrap = wdFindContinue
.MatchWildcards = False
.Text = "^p^w"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^w^p"
.Execute Replace:=wdReplaceAll
.MatchWildcards = True
.Text = "^13{2" & LS & "}"
.Execute Replace:=wdReplaceAll
.Wrap = wdFindStop
End With
Do While .Find.Execute = True
With .Duplicate
.Font.Hidden = False
.Start = .Start + 1
.Text = vbNullString
End With
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
For Each Tbl In ActiveDocument.Range.Tables
With Tbl.Range
Do While .Characters.First.Previous.Previous = vbCr
.Characters.First.Previous.Previous = vbNullString
Loop
.Characters.First.Previous.Font.Hidden = False
Do While .Characters.Last.Next = vbCr
If .Characters.Last.Next.End = ActiveDocument.Range.End Then Exit Do
If .Characters.Last.Next.Next.Information(wdWithInTable) = True Then Exit Do
.Characters.Last.Next = vbNullString
Loop
.Characters.Last.Next.Font.Hidden = False
End With
Next
ActiveWindow.View.ShowHiddenText = bHid
Application.ScreenUpdating = True
End Sub
You will observe various lines in the code that apply .Font.Hidden = False. Depending on what you're trying to achieve visually, you may or may not want those.

Word macros are slow

I am new to word Macros.
I have a task of highlighting all the bolds, italics, superscripts, subscripts. I have already written the code for it which works as expected but it is too slow even for a 50-page document can this code be improved?
The Code
Application.ScreenUpdating = False
For Each ch In ActiveDocument.Characters
If ch.Font.Superscript = True Or ch.Font.Subscript = True Or ch.Font.Bold = True Or ch.Font.Italic = True Or ch.Font.Name = "Consolas" Or ch.Font.Name = "Courier New" Then
ch.HighlightColorIndex = wdYellow
End If
Next
Application.ScreenUpdating = True
Please let me know if this can be improved.
Thanks
There really is no need to loop through the document one character at a time. You should also learn to make more productive use of the tools already available via the GUI. In a long document, a few manual Find/Replace operations might even be faster than your macro! Try:
Sub Demo()
Application.ScreenUpdating = False
Options.DefaultHighlightColorIndex = wdYellow
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = "^&"
.Replacement.Highlight = True
.Format = True
.Forward = True
.Wrap = wdFindContinue
.Font.Superscript = True
.Execute Replace:=wdReplaceAll
.Font.Subscript = True
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Font.Name = "Consolas"
.Execute Replace:=wdReplaceAll
.Font.Name = "Courier New"
.Execute Replace:=wdReplaceAll
End With
End With
Application.ScreenUpdating = True
End Sub

Why doesn't find and replace in VBA Macro with wildcard setting recognise hyperlinked word as a legitimate part of the text

The below VBA Word macro is run after selecting several paragraphs or for this example all of them.
I attach a sample .rtf file on which to run the macro.
The biblical references at the start of the paragraphs all get a pair around them, except the one that has a hyperlink. Is my macro at fault or is this an issue with Word 2010. As a secondary point it would be helpful to know if this works on Office 365
(I have tried the same, on LibreOffice and it does match even if word is hyperlinked one
(^)([A-Z123I ]{1,3}[^ ]{1,15} )([0-9]{1,3}:[0-9-\–]{1,7})
$1$2$3$2$3
So please don't suggest that I have not made any effort to find if this should work, or that I have not tried different settings. It would have been more helpful for someone to post that it did not work for them to at least show they had taken the time to download the macro test file and actually do a test)
Private Sub RelRefWithBibleName_Click()
InSelection = False
If selection.Type = wdSelectionIP Then InSelection = True
If InSelection = True Then
MsgBox ("select some text")
Exit Sub
End If
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
selection.Find.Replacement.Font.Reset
Application.ScreenUpdating = False
With selection
'Added this to make selection go beyond the start of the selected paragraph
'so that the detection would work
selection.MoveStartUntil Cset:=wdCharacter, Count:=wdBackward
strFindText = "([^13])([A-Z123I ]{1,3}[! ]{1,15} )([0-9]{1,3}:[0-9\-\–]{1,7})"
strReplaceText = "\1<ref>\2\3</ref>\2\3"
End With
With selection.Find
.MatchWildcards = True
.ClearFormatting
.Replacement.ClearFormatting
.text = strFindText
.Replacement.text = strReplaceText
.Format = False
.MatchWholeWord = True
.Forward = True
.Wrap = wdFindStop
End With
selection.Find.Execute Replace:=wdReplaceAll
selection.Shrink
selection.Move
Application.ScreenUpdating = True
selection.Find.ClearFormatting
selection.Find.Replacement.ClearFormatting
End Sub
Looping through the hyperlinks collection is no big deal. That said, there is another way:
Sub Demo()
Application.ScreenUpdating = False
Dim RngFnd As Range, StrTxt As String
With Selection
Set RngFnd = .Range
With .Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "[A-Z1-3 ]{1,3}[! \<\>]{1,15} [0-9]{1,3}:[0-9\-\?]{1,7}"
.Replacement.Text = ""
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .InRange(RngFnd) Then
If .Paragraphs.Count > 1 Then .Start = .Paragraphs(1).Range.End
If .Start = .Paragraphs(1).Range.Start Then
StrTxt = .Text
.InsertBefore "<ref>" & StrTxt & "</ref>"
.Font.Bold = False
.Start = .End - Len(StrTxt)
.Font.Bold = True
End If
If .Hyperlinks.Count > 0 Then
If .Hyperlinks(1).Range.Start = .Paragraphs(1).Range.Start Then
With .Hyperlinks(1).Range
StrTxt = .Text
.InsertBefore "<ref>" & StrTxt & "</ref>"
.Font.Bold = False
.Start = .End - Len(StrTxt)
.Font.Bold = True
End With
End If
End If
Else
Exit Do
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
End With
RngFnd.Select
Application.ScreenUpdating = True
End Sub
There is nothing wrong with your Find/Replace expressions, though they could be simplified:
strFindText = "([^13])([A-Z1-3 ]{1,3}[! ]{1,15} [0-9]{1,3}:[0-9\-\–]{1,7})"
strReplaceText = "\1<ref>\2</ref>\2"
The Word version is of no consequence. For hyperlinks, you could loop through the hyperlinks collection and, if applicable, testing the display text, before inserting the tags either side of them.

Replacing text with StoryRange on Word VBA makes it briefly unresponsive

I've used the following code to find and replace text on every storyrange, although I'm looking specifically for footers/headers and mainbody.
For Each myStoryRange In ActiveDocument.StoryRanges
If myStoryRange.StoryType = wdPrimaryFooterStory Then
myStoryRange.Find.Execute FindText:=footerfindreplace.Item(i).FND, Forward:=True, ReplaceWith:=footerfindreplace.Item(i).replc, Wrap:=wdFindContinue, replace:=wdReplaceAll
End If
If myStoryRange.StoryType = wdFirstPageFooterStory Then
myStoryRange.Find.Execute FindText:=footerfindreplace.Item(i).FND, Forward:=True, ReplaceWith:=footerfindreplace.Item(i).replc, Wrap:=wdFindContinue, replace:=wdReplaceAll
End If
If myStoryRange.StoryType = wdEvenPagesFooterStory Then
myStoryRange.Find.Execute FindText:=footerfindreplace.Item(i).FND, Forward:=True, ReplaceWith:=footerfindreplace.Item(i).replc, Wrap:=wdFindContinue, replace:=wdReplaceAll
End If
Next myStoryRange
It worked allright for the footers and if the document had only one section.
However I have documents with more than one section of course, and I'd rather go trough all the document. So I found a different approach:
With ActiveDocument
For Each Rng In .StoryRanges
On Error Resume Next
With Rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.MatchCase = True
.Execute replace:=wdReplaceAll
End With
On Error GoTo 0
Next
For Each Sctn In .Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
With .Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End With
Next
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then
With .Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End With
Next
Next
End With
This works perfectly but has something I dislike, the word becomes unresponsive for about 10 seconds, regardless of the document. I also found there are 17 types of storyranges, and maybe that's the reason why it takes so long.
I know at least that with headers and footers (which are 6 of them) I can use the condition .Exists = true or false, to skip them. But that doesn't improve the result a lot.
I only have 5 words for replacement, Why does it become unresponsive? Is there a way to make it smooth?
Thanks for any help.
Update:
Upon reading the comments, I've tried the following with no avail
With ActiveDocument.StoryRanges(1).Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.MatchCase = True
.MatchAllWordForms = False
.MatchWholeWord = False
.MatchWildcards = False
End With
For Each Rng In ActiveDocument.StoryRanges
On Error Resume Next
With Rng.Find
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
On Error GoTo 0
Next
For Each Sctn In ActiveDocument.Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
If HdFt.Exists = True Then
With .Range.Find
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End If
End With
Next
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then
If HdFt.Exists = True Then
With .Range.Find
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
.Execute replace:=wdReplaceAll
End With
End If
End If
End With
Next
Next
If I dont place
.Text = footerfindreplace.Item(i).FND
.Replacement.Text = footerfindreplace.Item(i).replc
In every loop, it will not replace. Also the update display is already false.
Can anyone help?
Update:
Recently I tried to search every section inside mystory ranges, hoping to filter out the order..
For Each storyrang In ActiveDocument.StoryRanges
For Each Sctn In storyrang.Sections
For Each rang In Sctn.Ranges
With rang
For ii = 1 To footerfindreplace.count
Call RngFnd(.Range, footerfindreplace.Item(i).FND, footerfindreplace.Item(i).replc) 'find and replace text in the given range
Next ii
End With
Next
Next
Next
The result however remains not good
Your code is lacking context - specifically regarding footerfindreplace.Item(i).FND and footerfindreplace.Item(i).replc. You're code is also processing all storyranges (which includes headers & footers), then processing headers & footers again by Section.
If footerfindreplace.Item(i).FND and footerfindreplace.Item(i).replc represent a single call to the document, you might use code like:
Sub Demo()
Application.ScreenUpdating = False
Dim Sctn As Section, HdFt As HeaderFooter
With ActiveDocument
Call RngFnd(.Range, footerfindreplace.Item(i).FND, footerfindreplace.Item(i).replc)
For Each Sctn In .Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
Call RngFnd(.Range, footerfindreplace.Item(i).FND, footerfindreplace.Item(i).replc)
End If
End With
Next
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then
Call RngFnd(.Range, footerfindreplace(i).FND, footerfindreplace(i).replc)
End If
End With
Next
Next
End With
Application.ScreenUpdating = True
End Sub
Sub RngFnd(Rng As Range, StrFnd As String, StrRep As String)
With Rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Text = StrFnd
.Replacement.Text = StrRep
.MatchCase = True
.Execute Replace:=wdReplaceAll
End With
End Sub
Alternatively, if you're processing multiple footerfindreplace items, you might use code like:
Sub Demo()
Application.ScreenUpdating = False
Dim Sctn As Section, HdFt As HeaderFooter
With ActiveDocument
Call RngFnd(.Range, footerfindreplace)
For Each Sctn In .Sections
For Each HdFt In Sctn.Headers
With HdFt
If .LinkToPrevious = False Then
Call RngFnd(.Range, footerfindreplace)
End If
End With
Next
For Each HdFt In Sctn.Footers
With HdFt
If .LinkToPrevious = False Then
Call RngFnd(.Range, footerfindreplace)
End If
End With
Next
Next
End With
Application.ScreenUpdating = True
End Sub
Sub RngFnd(Rng As Range, ArrFndRep)
Dim i As Long
With Rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = False
.Forward = True
.MatchCase = True
.Wrap = wdFindContinue
For i = 0 To UBound(ArrFndRep)
.Text = ArrFndRep(i).FND
.Replacement.Text = ArrFndRep(i).replc
.Execute Replace:=wdReplaceAll
Next
End With
End Sub
In either case, though, it's hardly apparent why you'd be processing something described as footerfindreplace in either the document body or its headers...
I'm posting this years later in case it helps others coding for StoryRanges.
I've got a similar issue and the OP's code gave me a start, so this is my Thank you.
The OP wanted to replace arbitrary text in MainTextStory, and in all Headers & Footers. His original code failed if there was more than a single Section.
The vital element is that Help defines StoryRanges as a Collection of Ranges.
Story Ranges can be chained together by the NextStoryRange property
This code iterates the entire document text just once
The Find Replace is the OP's code, but I've added a For loop for clarity
For Each myStoryRange In ActiveDocument.StoryRanges
Do
For i = lbound(footerfindreplace.Item) to Ubound(footerfindreplace.Item)
myStoryRange.Find.Execute FindText:=footerfindreplace.Item(i).FND, Forward:=True, ReplaceWith:=footerfindreplace.Item(i).replc, Wrap:=wdFindContinue, Replace:=wdReplaceAll
next i
If Not myStoryRange.NextStoryRange Is Nothing Then
Set myStoryRange = myStoryRange.NextStoryRange
End If
Loop Until myStoryRange.NextStoryRange Is Nothing
Next MyStoryRange
I hope this helps somebody at some time
Spilly

Word macro for changing color for negative numbers in a specific column depending on value in a different column

Thanks in advance for any reply.
I am working on presentation of some reports. The periodical reports are imported from a different software into Word template. For all tables and for each row I would like to change the color of the negative numbers in column 14 only if there is a certain text in column 3.
Unfortunately I have to use a Word template to do this. It seems that a macro is my only option so I have tried to Frankenstein something from different macros I found online:
Dim varColumn As Column
Dim clColumn As Column
Dim cCell As Variant
Set clColumn = Selection.Columns(3)
Set varColumn = Selection.Columns(14)
With clColumn
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.text = "value"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True Then
Selection.MoveRight Unit:=wdCell, Count:=11
End If
If cCell < 0 Then
Selection.Font.color = wdColorRed
End If
Loop
End With
End Sub
I think the macro needs lines to repeat the search. See the two lines added before Loop.
With Selection
.HomeKey Unit:=wdStory 'Starts at the beginning, to search all tables.
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "value"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found
If .Information(wdWithInTable) = True And _
.Cells(1).ColumnIndex = 3 Then 'Confirms it's in the 3rd column.
.MoveRight Unit:=wdCell, Count:=11
End If
If .Range < 0 Then
.Font.Color = wdColorRed
End If
.Collapse wdCollapseEnd 'Collapses the selection to no characters.
.Find.Execute 'Searches again from the current selection point.
Loop
End With
Try:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "-[0-9][0-9,.]{1,}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchWildcards = True
.Execute
End With
Do While .Find.Found = True
If .Information(wdWithInTable) = True Then
If .Cells(1).ColumnIndex = 14 Then
If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then
.Font.ColorIndex = wdRed
End If
End If
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
End With
Application.ScreenUpdating = True
End Sub
If the table might have vertically-merged cells, change:
If Split(.Rows(1).Cells(3).Range.Text, vbCr)(0) = "specified text" Then
to:
If Split(.Tables(1).Cell(.Cells(1).RowIndex, 3).Range.Text, vbCr)(0) = "specified text" Then