我想在 Excel 中格式化文本的下标。由于我有很多文本,我想批量执行。请问是否有将文本转换为下标的函数?
我想使用功能,而不是热键。
例如,转换可变长度到最大。
答案1
为了实现这一点,您可以编写一个宏,然后从开发人员选项卡运行它。
有关如何运行宏或启用开发人员工具的更多信息,请参阅支持页面
'You can make the first character in the A1 cell subscript like this
Worksheets("Sheet1").Range("A1").Characters(0, 1).Font.Subscript = True
扩展示例
Sub SubscriptWithRegex()
'Create a new regex object, import reference from
'Tools > References > Microsoft VBScript Regular Expressions 5.5
Set regex = New RegExp
'Define the pattern, use regexr.com to test and write regex
'For this example we will subscript all L characters after a V
regex.Pattern = "V(L+)"
'if TRUE, find all matches, if FALSE find only the first match
regex.Global = True
'Iterate trough cells in Sheet1 range A1:A6
For Each cell In Worksheets("Sheet1").Range("A1:A6")
'Search for the pattern
Set matches = regex.Execute(cell.Value)
'Iterate trough the matches
For Each Match In matches
'Make characters subscript
'We need to shift the match to left by 2 to skip the V character
'and we need to subtract that single V from the total length
cell.Characters(Match.FirstIndex + 2, Match.Length - 1).Font.Subscript = True
Next
Next
End Sub
运行宏之前和之后
+----------+----------+ | 之前 | 之后 | +---+------+---+------+ | | 一个 | | 一个 | +---+------+---+------+ | 1 | 非常低廉 | 1 | 非常低廉| +---+------+---+------+ | 2 | VL | 2 | VL | +---+------+---+------+ | 3 | VVLL | 3 | VV LL | +---+------+---+------+ | 4 | VLVL | 4 | V L V L | +---+------+---+------+ | 5 | 微光 | 5 | 微光 | +---+------+---+------+ | 6 | V | 6 | V | +---+------+---+------+