Excel VBA
excel 设置单元格设定字符为上标
2018-03-08 09:27:02

在工作生活中,因为有的行业的特殊性,有的单元格的部分数据会出现上标/下标。

在数据录入过程中,我们可以设定单元格格式来设置为上标或者下标。

如果有大量的单元格中存在某个字符需要设置为上标,我们该如何批量设置?

这里通过一段vba自定义函数,批量设定选中的单元格的某个字符作为上标,这里以“*”为例

Sub 标上上标() '    先选择单元格(可以为多个),再运行此宏     Dim TRan As Range, FirstAddress As String, FindStr As String, i, j, k, l     FindStr = "*" '标上上标的字符串     With Selection         Set TRan = .Find(FindStr, LookIn:=xlValues)         If Not TRan Is Nothing Then             FirstAddress = TRan.Address             Do                 i = (Len(TRan.Value) - Len(WorksheetFunction.Substitute(TRan.Value, FindStr, ""))) / Len(FindStr)                 k = 1                 For j = 1 To i                     l = WorksheetFunction.Find(FindStr, TRan.Value, k)                     TRan.Characters(Start:=l, Length:=Len(FindStr)).Font.Superscript = True                     k = l + Len(FindStr)                 Next                 Set TRan = .FindNext(TRan)             Loop While Not TRan Is Nothing And TRan.Address <> FirstAddress         End If     End With End Sub

选中需要设置的单元格,运行代码:

参考至:excel吧