我正在创建一个电子表格,其列标题包括化学名称、CAS# 和化学式。我想格式化化学式,以便在输入化学式时自动为化学式上的数字添加下标。
答案1
来源:https://www.extendoffice.com/documents/excel/3759-excel-find-and-replace-subscript.html
Excel VBA 脚本
Sub SubscriptNumbers()
'UpdatebyExtendoffice20160705
Dim xRg As Range
Dim xTxt As String
Dim xCell As Range
Dim xChar As String
Dim I As Long
On Error Resume Next
If ActiveWindow.RangeSelection.Count > 1 Then
xTxt = ActiveWindow.RangeSelection.AddressLocal
Else
xTxt = ActiveSheet.UsedRange.AddressLocal
End If
Set xRg = Application.InputBox("Select a range:", "Kutools for Excel", xTxt, , , , , 8)
If xRg Is Nothing Then Exit Sub
Application.ScreenUpdating = False
For Each xCell In xRg
xTxt = xCell.Value
For I = 1 To Len(xTxt)
xChar = Mid(xTxt, I, 1)
If xChar >= "0" And xChar <= "9" Then
xCell.Characters(I, 1).Font.Subscript = True
End If
Next
Next
Application.ScreenUpdating = True
End Sub