Macro for conditional superscripts in Excel

phatcat43

New Member
Joined
Jun 5, 2008
Messages
4
Hi there-
I often work with spreadsheets that need superscripts to identify statistical significance (with letters). Is there a way to create a macro that will convert all letters within highlighted cells into superscripts?

Thanks
david
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

KWMSeattle

Board Regular
Joined
Aug 23, 2006
Messages
149
Highlight the cells necessary and then run this macro:

Code:
Sub AddSuperScript()
 
    With Selection.Font
        .SuperScript = True
    End With
 
End Sub
 
Upvote 0

phatcat43

New Member
Joined
Jun 5, 2008
Messages
4
Thanks, but its not quite what I'm looking for. I should have explained that the cells have both numbers and letters, and I want to keep the numbers as regular font and convert only the letters to superscripts.
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try

Code:
Sub SuperS()
Dim c As Range, iChr As Integer
For Each c In Selection
    For iChr = 1 To Len(c.Value)
        If Val(Mid(c.Value, iChr, 1)) = 0 Then c.Characters(Start:=iChr, Length:=1).Font.Superscript = True
    Next iChr
Next c
End Sub
 
Upvote 0

KWMSeattle

Board Regular
Joined
Aug 23, 2006
Messages
149
Great solution, VoG! This was an interesting challenge. I love learning new tricks like this. :)
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Perhaps this will give you a start.
Code:
Sub Macro1()
Dim c As Range
Dim rng As Range
Dim I As Long
    Set rng = Range("A1:A10")
    
    For Each c In rng
        For I = 1 To Len(c.Value)
            If UCase(Mid(c.Value, I, 1)) Like "[A-Z]" Then
                c.Characters(Start:=I, Length:=1).Font.Superscript = True
            End If
        Next I
    Next c
    
End Sub
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Great solution, VoG! This was an interesting challenge. I love learning new tricks like this. :)


Thank you :)

Actually, this is somewhat more transparent

Code:
If Not IsNumeric(Mid(c.Value, iChr, 1)) Then c.Characters(Start:=iChr, Length:=1).Font.Superscript = True
 
Upvote 0

phatcat43

New Member
Joined
Jun 5, 2008
Messages
4
Thanks, that works great except for one thing - it turns 0 into a superscript. Anyway to avoid that?

David
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Thanks, that works great except for one thing - it turns 0 into a superscript. Anyway to avoid that?

David

Try

Code:
Sub SuperS()
Dim c As Range, iChr As Integer
For Each c In Selection
    For iChr = 1 To Len(c.Value)
        If Not IsNumeric(Mid(c.Value, iChr, 1)) Then c.Characters(Start:=iChr, Length:=1).Font.Superscript = True
    Next iChr
Next c
End Sub
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Vog

Are you sure about that?:eek:

Your first code does seem to superscript 0 but the amendment you posted seems to sort that out.:)
 
Upvote 0

Forum statistics

Threads
1,191,669
Messages
5,987,949
Members
440,121
Latest member
eravella

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top