@Jack BeNimble 感谢代码,在 10 分钟内成功使用它来突出显示单元格中的所有数字。我对其进行了一点重组,首先搜索一行和单元格中的所有搜索词,并允许多列。我发现了一个错误,您的突出显示文本不喜欢重复 55、444,只突出显示序列中的奇数重复。修改高亮功能中的一行
newOffset = offSet + foundPos + Len(searchString) - 1 //added the - 1.
这是我修改后的代码。
子编号颜色()
Dim searchTerms As Variant
searchTerms = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".")
Dim searchString As String
Dim targetString As String
Dim offSet As Integer
Dim colsToSearch As Variant
Dim arrayPos, colIndex, colNum As Integer
Dim rowNum As Integer
colsToSearch = Array(4, 44, 45)
For colIndex = LBound(colsToSearch) To UBound(colsToSearch)
colNum = colsToSearch(colIndex)
For rowNum = 5 To 3000
For arrayPos = LBound(searchTerms) To UBound(searchTerms)
searchString = Trim(searchTerms(arrayPos))
offSet = 1
Dim x As Integer
targetString = Cells(rowNum, colNum).Value
x = HilightString(offSet, searchString, rowNum, colNum)
Next arrayPos
Next rowNum
Next colIndex
结束子
函数 HilightString(offSet As Integer, searchString As String, rowNum As Integer, ingredCol As Integer) As Integer
Dim x As Integer
Dim newOffset As Integer
Dim targetString As String
' offet starts at 1
targetString = Mid(Cells(rowNum, ingredCol), offSet)
foundPos = InStr(LCase(targetString), searchString)
If foundPos > 0 Then
' the found position will cause a highlight where it was found in the cell starting at the offset - 1
Cells(rowNum, ingredCol).Characters(offSet + foundPos - 1, Len(searchString)).Font.Color = vbBlue
' increment the offset to found position + 1 + the length of the search string
newOffset = offSet + foundPos + Len(searchString) - 1
x = HilightString(newOffset, searchString, rowNum, ingredCol)
Else
' if it's not found, come back out of the recursive call stack
Exit Function
End If
结束功能
谢谢 Jack BeNimble 和数据