人气 5044

[Excel技巧] excel 设置单元格设定字符为上标 [复制链接]

中夜栀子 2022-4-21 20:20:27
在工作生活中,因为有的行业的特殊性,有的单元格的部分数据会出现上标/下标。在数据录入过程中,我们可以设定单元格格式来设置为上标或者下标。
如果有大量的单元格中存在某个字符需要设置为上标,我们该如何批量设置?这里通过一段vba自定义函数,批量设定选中的单元格的某个字符作为上标,这里以“*”为例

1520471296797809.png

1520471296797809.png

Sub标上上标()
'先选择单元格(可以为多个),再运行此宏
DimTRanAsRange,FirstAddressAsString,FindStrAsString,i,j,k,l
FindStr="*" '标上上标的字符串
WithSelection
SetTRan=.Find(FindStr,LookIn:=xlValues)
IfNotTRanIsNothingThen
FirstAddress=TRan.Address
Do
i=(Len(TRan.Value)-Len(WorksheetFunction.Substitute(TRan.Value,FindStr,"")))/Len(FindStr)
k=1
Forj=1Toi
l=WorksheetFunction.Find(FindStr,TRan.Value,k)
TRan.Characters(Start:=l,Length:=Len(FindStr)).Font.Superscript=True
k=l+Len(FindStr)
Next
SetTRan=.FindNext(TRan)
LoopWhileNotTRanIsNothingAndTRan.AddressFirstAddress
EndIf
EndWith
EndSub
选中需要设置的单元格,运行代码:

1520471349919029.png

1520471349919029.png

参考至:excel吧
您需要登录后才可以回帖 登录 | 立即注册

QQ|手机版|精益人 ( 沪ICP备19004111号-1 )

GMT+8, 2024-5-19 05:37 , Processed in 0.228668 second(s), 22 queries .

Powered by Lean.ren X3.5 Licensed  © 2001-2030 LEAN.REN