某些子字符串之间 Excel 中的格式和颜色文本

Format and Color Text in Excel Between Certain SubStrings

我想在 excel 中突出显示一些文本(将其设为红色和粗体)。这应该在特定条件下使用公式自动完成。 文本格式如下:

<file size="99999" index="0" tid="2893892389283">picture.jpg</file><file size="65444557" index="0" tid="5636346466">movie.avi</file><file size="12135" index="0" tid="43743766433">textfile.txt</file>

所有这些东西都在一个单元格中。现在我想突出显示 "size=" 之后的数字和文件名。例如,在第一个字段中,我想将 99999 和 pictures.jpg 设置为红色和粗体。

我的 excel 文档中有很多这种格式的文本,唯一的办法就是自动生成它。

这是一个例子。右侧是我需要的结果:

正如@ScottCraner 在评论中提到的,这是无法通过公式实现的。但是, 的选择是无限的。

这实现了 "task":

的 50%

Public Sub FormatAndColor()

    Range("B1") = Range("A1")

    Dim lookFor As String: lookFor = "size="""
    Dim i As Long
    Dim lenLookFor As Long: lenLookFor = Len(lookFor)

    For i = 1 To Len(Range("B1"))
        With Range("B1")
            If .Characters(i, lenLookFor).Text = lookFor Then
                While IsNumeric(.Characters(i + lenLookFor, 1).Text)
                    With .Characters(i + lenLookFor, 1).Font
                    .Bold = True
                    .Color = vbRed
                    End With
                    i = i + 1
                Wend
            End If
        End With
    Next i

End Sub

这就是代码的作用:

  • 将输入从 A1 复制到 B1:
  • 遍历当前字符串的每个字符
  • 如果它发现字符串和下一个 N 字符串等于 size=",它会进入一个新的循环。
  • 在新循环中,如果每个数字字符加粗并着色

对于 "task" 的其他 50%,您可以应用类似的逻辑 - 查找 > 并为其着色,直到找不到下一个开始标记。只是注意不要和最后的 > 进入死循环。这就是 i < Len(Range("B1")) 出现在那里的原因:

Public Sub FormatAndColor()

    Range("B1") = Range("A1")

    Dim lookFor As String: lookFor = ">"
    Dim i As Long
    Dim lenLookFor As Long: lenLookFor = Len(lookFor)

    For i = 1 To Len(Range("B1"))
        With Range("B1")
            If .Characters(i, lenLookFor).Text = lookFor Then
                While .Characters(i + lenLookFor, 1).Text <> "<" And i < Len(Range("B1"))
                    .Characters(i + lenLookFor, 1).Font.Bold = True
                    .Characters(i + lenLookFor, 1).Font.Color = vbRed
                    i = i + 1
                Wend
            End If
        End With
    Next i

End Sub

最后,如果您合并两个循环并确保更新 lookForlenLookFor,您将获得所需的内容: