Forum Discussion
Need numbers from within cells
- May 27, 2023
Okay, please try this and see if this works for you.
Function ExtractNumber(ByVal str As String) As Variant Dim Matches As Object With CreateObject("VBScript.RegExp") .Global = False .Pattern = "\d+" If .test(str) Then Set Matches = .Execute(str) ExtractNumber = Matches(0) + 0 If (Len(ExtractNumber) < 7) Or (Len(ExtractNumber) > 9) Then ExtractNumber = "" End If End With End Function
Thanks for the help. I tried your code, it works.
However, it also is giving numbers for the following columns. I want to ignore these specific columns in case there isn't 7,9 (8 as well, which we missed in code) digits in a column. Lets say there are more than 9 digits in those columns.
Tq.
Okay, please try this and see if this works for you.
Function ExtractNumber(ByVal str As String) As Variant
Dim Matches As Object
With CreateObject("VBScript.RegExp")
.Global = False
.Pattern = "\d+"
If .test(str) Then
Set Matches = .Execute(str)
ExtractNumber = Matches(0) + 0
If (Len(ExtractNumber) < 7) Or (Len(ExtractNumber) > 9) Then ExtractNumber = ""
End If
End With
End Function
- bsrujanMay 27, 2023Copper Contributor
Oh yes, this works.
Seems as additional argument with if statement to keep b/w 7&9 is what made the difference. Tq again. Still learning VBA, this will work as feedback for me.
Have a great day ahead!
Best,
Srujan B
- Subodh_Tiwari_sktneerMay 27, 2023Silver Contributor
You're welcome bsrujan! Glad it worked for you. Thanks! You too.
Please take a minute to accept the proposed answer as the Best Response to mark your question as Solved.
- bsrujanMay 27, 2023Copper ContributorIts done, tq Subodh!