Macro with .subscript = FALSE doesn't change the text format

NiMip

Board Regular
Joined
Oct 9, 2017
Messages
167
Hi there, pretty new to macros and VBA, but I'm fairly confident this is strange behaviour.

I wrote a little macro to toggle the last letter of the selected cell to superscript, to help make m^2 easier to format. I've pasted it below and it works perfectly.
Only thing is, I went to do the same for subscript, also pasted below, and it'll turn it to subscript but won't turn it back to normal again. It's like subscript = FALSE behaves differently to .superscript = FALSE. Anyone got an idea why?
Cheers!



Sub SuperScrLastChar()
'Turn last character in string to superscript​
With Selection.Characters(Start:=Len(ActiveCell.Value), Length:=1).Font​
If .Superscript = False Then​
.Superscript = True​
Else​
.Superscript = False​
End If​
End With​
End Sub


Sub SubScrLastChar()
' Turn last character in string to subscript

With Selection.Characters(Start:=Len(ActiveCell.Value), Length:=1).Font
If .Subscript = False Then​
.Subscript = True​
Else​
.Subscript = False​
End If​
End With​
End Sub
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
That's a weird quirk and no idea why; there seems to be some issue with the Superscript property, despite being Subscript you're trying to undo.

Found this solution by trial and error, that does toggle the Subscript property on/off. Curious to see other comments to this thread for explanation:
Rich (BB code):
Sub SubScrLastChar()
' Turn last character in string to subscript
With Selection.Characters(start:=Len(ActiveCell.Value), Length:=1).Font
    If .Subscript = False Then
        .Subscript = True
    Else
        .Subscript = False
        .Superscript = False
    End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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
Back
Top