Last active
March 28, 2016 11:35
-
-
Save jschpp/cfaa67d587191a0e1e63 to your computer and use it in GitHub Desktop.
Taken from http://superuser.com/a/513117/37521
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Super_Sub() | |
' | |
' Keyboard Shortcut: Ctrl+Shift+D | |
' | |
' If the characters are surrounded by "<" & ">" then they will be subscripted | |
' If the characters are surrounded by "{" & "}" then they will be superscripted | |
' | |
Dim NumSub | |
Dim NumSuper | |
Dim SubL | |
Dim SubR | |
Dim SuperL | |
Dim SuperR | |
Dim CheckSub, CheckSuper as Boolean | |
Dim CounterSub, CounterSuper as Integer | |
Dim aCell, CurrSelection As Range | |
For Each c In Selection | |
c.Select | |
CheckSub = True | |
CounterSub = 0 | |
CheckSuper = True | |
CounterSuper = 0 | |
aCell = ActiveCell | |
' | |
NumSub = Len(aCell) - Len(Application.WorksheetFunction.Substitute(aCell, "<", "")) | |
NumSuper = Len(aCell) - Len(Application.WorksheetFunction.Substitute(aCell, "{", "")) | |
' | |
If Len(aCell) = 0 Then Exit Sub | |
If IsError(Application.Find("<", ActiveCell, 1)) = False Then | |
Do | |
Do While CounterSub <= 1000 | |
SubL = Application.Find("<", ActiveCell, 1) | |
SubR = Application.Find(">", ActiveCell, 1) | |
ActiveCell.Characters(SubL, 1).Delete | |
ActiveCell.Characters(SubR - 1, 1).Delete | |
ActiveCell.Characters(SubL, SubR - SubL - 1).Font.Subscript = True | |
CounterSub = CounterSub + 1 | |
If CounterSub = NumSub Then | |
CheckSub = False | |
Exit Do | |
End If | |
Loop | |
Loop Until CheckSub = False | |
End If | |
' | |
' | |
If IsError(Application.Find("{", ActiveCell, 1)) = False Then | |
Do | |
Do While CounterSuper <= 1000 | |
SuperL = Application.Find("{", ActiveCell, 1) | |
SuperR = Application.Find("}", ActiveCell, 1) | |
ActiveCell.Characters(SuperL, 1).Delete | |
ActiveCell.Characters(SuperR - 1, 1).Delete | |
ActiveCell.Characters(SuperL, SuperR - SuperL - 1).Font.Superscript = True | |
CounterSuper = CounterSuper + 1 | |
If CounterSuper = NumSuper Then | |
CheckSuper = False | |
Exit Do | |
End If | |
Loop | |
Loop Until CheckSuper = False | |
End If | |
' | |
Next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment