Extract only first available numeric from a string using access vba

Harshitha B M

New Member
Joined
Aug 7, 2022
Messages
1
Office Version
  1. 2010
Platform
  1. Windows
For an example if column value is "ABC 123 981" need to extract only 123... like so if its "456_wert" need to extract only 456 using access VBA code. Can somebody please help on this.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
One example.
VBA Code:
Sub Example()
    Dim RX As Object
    Dim S As String, ValStr As String
    Dim Msg As String
    
    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.Pattern = "[^0-9 _]"
    
    S = "ABC_123 981"
    ValStr = Split(Trim(RX.Replace(Replace(S, "_", " "), "")), " ")(0)
    Msg = "String: " & S & vbCr & "Value: " & ValStr
    
    Msg = Msg & vbCr & vbCr
    
    MsgBox Msg
End Sub

(Note: I am assuming late binding is possible under the Access object model as it is for Excel and Word)
 
Upvote 0
Yes, it is possible. Likely the code needs to return a value, which subs cannot do, so it might need to be a function. If that function is written to accept a parameter, then that might be better than hard coding the string. However, I get that the code is only for providing an excellent solution (I have tried to learn RegEx but it just doesn't seem to sink in) so well done! As a function:
VBA Code:
Function Example(strIn As String) As Long 'assumes number type of Long is wanted vs anything else
    Dim RX As Object

    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.Pattern = "[^0-9 _]"
    Example = Split(Trim(RX.Replace(Replace(strIn, "_", " "), "")), " ")(0)
    
End Function
How it might be called: myVariable = example("ABC 123 981")
or if a return message is needed:
MsgBox Example("ABC 123 981")
 
Upvote 0
Yes, it is possible. Likely the code needs to return a value, which subs cannot do, so it might need to be a function. If that function is written to accept a parameter, then that might be better than hard coding the string. However, I get that the code is only for providing an excellent solution (I have tried to learn RegEx but it just doesn't seem to sink in) so well done! As a function:
VBA Code:
Function Example(strIn As String) As Long 'assumes number type of Long is wanted vs anything else
    Dim RX As Object

    Set RX = CreateObject("VBScript.RegExp")
    RX.Global = True
    RX.Pattern = "[^0-9 _]"
    Example = Split(Trim(RX.Replace(Replace(strIn, "_", " "), "")), " ")(0)
  
End Function
How it might be called: myVariable = example("ABC 123 981")
or if a return message is needed:
MsgBox Example("ABC 123 981")

Yes, it should probably be a function, but the OP did not explain anything about how it was going to be used, so I wanted a simple example to focus on the string extraction. Hence the hard-coding.
 
Upvote 0

Forum statistics

Threads
1,216,037
Messages
6,128,442
Members
449,453
Latest member
jayeshw

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