有说数字串作为一个字符吗,我好像没看见,逻辑也很简单,就是用自定义的split2函数来拆分文本为数组,把连续的数字作为一个整体,内层循环for j修改为for each t,完整代码如下:
程序代码:
Option Explicit
Sub x()
Dim dic As Object
Dim arr, i&, j&, s$, t
arr = ActiveSheet.UsedRange
'第一步做字典
Set dic = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(arr)
s = Trim(arr(i, 1)) '1=》A列
If s <> "" Then
For Each t In split2(s)
dic(t(1)) = True
Next t
End If
Next i
'第二步做标记
For i = 1 To UBound(arr)
s = arr(i, 2) '2=》B列
If s <> "" Then
For Each t In split2(s)
If t(1) = "=" Or t(1) = "各" Then Exit For
If dic(t(1)) Then Cells(i, 2).Characters(t(0), Len(t(1))).Font.Color = RGB(255, 0, 0)
Next t
End If
Next i
End Sub
Function split2(s$) '把字符串拆分为字符数组,数字作为整体
Dim m&, arr(), i&, n&, c$, c2$, ci&, si$
m = Len(s)
n = 0
ci = 0
si = ""
For i = 1 To m
c = Mid(s, i, 1)
si = si & c
If i = m Then c2 = "" Else c2 = Mid(s, i + 1, 1)
If ci = 0 Then ci = i
If notNumber(c) Or notNumber(c2) Or i = m Then
ReDim Preserve arr(0 To n)
arr(n) = Array(ci, si)
n = n + 1
si = ""
ci = 0
End If
Next i
split2 = arr
End Function
Function notNumber(c$) '判断字符c是否数字
notNumber = c < "0" Or c > "9"
End Function
我很想知道能看明白代码吗,而不代码执行了是什么效果