Hi there,
I have a column with a mix of letters and numbers in each cell and I need one specific number to be subscript, even when there are other instances of that number present (e.g. I need the first 2 in H2-2 subscript but everything else normal case). I tried to target the first 2 by subscripting "H2" with Font.Subscript = True and then running another code with Font.Subscript = False for the "H" but that didn't do anything (no error, it just didn't change anything on the sheet). I tried to go around that by placing spaces on either side of the target 2 and then targeting " 2 " for the subscript but I'm now trying to delete those two spaces only, leaving the other spaces in the cell.
I've also tried subscripting at a specific character position but the number of characters and the specific character position of the spaces and the subscripted number vary from cell to cell.
The whole cell's contents would be something like "7.13 (1H, s, H *2* -20a);" with the asterixes indicating the subscript.
From what I've read, you can trim all, trailing or leading spaces using Trim and its variants, but that doesn't really fit my case. Is there anyway to delete those two spaces while leaving all the others? Maybe using Replace? They're the two right most spaces in the cell if that helps but they're not on the end of the string.
Or is there a way to better target the subscripting of the number?
Code I used to subscript the " 2 ": (not mine, I found it on a forum or site that used it to italicize a certain character, I repurposed it for subscripting instead.)
I used the same code to try and remove the subscript on the H by changing it to the following, but it did nothing to the sheet.
Many thanks in advance
I have a column with a mix of letters and numbers in each cell and I need one specific number to be subscript, even when there are other instances of that number present (e.g. I need the first 2 in H2-2 subscript but everything else normal case). I tried to target the first 2 by subscripting "H2" with Font.Subscript = True and then running another code with Font.Subscript = False for the "H" but that didn't do anything (no error, it just didn't change anything on the sheet). I tried to go around that by placing spaces on either side of the target 2 and then targeting " 2 " for the subscript but I'm now trying to delete those two spaces only, leaving the other spaces in the cell.
I've also tried subscripting at a specific character position but the number of characters and the specific character position of the spaces and the subscripted number vary from cell to cell.
The whole cell's contents would be something like "7.13 (1H, s, H *2* -20a);" with the asterixes indicating the subscript.
From what I've read, you can trim all, trailing or leading spaces using Trim and its variants, but that doesn't really fit my case. Is there anyway to delete those two spaces while leaving all the others? Maybe using Replace? They're the two right most spaces in the cell if that helps but they're not on the end of the string.
Or is there a way to better target the subscripting of the number?
Code I used to subscript the " 2 ": (not mine, I found it on a forum or site that used it to italicize a certain character, I repurposed it for subscripting instead.)
VBA Code:
Sub SubscriptAttempt3
Dim c As Range, n As Long, m As Long
For Each c In Range("AG4", Range("AG" & Rows.Count).End(3))
n = InStr(1, c.Value, " 2 ", vbTextCompare)
If n > 0 Then
m = InStr(1, c.Value, "-", vbTextCompare)
If m = 0 Then m = Len(c.Value) Else m = m - n
c.Characters(Start:=n, Length:=m).Font.Subscript = True
End If
Next c
End Sub
I used the same code to try and remove the subscript on the H by changing it to the following, but it did nothing to the sheet.
VBA Code:
Sub SubscriptAttempt3
Dim c As Range, n As Long, m As Long
For Each c In Range("AG4", Range("AG" & Rows.Count).End(3))
n = InStr(1, c.Value, "H", vbTextCompare)
If n > 0 Then
m = InStr(1, c.Value, "2", vbTextCompare)
If m = 0 Then m = Len(c.Value) Else m = m - n
c.Characters(Start:=n, Length:=m).Font.Subscript = False
End If
Next c
End Sub
Many thanks in advance