Change Subscript to Markup Value

joand

Active Member
Joined
Sep 18, 2003
Messages
267
The code below converts a string to chemical formula (subscript numeric characters). If I want to change the subscript characters to a markup character ("_") what should I do? For example CH2CH2CH3 to CH_2CH_2CH_3, or C6H11 to C_6H_1_1

Code:
Sub test()

For x = 1 To Len(ActiveCell)
    char = Mid(ActiveCell, x, 1)
    If Asc(char) >= 48 And Asc(char) <= 57 Then ActiveCell.Characters(Start:=x, Length:=1).Font.Subscript = True
Next x

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Change subscript or add underscore before subscript?
 
Upvote 0
Or add an underscore before each digit of each subscript?
 
Upvote 0
Add underscore before each numeric value. but for double numeric digits such H11, each has to be added an underscore, H_1_1.

just like in the output above.
 
Upvote 0
UDF
Code:
Function Subscr(rng As Range) As String

    Dim s As String, i As Integer, char As String
    
    Application.Volatile
    
    For i = 1 To Len(rng)

        char = Mid(rng, i, 1)

        If IsNumeric(char) Then
            s = s & "_" & char
        Else
            s = s & char
        End If

    Next

    Subscr = s

End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,286
Members
452,902
Latest member
Knuddeluff

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