|
求助如何优化窗口输入区域选择自定义字数以上相同文字标红,现处理A2:A150(150条)处理需要时间3个钟
窗口输入区域选择自定义字数以上相同文字标红.zip
(19.31 KB, 下载次数: 2)
- Sub 窗口输入区域选择自定义字数以上相同文字标红()
- Dim ws As Worksheet
- Dim cell As Range
- Dim cellText As String
- Dim i As Long, j As Long, k As Long
- Dim minSubstringLength As Long
- Dim foundMatch As Boolean
- Dim startMatch As Long
- Dim currentSubstring As String
- Dim inputValue As String
- Dim userRange As Range
-
- ' 设置最小子字符串长度的默认值或让用户输入
- inputValue = InputBox("请输入要查找的重复子字符串的最小长度(例如:5):", "输入最小长度")
- If inputValue = vbNullString Then Exit Sub ' 用户点击了取消
- If Not IsNumeric(inputValue) Or CLng(inputValue) <= 0 Then
- MsgBox "请输入一个大于0的有效数字。", vbExclamation
- Exit Sub
- End If
- minSubstringLength = CLng(inputValue)
-
- ' 让用户选择范围
- On Error Resume Next
- Set userRange = Application.InputBox("请选择要处理的单元格范围:", "选择范围", Type:=8)
- On Error GoTo 0
- If userRange Is Nothing Then Exit Sub ' 用户没有选择范围
-
- ' 关闭屏幕更新以提高性能
- Application.ScreenUpdating = False
-
- ' 遍历用户选择的范围内的每个单元格
- For Each cell In userRange
- cellText = cell.Value
- ' 遍历文本的所有可能子字符串
- For i = minSubstringLength To Len(cellText)
- ' 遍历文本,从第一个字符开始
- For j = 1 To Len(cellText) - i + 1
- currentSubstring = Mid(cellText, j, i)
- ' 检查后续文本中是否有匹配的子字符串
- foundMatch = False
- For k = j + 1 To Len(cellText) - i + 1
- If Mid(cellText, k, i) = currentSubstring Then
- ' 找到匹配项,标红匹配文本
- cell.Characters(j, i).Font.Color = RGB(255, 0, 0)
- foundMatch = True
- Exit For
- End If
- Next k
- ' 如果找到匹配,可以跳出内层循环避免重复标红
- If foundMatch Then Exit For
- Next j
- Next i
- Next cell
-
- ' 重新开启屏幕更新
- Application.ScreenUpdating = True
-
- MsgBox "标红完成!", vbInformation
- End Sub
复制代码
|
|