在Ex中以不同的格式设置单个字符

2024-04-25 00:28:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我在excel2013中有一列包含字母和数字1、2、3和4(代表拼音发音和音调值)。它们都是相同的字体和格式,但我只想把数字转换成上标。我似乎无法使用Excel的任何内置的查找和替换功能来将单元格中的单个字符替换为其上标版本:整个单元格格式都会发生更改。我看到了一个线程Format individual characters in a single Excel cell with python,它显然包含了一个解决方案,但那是我第一次听说Python或xlwt。在

既然我从未使用过Python和xlwt,有人能给我一个基本的一步一步的指令集来安装这些实用程序,自定义脚本并运行它吗?在

样品:

Li1Shi4
Qin3Fat1
Gon1Lin3
Den1Choi3
Xin1Nen3

来自其他线程的脚本:

^{pr2}$

实现“查找数字并替换为上标”的语法是什么?是字体还是样式?另一个线程的代码似乎是手动输入“seg1”、“seg2”、“seg3”等,还是我误解了代码?在

提前谢谢。我使用的是Windows 8,64位,Excel 2013。在


Tags: 代码脚本格式字母字体代表数字线程
1条回答
网友
1楼 · 发布于 2024-04-25 00:28:25

我很无聊,而且是在教学,所以,这里有一个很长的“答案”,它也解释了你将来如何为自己解决这些问题:)

我在单元格中键入abc123def,并使用宏记录器录制了一个宏。在

如果您不知道正确的语法是什么,您应该始终从这里开始。在

无论如何,我选择了这个单元格的数字部分,然后右键单击,设置单元格格式,将字体改为上标。在

这就是宏记录器给我的。这是很多代码。幸运的是,这是很多垃圾。在

Sub Macro2()
    With ActiveCell.Characters(Start:=1, Length:=3).Font  'Applies to the first 3 characters
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With ActiveCell.Characters(Start:=4, Length:=3).Font 'Applies to the middle 3 characters
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = True
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
    With ActiveCell.Characters(Start:=7, Length:=3).Font 'Applies to the last 3 characters
        .Name = "Calibri"
        .FontStyle = "Regular"
        .Size = 11
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
        .ThemeFont = xlThemeFontMinor
    End With
End Sub

它所代表的是三个格式块:第一个是前3个未更改的字符,然后是我们应用上标的3个字符,然后是最后三个字符。在

几乎所有这些都是默认属性,因为我没有做其他更改,所以我可以将其修改为:

^{pr2}$

现在我们可以看到这有两个重要的部分。第一部分是如何指定要格式化的字符。这是通过引用单元格的.Characters来完成的:

ActiveCell.Characters(Start:=4, Length:=3).Font

在“abc16”或“宏”中可以看到“abc16”中的字符。在

下一个明显的部分是将.Font.Superscript属性赋值为True。在

现在你想把它推广到任何地方。上面的代码“硬编码”了StartLength参数。我们需要让它充满活力。最简单的方法是一次输入1个字符,然后检查它是否是数字,如果是,则应用上标。在

Sub ApplySuperscriptToNumbers()
    Dim i As Long
    Dim str As String
    Dim rng As Range
    Dim cl As Range

    '## Generally should work on any contiguous "Selection" of cell(s)
    Set rng = Range(Selection.Address)
    '## Iterate over each cell in this selection
    For Each cl In rng.Cells
        str = cl.Value
        '## Iterate over each character in the cell
        For i = 1 To Len(str)
            '## Check if this character is numeric
            If IsNumeric(Mid(str, i, 1)) Then
                '## Apply superscript to this 1 character
                cl.Characters(Start:=i, Length:=1).Font.Superscript = True
            End If
        Next
    Next
End Sub

相关问题 更多 >