Domanda

I have a column in Excel 2013 containing letters and the digits 1,2,3 and 4 (representing pinyin pronunciations and tone values). They are all in the same font & format, but I would like to convert the numbers only to superscript. It does not seem that I can use any of Excel's built-in find-and-replace functionality to replace a single character in a cell with its superscript version: the entire cell format gets changed. I saw a thread Format individual characters in a single Excel cell with python which apparently holds a solution, but that was the first time I had heard of Python or xlwt.

Since I have never used Python and xlwt, can someone give me a basic step-by-step set of instructions to install those utilities, customize the script and run it?

Sample:

Li1Shi4
Qin3Fat1
Gon1Lin3
Den1Choi3
Xin1Nen3

Script from other thread:

import xlwt

wb = xlwt.Workbook()
ws = wb.add_sheet('Sheet1')

font0 = xlwt.easyfont('')
font1 = xlwt.easyfont('bold true')
font2 = xlwt.easyfont('color_index red')
style = xlwt.easyxf('font: color_index blue')

seg1 = ('bold', font1)
seg2 = ('red', font2)
seg3 = ('plain', font0)
seg4 = ('boldagain', font1)

ws.write_rich_text(2, 5, (seg1, seg2, seg3, seg4))
ws.write_rich_text(4, 1, ('xyz', seg2, seg3, '123'), style)

wb.save('rich_text.xls')

What is the syntax that will achieve the "find numbers and replace with superscript"? Is it a font or a style? The code from the other thread seems to manually input "seg1" , "seg2" , "seg3" etc. Or am I misunderstanding the code?

Thanks in advance. I am using Windows 8, 64 bit, Excel 2013.

È stato utile?

Soluzione

I'm bored and in a teaching mood, so, here's a long "answer" that also explains a little bit about how you can figure these things out for yourself in the future :)

I typed abc123def into a cell, and recorded a macro using the macro recorder.

This is where you should always start if you don't know what the correct syntax is.

In any case, I selected the numeric part of this cell, and right-clicked, format cell, change font to superscript.

This is what the macro recorder gives me. This is a lot of code. Fortunately, it's a lot of junk.

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

What it represents is three blocks of formatting: the first is the first 3 characters that aren't changed, then the 3 that we applied superscript to, and then the last three characters.

Almost all of this is default properties, since I made no other changes, so I can revise it to this:

Sub Macro2()
    With ActiveCell.Characters(Start:=4, Length:=3).Font
        .Superscript = False
    End With
End Sub

Now we can see that there are two important parts to this. The first part is how to specify which characters to format. This is done by refereing to a cell's .Characters:

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

So we can see that this macro refers to the characters in the positon 4-6 in the string "abc123def", or "123".

The next, obvious part is to assign the .Font.Superscript property is True.

Now you want to generalize this so that you can apply it anywhere. The above code is "hardcoded" the Start and Length arguments. We need to make it dynamic. Easiest way to do this is to go 1 character at a time, and check to see if it's numeric, if so, apply the superscript.

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top