I have strange behaviour with a Macro to toggle a Subscript in an Excel 2010 Cell.
My Macro will toggle the values show in Row 1 to have the final character of the string in the selected cell converted to a Subscript as shown in Row 3.
However, trying to reverse the process works sometimes, but rarely.
Here is my VBA code for the Macro:
Sub Subscript() Dim sFontName As String Dim sFontStyle As String Dim sFontSize As Long Dim sStringLength As Long Dim sSubscript As Boolean Dim Msg, Button, Title, Response As String Button = vbExclamation ActiveCell.Select sStringLength = Len(ActiveCell) sFontName = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Name sFontStyle = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.FontStyle sFontSize = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Size sSubscript = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Subscript Title = “Test for Subscript 0 …” Msg = “Font Name: ” & sFontName & vbCrLf & _ “Font Style: ” & sFontStyle & vbCrLf & _ “Font Size: ” & sFontSize & vbCrLf & _ “String Length: ” & sStringLength & vbCrLf & _ “00 Subscript: ” & sSubscript Response = MsgBox(Msg, Button, Title) If sSubscript Then sSubscript = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Subscript Title = “Test for Subscript Initially True 1.0…” Msg = “Before Subscript Toggle: ” & sSubscript Response = MsgBox(Msg, Button, Title) ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Subscript = False sSubscript = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Subscript Title = “Test for Subscript Initially True 1.1…” Msg = “After Subscript Toggle: ” & sSubscript Response = MsgBox(Msg, Button, Title) Else sSubscript = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Subscript Title = “Test for Subscript Initially False 2.0 …” Msg = “Before Subscript Toggle: ” & sSubscript Response = MsgBox(Msg, Button, Title) ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Subscript = True sSubscript = ActiveCell.Characters(Start:=sStringLength, Length:=1).Font.Subscript Title = “Test for Subscript Initially False 2.1 …” Msg = “After Subscript Toggle: ” & sSubscript Response = MsgBox(Msg, Button, Title) End If End Sub
The code has numerous messages in it to display the status of the action, hence it looks long and complicated – it’s not really.
Any bright ideas on why this code is not working correctly?
Thanks in anticipation for all assistance.
Trevor