gpt4 book ai didi

VBA - 更改修改文本的颜色

转载 作者:行者123 更新时间:2023-12-03 00:23:42 26 4
gpt4 key购买 nike

我有这段代码,如果单元格中的文本被修改,它会更改它的颜色。然而,我正在研究一些只改变单元格内修改文本的颜色的东西。例如,我在单元格 A1 =“此单元格”中,当我将其更改为“此单元格 - 这是新文本”时,我只想更改“- 这是新文本”的颜色

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
If Target.Font.ColorIndex = 3 Then
Target.Font.ColorIndex = 5
Else
Target.Font.ColorIndex = 3
End If
End If

End Sub

谢谢

最佳答案

这是我整理的内容:

Dim oldString$, newString$

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
newString = Target.Value
If Target.Font.ColorIndex = 3 Then
Target.Font.ColorIndex = 5
Else
Target.Font.ColorIndex = 3
End If
End If
Debug.Print "New text: " & newString
color_New_Text oldString, newString, Target
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("A1:A100")) Is Nothing Then
oldString$ = Target.Value
Debug.Print "Original text: " & oldString$
End If
End Sub

Sub color_New_Text(ByVal oldString As String, ByVal newString As String, ByVal theCell As Range)
Dim oldLen&, newLen&, i&, k&
oldLen = Len(oldString)
newLen = Len(newString)

Debug.Print newString & ", " & oldString
For i = 1 To newLen
If Mid(newString, i, 1) <> Mid(oldString, i, 1) Then
Debug.Print "different"
Debug.Print theCell.Characters(i, 1).Text
If theCell.Characters(i, 1).Font.ColorIndex = 3 Then
theCell.Characters(i, 1).Font.ColorIndex = 5
Else
theCell.Characters(i, 1).Font.ColorIndex = 3
End If
End If
Next i

End Sub

它有两个全局变量,一个 Worksheet_SelectionChangeWorksheet_Change 用于获取字符串。

关于VBA - 更改修改文本的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37164020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com