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

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

JackDanIce

Well-known Member
Joined
Feb 3, 2010
Messages
9,821
Office Version
  1. 365
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,133,244
Messages
5,657,582
Members
418,401
Latest member
B_A_M155

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