Excel 如何强制字体颜色保持不变而不是改变

Excel 如何强制字体颜色保持不变而不是改变,excel,vba,Excel,Vba,我的程序基本上会将文本中的关键字突出显示为用户选择的字体颜色。只有在用户指定的单元格中显示搜索词时,而不是在实际执行程序时,我才遇到问题。我从用户处获取术语输入、范围/单元格以显示这些术语、字体颜色。这是部分代码: Dim Ran As String searchTerms = InputBox("Please enter words to search, if more than one string seperate by comma and space", "Need

我的程序基本上会将文本中的关键字突出显示为用户选择的字体颜色。只有在用户指定的单元格中显示搜索词时,而不是在实际执行程序时,我才遇到问题。我从用户处获取术语输入、范围/单元格以显示这些术语、字体颜色。这是部分代码:

 Dim Ran As String
          searchTerms = InputBox("Please enter words to search, if more than one string seperate by comma and space", "Need Input", 1)


       Ran = InputBox("Please enter cell where you want the search terms to be displayed ideally below verbatim like C2,D2 ", "Need Input", 0, 8)
      r = Range(Ran).Row
      c = Range(Ran).Column

       If IsEmpty(Cells(r, 1)) And c <> "A" Then
         Range(Ran).Value = Range(Ran).Value & ", " & searchTerms
        Else: Range(Ran).EntireRow.Insert
        Range(Ran).Value = searchTerms
        End If

       searchTerms = Split(UCase(searchTerms), ", ")
第一次执行突出显示转弯,蓝色信号灯,并在单元格c2中创建:

第二次执行以绿色突出显示后部,在这一点之前效果良好

在完成第三次执行之前,高亮显示闪烁


第三次执行后。执行此操作以添加新词时,第二个执行的词变回蓝色:

Range(Ran).Value = Range(Ran).Value & ", " & searchTerms
不能保留文本的多种颜色-替换内容只会为整个单元格提供第一个字母的颜色。这对你的第一次和第二次跑步没问题,但从第三次跑步开始就会失败

您需要使用Characters集合添加新文本,而不是替换整个单元格内容

例如:

Sub Tester()

    Dim c As Range
    Set c = Range("A1")

    AddTextWithColor c, "first", vbRed
    AddTextWithColor c, "second", vbBlue
    AddTextWithColor c, "third", vbGreen

End Sub

Sub AddTextWithColor(c As Range, txt As String, clr As Long)
    Dim l As Long
    With c
        If Len(.Value) = 0 Then
            .Value = txt
        Else
            l = .Characters.Count
            'adds the new text without replacing existing formatting
            .Characters(l + 1, Len(txt) + 2).Text = "," & txt
        End If
        With .Characters(IIf(l = 0, 1, l + 2), Len(txt)).Font
            .Color = clr
            .Size = 14
        End With
    End With
End Sub

好像有很多相关的代码丢失了?这里没有与颜色相关的内容……请您至少在问题中添加一些屏幕截图。我如何保存格式?我猜这个值会使它丢失格式。我想我所需要做的就是在这一步中保存格式`范围(Ran).值=范围(Ran).值&“,”和搜索术语`@Tim Williams如何将字体颜色与值一起保存我应该在高亮显示函数中插入这部分代码还是在使用高亮显示函数之前?当我从像fontcolor这样的用户那里获得输入时,或者我需要插入的范围,基本上这就是我的突出显示功能所做的正确操作?您需要在代码中进行的相关更改是,要添加内容到现有内容中,您应该使用
字符集(
.Characters(l+1,Len(txt)+2)。Text=“,”&txt
)不要使用像
Range(Ran).Value=Range(Ran).Value&“,”和searchTerms这样的东西,它可以强制“,”成为黑色,如果这是你想要的-找到“,”并将它/它们涂成黑色:默认情况下,它将从任何现有内容继承颜色。
Sub Tester()

    Dim c As Range
    Set c = Range("A1")

    AddTextWithColor c, "first", vbRed
    AddTextWithColor c, "second", vbBlue
    AddTextWithColor c, "third", vbGreen

End Sub

Sub AddTextWithColor(c As Range, txt As String, clr As Long)
    Dim l As Long
    With c
        If Len(.Value) = 0 Then
            .Value = txt
        Else
            l = .Characters.Count
            'adds the new text without replacing existing formatting
            .Characters(l + 1, Len(txt) + 2).Text = "," & txt
        End If
        With .Characters(IIf(l = 0, 1, l + 2), Len(txt)).Font
            .Color = clr
            .Size = 14
        End With
    End With
End Sub