我正在尝试编写一些代码来查找 excel 单元格中是否包含我要查找的单词并将其设为粗体。到目前为止,我已经编写了以下代码
With Worksheets("Label Print").Cells(i, J)
.Characters(Start:=InStr(.Value, “Name”), Length:=Len(“Name”)).Font.Bold = True
End With
问题是如果“名称”在一个单元格中出现两次(或更多),它将仅突出显示其第一次出现。
先感谢您
这是一个函数,它需要检查单元格和要搜索的单词,并将该单词的所有大小写加粗:
Public Sub BoldWord(rngCell As Range, sWord As String)
Dim iPlace As Integer
iPlace = InStr(1, rngCell.Value, sWord, vbTextCompare)
Do While iPlace > 0
rngCell.Characters(Start:=iPlace, Length:=Len(sWord)).Font.Bold = True
iPlace = InStr(iPlace + 1, rngCell.Value, sWord, vbTextCompare)
Loop
End Sub
注 1: rngCell 必须是单个单元格。
注意 2:搜索不区分大小写...如有必要,请更改 vbTextCompare。