Deleting Specific Spaces & Subscripting Issues

Naut05

New Member
Joined
Apr 26, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
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.)

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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Would be good to see some more examples of the strings in question to see if there is an easier method to extract.

Below is one example but there will be many more methods depending on the data:
Book1
AB
17.13 (1H, s, H2 -20a);2
Sheet1
Cell Formulas
RangeFormula
B1B1=LET( r,FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', '')='']"), --RIGHT(r,LEN(r)-1))
 
Upvote 0
Would be good to see some more examples of the strings in question to see if there is an easier method to extract.

Below is one example but there will be many more methods depending on the data:
Book1
AB
17.13 (1H, s, H2 -20a);2
Sheet1
Cell Formulas
RangeFormula
B1B1=LET( r,FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', '')='']"), --RIGHT(r,LEN(r)-1))

Thank you for your reply, here's a few more examples. The subscripted number will likely range from 1 to 9

12.01–10.98 (2H, m, H 2 -9);
5.19 (1H, dd, J = 6.0, 1.5 Hz, H 1 -20a);
5.15 (1H, s, H 1 -20b);
4.51 (2H, AB q, J = 17.2 Hz, H 2 -22);
4.01 (2H, d, J = 5.4 Hz, H 2 -17);
7.13–7.00 (1H, m, H 1 -18);
3.56 (3H, dt, J = 11.0, 1.5 Hz, H 3 -15);
 
Upvote 0
Is the space already there before the letter or has that been put in with code?
 
Upvote 0
Maybe the below will help with either:
Book1
AB
112.01–10.98 (2H, m, H2 -9);2
25.19 (1H, dd, J = 6.0, 1.5 Hz, H1 -20a);1
35.15 (1H, s, H1 -20b);1
44.51 (2H, AB q, J = 17.2 Hz, H2 -22);2
54.01 (2H, d, J = 5.4 Hz, H2 -17);2
67.13–7.00 (1H, m, H1 -18);1
73.56 (3H, dt, J = 11.0, 1.5 Hz, H3 -15);3
8
9
1012.01–10.98 (2H, m, H 2 -9);2
115.19 (1H, dd, J = 6.0, 1.5 Hz, H 1 -20a);1
125.15 (1H, s, H 1 -20b);1
134.51 (2H, AB q, J = 17.2 Hz, H 2 -22);2
144.01 (2H, d, J = 5.4 Hz, H 2 -17);2
157.13–7.00 (1H, m, H 1 -18);1
163.56 (3H, dt, J = 11.0, 1.5 Hz, H 3 -15);3
Sheet1
Cell Formulas
RangeFormula
B1:B7B1=LET(v,TEXTBEFORE(TAKE(TEXTSPLIT(A1,", "),,-1),"-"),--RIGHT(v,LEN(v)-1))
B10:B16B10=LET(v,TEXTBEFORE(TAKE(TEXTSPLIT(A10,", "),,-1),"-"),--TEXTAFTER(v," "))
 
Upvote 0
Would be good to see some more examples of the strings in question to see if there is an easier method to extract.

Below is one example but there will be many more methods depending on the data:
Book1
AB
17.13 (1H, s, H2 -20a);2
Sheet1
Cell Formulas
RangeFormula
B1B1=LET( r,FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', '')='']"), --RIGHT(r,LEN(r)-1))

Maybe the below will help with either:
Book1
AB
112.01–10.98 (2H, m, H2 -9);2
25.19 (1H, dd, J = 6.0, 1.5 Hz, H1 -20a);1
35.15 (1H, s, H1 -20b);1
44.51 (2H, AB q, J = 17.2 Hz, H2 -22);2
54.01 (2H, d, J = 5.4 Hz, H2 -17);2
67.13–7.00 (1H, m, H1 -18);1
73.56 (3H, dt, J = 11.0, 1.5 Hz, H3 -15);3
8
9
1012.01–10.98 (2H, m, H 2 -9);2
115.19 (1H, dd, J = 6.0, 1.5 Hz, H 1 -20a);1
125.15 (1H, s, H 1 -20b);1
134.51 (2H, AB q, J = 17.2 Hz, H 2 -22);2
144.01 (2H, d, J = 5.4 Hz, H 2 -17);2
157.13–7.00 (1H, m, H 1 -18);1
163.56 (3H, dt, J = 11.0, 1.5 Hz, H 3 -15);3
Sheet1
Cell Formulas
RangeFormula
B1:B7B1=LET(v,TEXTBEFORE(TAKE(TEXTSPLIT(A1,", "),,-1),"-"),--RIGHT(v,LEN(v)-1))
B10:B16B10=LET(v,TEXTBEFORE(TAKE(TEXTSPLIT(A10,", "),,-1),"-"),--TEXTAFTER(v," "))
Neither of those worked, unfortunately.

For the first formula you gave for 1 example, it just outputs 0, and the second formula doesn't do anything to the sheet. I think it might be because excel is weird when it comes to subscript? - when removed anything on either side with things like Find and Replace it undoes the subscript too. The final string needs to end up in a word doc anyway, so I've left the spaces until they're in Word and then using Find and Replace and ^c to replace it with just the subscripted number.

Thank you for your help, though.
 

Attachments

  • Excel1.jpg
    Excel1.jpg
    156.6 KB · Views: 5
Upvote 0
Welcome to the MrExcel board!

Is the space already there before the letter or has that been put in with code?
We did not get an answer to that so for the moment I have assumed that the original data does not have the space before (or after) the value to be subscripted. It is also unclear to me if the only letter that will have a value to be subscripted is "H".

In any case try this with a copy of your data to see if it is headed in the right direction. Its aim is to subscript any digits that immediately follow any upper case letter.

VBA Code:
Sub Make_Subscripts()
  Dim RX As Object, M As Object
  Dim a As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "[A-Z]\d+"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      For Each M In RX.Execute(a(i, 1))
        .Cells(i).Characters(M.firstindex + 2, Len(M) - 1).Font.Subscript = True
      Next M
    Next i
  End With
End Sub

My sample data

Naut05.xlsm
A
1
212.01–10.98 (2H, m, H2-9);
35.19 (1H, dd, J = 6.0, 1.5 Hz, H1-20a);
45.15 (1H, s, H1-20b);
54.51 (2H, AB q, J = 17.2 Hz, H2-22);
64.01 (2H, d, J = 5.4 Hz, H2-17);
77.13–7.00 (1H, m, H1-18);
83.56 (3H, dt, J = 11.0, 1.5 Hz, H3-15);
94.01 (2H3, d, J = 5.4 Hz, H32 -17);
104.01 (2BW333, d, J = 5.4 Hz, X32 -17);
Sheet4


Result of the above code

1682583310479.png
 
Upvote 1
Solution
Welcome to the MrExcel board!


We did not get an answer to that so for the moment I have assumed that the original data does not have the space before (or after) the value to be subscripted. It is also unclear to me if the only letter that will have a value to be subscripted is "H".

In any case try this with a copy of your data to see if it is headed in the right direction. Its aim is to subscript any digits that immediately follow any upper case letter.

VBA Code:
Sub Make_Subscripts()
  Dim RX As Object, M As Object
  Dim a As Variant
  Dim i As Long
 
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "[A-Z]\d+"
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      For Each M In RX.Execute(a(i, 1))
        .Cells(i).Characters(M.firstindex + 2, Len(M) - 1).Font.Subscript = True
      Next M
    Next i
  End With
End Sub

My sample data

Naut05.xlsm
A
1
212.01–10.98 (2H, m, H2-9);
35.19 (1H, dd, J = 6.0, 1.5 Hz, H1-20a);
45.15 (1H, s, H1-20b);
54.51 (2H, AB q, J = 17.2 Hz, H2-22);
64.01 (2H, d, J = 5.4 Hz, H2-17);
77.13–7.00 (1H, m, H1-18);
83.56 (3H, dt, J = 11.0, 1.5 Hz, H3-15);
94.01 (2H3, d, J = 5.4 Hz, H32 -17);
104.01 (2BW333, d, J = 5.4 Hz, X32 -17);
Sheet4


Result of the above code

View attachment 90547
My apologies, I should have clarified in my previous replies, the spaces were not part of the original data, as you assumed. They were added in my attempt to target that specific number.
And yes, only the number following the uppercase H needs to be subscripted, and it will always be an uppercase H.

Your code worked brilliantly! Thank you so much.
 
Upvote 0
You're welcome. Thanks for the follow-up.

only the number following the uppercase H needs to be subscripted, and it will always be an uppercase H.
If is was possible that the data contained another letter followed by a number (that should not be subscripted) then the 'pattern' line should be changed to
VBA Code:
RX.Pattern = "H\d+"

Example with this changed pattern line

Naut05.xlsm
A
1
212.01–10.98 (Z2, m, H2-9);
35.19 (1H, dd, J = 6.0, 1.5 Hz, H1-20a);
Sheet3


Result (the Z2 part is not altered)

1682596462801.png
 
Upvote 0

Forum statistics

Threads
1,215,056
Messages
6,122,907
Members
449,096
Latest member
dbomb1414

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