Formula to extract first character from right side but before the numeric data

mehidy1437

Active Member
Joined
Nov 15, 2019
Messages
348
Office Version
  1. 365
  2. 2016
  3. 2013
Platform
  1. Windows
  2. Mobile
  3. Web
Hi guys,

I want the below result from the string, how can I do this with formula?

Book1
AB
1STRINGREQUIRED RESULT
2BBBA123A
3BB1234B
4CC3C
5DD123CD
6F11CCD32CD
7F11CCE32CME
8F11CCF3CMF
9F11CCG345CG
Sheet1
Capture.PNG
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try the below formula in B2 and copy it into its below cells by dragging.
Excel Formula:
=IF(Len(A2)>7,MID(A2,6,1),IF(Len(A2)=7,MID(A2,4,1),MID(A2,2,1)))
 
Last edited:
Upvote 0
Try the below formula in B2 and copy it into its below cells by dragging.
Excel Formula:
=IF(Len(A2)>7,MID(A2,6,1),IF(Len(A2)=7,MID(A2,4,1),MID(A2,2,1)))
thanks for the response, but I'm looking for a dynamic formula coz string size can be changed.
it does not work for the below string.

BBddddBA123
 
Upvote 0
Could use VBA.

Book1
ABC
1STRINGREQUIRED RESULTCustom Function
2BBBA123AA
3BB1234BB
4CC3CC
5DD123CDD
6F11CCD32CDD
7F11CCE32CMEE
8F11CCF3CMFF
9F11CCG345CGG
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=fc(A2)


VBA Code:
Function FC(s As String)
Dim bt() As Byte: bt = s
Dim b As Boolean: b = False

For i = UBound(bt) - 1 To 0 Step -2
    If bt(i) > 47 And bt(i) < 58 Then
        b = True
    End If
    
    If bt(i) > 64 And bt(i) < 91 And b Then
        FC = Chr(bt(i))
        Exit Function
    End If
Next i
End Function
 
Upvote 0
thanks for the response, but I'm looking for a dynamic formula coz string size can be changed.
it does not work for the below string.
You didn't mention that.
We need the rule of how the result is calculated then.
 
Upvote 0
Could use VBA.

Book1
ABC
1STRINGREQUIRED RESULTCustom Function
2BBBA123AA
3BB1234BB
4CC3CC
5DD123CDD
6F11CCD32CDD
7F11CCE32CMEE
8F11CCF3CMFF
9F11CCG345CGG
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=fc(A2)


VBA Code:
Function FC(s As String)
Dim bt() As Byte: bt = s
Dim b As Boolean: b = False

For i = UBound(bt) - 1 To 0 Step -2
    If bt(i) > 47 And bt(i) < 58 Then
        b = True
    End If
   
    If bt(i) > 64 And bt(i) < 91 And b Then
        FC = Chr(bt(i))
        Exit Function
    End If
Next i
End Function
It's given me the perfect result, many thanks.

Isn't it possible to do with the excel formula, without VBA?
 
Upvote 0
This seems to work.

Book1
ABC
1STRINGREQUIRED RESULTFormula
2BBBA123AA
3BB1234BB
4CC3CC
5DD123CDD
6F11CCD32CDD
7F11CCE32CMEE
8F11CCF3CMFF
9F11CCG345CGG
Sheet1
Cell Formulas
RangeFormula
C2:C9C2=MID(A2,MAX((CODE(MID(A2,ROW(INDIRECT("1:"&LEN(A2)-1)),1))>=65)*(CODE(MID(A2,ROW(INDIRECT("1:"&LEN(A2)-1)),1))<=90)*(CODE(MID(A2,ROW(INDIRECT("2:"&LEN(A2))),1))>=48)*(CODE(MID(A2,ROW(INDIRECT("2:"&LEN(A2))),1))<=57)*ROW(INDIRECT("1:"&LEN(A2)-1))),1)
 
Upvote 0
It's not giving me the correct result in excel 2016

Formula to extract first character from right side but before the numeric data.xlsm
ABC
1STRINGREQUIRED RESULTFormula
2BBBA123A#VALUE!
3BB1234B#VALUE!
4CC3C#VALUE!
5DD123CD#VALUE!
6F11CCD32CDF
7F11CCE32CMEF
8F11CCF3CMFF
9F11CCG345CGF
Sheet3
Cell Formulas
RangeFormula
C2:C9C2=MID(A2,MAX((CODE(MID(A2,ROW(INDIRECT("1:"&LEN(A2)-1)),1))>=65)*(CODE(MID(A2,ROW(INDIRECT("1:"&LEN(A2)-1)),1))<=90)*(CODE(MID(A2,ROW(INDIRECT("2:"&LEN(A2))),1))>=48)*(CODE(MID(A2,ROW(INDIRECT("2:"&LEN(A2))),1))<=57)*ROW(INDIRECT("1:"&LEN(A2)-1))),1)
 
Upvote 0
I'm on 365. Might need to be entered as an array formula to work on 2016.

Click on cell C2 and hit the F2 key to edit the formula. Then hit Control+Shift+Enter and see if that makes it work.
 
Upvote 0
Solution
Many thanks, it's working fine with CSE, I just have added the UPPER function to avoid the lower case limitation in the formula.

I would like to have the formula without CSE in excel 2013/2016, if possible.

Formula to extract first character from right side but before the numeric data.xlsm
ABC
1STRINGREQUIRED RESULTFormulaCSE
2BBBAaaaaAc123AC
3BB1234BB
4CC3CC
5DD123CDD
6F11CCD32CDD
7F11CCE32CMEE
8F11CCF3CMFF
9F11CCGffffff3444444444444444444444444eee555CcccGE
Sheet3
Cell Formulas
RangeFormula
C2:C9C2=MID(UPPER(A2),MAX((CODE(MID(UPPER(A2),ROW(INDIRECT("1:"&LEN(UPPER(A2))-1)),1))>=65)*(CODE(MID(UPPER(A2),ROW(INDIRECT("1:"&LEN(UPPER(A2))-1)),1))<=90)*(CODE(MID(UPPER(A2),ROW(INDIRECT("2:"&LEN(UPPER(A2)))),1))>=48)*(CODE(MID(UPPER(A2),ROW(INDIRECT("2:"&LEN(UPPER(A2)))),1))<=57)*ROW(INDIRECT("1:"&LEN(UPPER(A2))-1))),1)
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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