将连字符左侧的单词格式化为粗体,将右侧的单词格式化为斜体

问题描述 投票:0回答:1

我在单元格 D23:D52 之间有一个单词列表,这些单词之间用连字符分隔。

示例:

单词 1 单词 2 - 单词 3 单词 4

我想将连字符左侧的单词格式设置为粗体,将右侧的单词格式设置为斜体:

字1字2 - 字3字4

我只能弄清楚如何将每个单词设置为斜体或将每个单词设置为粗体。

有没有办法使用连字符作为分隔符将单词分成斜体和粗体?

excel vba delimiter bold italics
1个回答
1
投票

请尝试一下。

Option Explicit

Sub demo()
     Dim c As Range, aTxt
     For Each c In Range("D23:D52")
        If Len(c.Value) > 0 Then
            If InStr(c.Value, "-") > 0 Then
                aTxt = Split(c.Value, "-")
                c.Characters(1, Len(aTxt(0))).Font.Bold = True
                c.Characters(Len(aTxt(0)) + 2, Len(aTxt(1))).Font.Italic = True
            End If
        End If
     Next
End Sub

微软文档:

Range.Characters 属性 (Excel)

分割功能

© www.soinside.com 2019 - 2024. All rights reserved.