在工作生活中,因為有的行業(yè)的特殊性,有的單元格的部分數(shù)據(jù)會出現(xiàn)上標/下標。
在數(shù)據(jù)錄入過程中,我們可以設定單元格格式來設置為上標或者下標。
如果有大量的單元格中存在某個字符需要設置為上標,我們該如何批量設置?
這里通過一段vba自定義函數(shù),批量設定選中的單元格的某個字符作為上標,這里以“*”為例
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吧