December 26, 2021

Create Superscript and Subscript Macros

How to create Superscript and Subscript Macros to format Excel worksheet data. Watch the video below:

Create Superscript and Subscript Macros to format Excel worksheet data

Here’s the complete VBA code:
Option Explicit
Dim intBegin As Integer
Dim intLen As Integer

Private Sub cmdClearData_Click()
TextBox1.Text = “”
End Sub

Private Sub cmdGetDataFromSheet_Click()
TextBox1.Text = ActiveCell.Text
End Sub

Private Sub cmdNormal_Click()
intLen = TextBox1.SelLength

If intLen > 0 Then
intBegin = TextBox1.SelStart + 1
ActiveCell.Characters(intBegin, intLen).Font.Subscript = False
ActiveCell.Characters(intBegin, intLen).Font.Superscript = False
End If
End Sub

Private Sub cmdSubScript_Click()
intLen = TextBox1.SelLength

If intLen > 0 Then
intBegin = TextBox1.SelStart + 1
ActiveCell.Characters(intBegin, intLen).Font.Subscript = True
End If

End Sub

Private Sub cmdSuperScript_Click()
intLen = TextBox1.SelLength

If intLen > 0 Then
intBegin = TextBox1.SelStart + 1
ActiveCell.Characters(intBegin, intLen).Font.Superscript = True
‘ActiveCell.Characters(intBegin, intLen).Font.Superscript = True

End If

End Sub

Private Sub cmdUnload_Click()
Unload UserForm1
End Sub

This macro to create Superscript and Subscript text is useful for chemists, engineers, writers, etc. to write chemical formulas or references in an article.

Create Superscript and Subscript Macros for text formatting
Create Superscript and Subscript Macros for text formatting