Extract Numbers from Cell if Number is 3 or 4 digits in access programming

teco1

New Member
Joined
May 9, 2016
Messages
10
Dear All

Need your support. I want to extract Number from a cell if that number is 3 or 4 digits. I want to do it in access using sql so that i can call this function in my query.

For example

DUM_NS_3808_38081_F5_A
BLK_DUM_NS_0512_05122_F6_A

The Output should be
3808
512

Many Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Will each entry have only one 3 or 4 digit number, or could they have more than one?
I would probably create a User Defined Function in VBA to do this. You can use these in Queries.
If this a valid option for you?
 
Upvote 0
Here is a UDF that will return all 3 or 4 digit numbers, no matter how many exist (if multiple, they are separated by commas)
Code:
Function GetNumber(myString As String) As String

    Dim myArray() As String
    Dim i As Long
    Dim mySubWord As String
    Dim myReturn As String

'   Create array of values, when delimited by underscore
    myArray = Split(myString, "_")
    
'   Loop through all delimited values and build string of matching values
    For i = LBound(myArray) To UBound(myArray)
        mySubWord = myArray(i)
        If (Len(mySubWord) = 3 Or Len(mySubWord) = 4) And IsNumeric(mySubWord) Then
            myReturn = myReturn & mySubWord & ","
        End If
    Next i
    
'   Remove last comma from string
    If Len(myReturn) > 1 Then GetNumber = Left(myReturn, Len(myReturn) - 1)
        
End Function
So, in your query, you would just use it like any other Function in a Calculated field, i.e.
MyNumbers: GetNumber([FieldName])
 
Last edited:
Upvote 0
Thanks Joe4 for your response. It works great.
Is it possible to return First 3 or 4 digits which are separated by an "_" . Appreciate if this could.


I am using below mention UDF in excel and works fine and return First 3 or 4 digits.

Function Get_Store(oCell As Range) As Long
Dim aryWords, strWord
aryWords = Split(oCell.Value, "_")
For Each strWord In aryWords
If IsNumeric(strWord) Then
If Len(strWord) = 3 Or Len(strWord) = 4 Then
Get_Store = CLng(strWord)
Exit For
End If
End If
Next
End Function
 
Upvote 0
Is it possible to return First 3 or 4 digits which are separated by an "_" . Appreciate if this could.
Try using the code I posted instead. It does that precise thing.
 
Upvote 0
your code return all 3 or 4 digit numbers, no matter how many exist (if multiple, they are separated by commas)
But i need to return First 3 or 4 digits that are seperated by an underscore. Thanks
 
Upvote 0
This variation will stop once it finds one and return that one only:
Code:
Function GetNumber(myString As String) As String

    Dim myArray() As String
    Dim i As Long
    Dim mySubWord As String

'   Create array of values, when delimited by underscore
    myArray = Split(myString, "_")
    
'   Loop through all delimited values and build string of matching values
    For i = LBound(myArray) To UBound(myArray)
        mySubWord = myArray(i)
        If (Len(mySubWord) = 3 Or Len(mySubWord) = 4) And IsNumeric(mySubWord) Then
            GetNumber = mySubWord
            Exit For
        End If
    Next i
    
End Function
 
Upvote 0
You are welcome!
I think that was the first time I used the Split function and Arrays of undetermined size in Access VBA. It was kind of fun!
:)
 
Upvote 0

Forum statistics

Threads
1,221,774
Messages
6,161,862
Members
451,725
Latest member
durzoblint87

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