|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
这是我网上凑出来的代码,代码的目的是,发现选区内的字母,大写字母或者小写字母,仅把字母标记为红色,其他的不标记。
代码可运行在数据区域小的地方,我正常需要的数据区域差不多是100列*10000行上下,这个代码就会让excel死机,求教该如何优化。
运行出来的效果如下,麻烦看下该如何优化,能让代码在大的数据区域下跑起来,感谢。
代码:
Sub 修改单元格内字母颜色()
Dim i As Integer
Dim MyKeys
'建立要设置颜色的文本数组
MyKeys = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
Dim TotalRange As Range
Dim ThisRange As Range
'指定要设置的单元格区域
Set TotalRange = Application.InputBox("选择具体区域,不要选择整行整列:", , , , , , , 8)
'用循环开始设置
For Each ThisRange In TotalRange
For i = 0 To UBound(MyKeys)
'调用自定义过程
Call MyReplace(ThisRange, MyKeys(i))
Next i
Next
End Sub
'自定义过程,对单元格里的指定文字设置为红色
Sub MyReplace(ByVal MyRange As Range, ByVal MyKey As String)
Dim LinStr As String
Dim L As Integer
Dim i As Integer
L = Len(MyKey)
LinStr = MyRange.Text
i = InStr(1, LinStr, MyKey)
Do While i <> 0
MyRange.Characters(Start:=i, Length:=L).Font.Color = RGB(255, 0, 0)
i = InStr(i + L, LinStr, MyKey)
Loop
End Sub
|
|