Results 1 to 3 of 3

Format some numeric characters in cell as subscript

Threaded View

  1. #1
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Format some numeric characters in cell as subscript

    I thought I had this solved but an inconsistency has shown up.
    I have a long list of chemical formulas that I want to format (partially) as subscript.
    Basically what I need the macro to do is look at each character within a cell and check to see if it is numeric. If it is AND it follows a non-numeric character it should be formatted as subscript.

    Examples
    H2O the 2 should be subscript
    H2SO4 the 2 and the 4 should be subscript
    2CCl4 only the 4 should be subscript

    The following code takes care of the requirements for those examples

    For Each rng In Selection
    s = rng.Value
        For c = 2 To Len(s)
        If IsNumeric(Mid(s, c, 1)) And IsNumeric(Mid(s, c - 1, 1)) = False Then
            rng.Characters(c, 1).Font.Subscript = True
        Else
        End If
        Next c
    Next rng
    End Sub
    But it fails with the following example:
    CuSO4 - 5H2O
    because, although it correctly subscripts the 4 and the 2, it incorrectly formats the 5 as subscript even though it is not preceded by a number
    The code apparently treats the space, chr(32), preceding the 5 as numeric

    I tried adding the following If Statement to the code (right before the "Next c") in an attempt to undo what the first If Statement did but it made no difference.
    If IsNumeric(Mid(s, c, 1)) And Mid(s, c - 1, 1) = Chr(32) Then
            rng.Characters(c, 1).Font.Subscript = False
    Else
    End If
    Is there another way to write the second condition of the original If Statement to check the preceding character and if it is less than chr(48) OR greater than chr(57) then the subscript format is applied to the numeric character? Or does anyone have a completely different approach?

    Thanks
    Last edited by Cutter; 02-16-2010 at 04:33 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1