Problem infinite loop - find and bold based on cell value

jptaz

New Member
Joined
May 1, 2020
Messages
46
Office Version
  1. 2010
Platform
  1. Windows
Hello,

I have found this code to find and bold specific words in my excel worksheet. The code works good with the 9 first words, however, if I add words based on cell value (text 10 and 11), the code seems to run endlessly and ultimately crashes. The line here seems to be the problem if I debug it " rCell.Characters(iSeek, Len(sToFind)).Font.bold = True". I'm really no expert and I hope someone can help me with this :)

thank you


VBA Code:
Sub Find_and_Bold1HAP() 

Dim rCell As Range, sToFind As String, iSeek As Long
Dim Text(1 To 11) As String
Dim i As Integer
Dim ws As Worksheet
 
 Set ws = Worksheets("Rapport ini")

Text(1) = "DOSSIER : "
Text(2) = "NOM, PRÉNOM :"
Text(3) = "DATE DE NAISSANCE :"
Text(4) = "Programme :"
Text(5) = "ÂGE :"
Text(6) = "SEXE :"
Text(7) = "TÉLÉPHONE :"
Text(8) = "ADRESSE :"
Text(9) = "Commentaires :"
Text(10) = Worksheets("Données").Range("D211").Text
Text(11) = Worksheets("Données").Range("E211").Text


For i = LBound(Text) To UBound(Text)
    For Each rCell In ws.Range("A1:S10, A54, A69, A72, A91")
    
            sToFind = Text(i)
            iSeek = InStr(1, rCell.Value, sToFind)
        Do While iSeek > 0
         
            rCell.Characters(iSeek, Len(sToFind)).Font.bold = True
            iSeek = InStr(iSeek + 1, rCell.Value, sToFind)
         Loop
    Next rCell
Next i
 
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You do not need the inner Do loop. InStr will find the string anywhere, you do not have to check each character. The first argument is optional and just tells it where to start looking. Leave it out, and it will always start at the beginning.

VBA Code:
   For i = LBound(Text) To UBound(Text)
       For Each rCell In ws.Range("A1:S10, A54, A69, A72, A91")
      
         sToFind = Text(i)
         iSeek = InStr(rCell.Value, sToFind)
         If iSeek > 0 Then
            rCell.Characters(iSeek, Len(sToFind)).Font.Bold = True
         End If
        
       Next rCell
   Next i
 
Upvote 0
Solution
You do not need the inner Do loop. InStr will find the string anywhere, you do not have to check each character. The first argument is optional and just tells it where to start looking. Leave it out, and it will always start at the beginning.

VBA Code:
   For i = LBound(Text) To UBound(Text)
       For Each rCell In ws.Range("A1:S10, A54, A69, A72, A91")
     
         sToFind = Text(i)
         iSeek = InStr(rCell.Value, sToFind)
         If iSeek > 0 Then
            rCell.Characters(iSeek, Len(sToFind)).Font.Bold = True
         End If
       
       Next rCell
   Next i
Thank you very much, it works great!

Have a nice day!!
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top