UDF Function not working

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hey everyone,

I have a UDF that would collect a set of 9 numeric digits.

For instance:
A1 = Hello123456789?/ok
B1 = GetNumber(A1)

UDF result:
B1 = 123456789

Code:
Function GetNumber(ByVal S As String) As Variant
Dim X As Long, Num As Variant
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  For Each Num In Split(Application.Trim(S))
    If Len(Num) > 8 Then
      If GetNumber > 0 Then
        GetNumber = CVErr(xlErrValue) 
        Exit Function
      Else
        GetNumber = CLng(Num)
      End If
    End If
  Next

End Function

But now I am trying to retrieve bigger values like
A1 = 23ALPHA100050822112345678930047839750ZEBRA36
B1 = GetNumber(A1)

UDF result:
B1 = #VALUE !

Required result:
B1 = 100050822112345678930047839750[TABLE="width: 460"]
<tbody>[TR]
[TD="width: 460"]Will appreciate any help.

Thank you.[/TD]
[/TR]
</tbody>[/TABLE]


 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try to change the below line of your code

Code:
'From this
GetNumber = CLng(Num)

'To This
 GetNumber = CStr(Num)
 
Upvote 0
Here is a UDF (user defined function) that will return the largest number in the string passed into it...
Code:
[table="width: 500"]
[tr]
	[td]Function LargestNumber(ByVal S As String) As String
  Dim X As Long, Nums() As String
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  Nums = Split(Application.Trim(S))
  For X = 0 To UBound(Nums)
    If Len(Nums(X)) > Len(LargestNumber) Then LargestNumber = Nums(X)
  Next
End Function[/td]
[/tr]
[/table]
 
Upvote 0
Thank you for your suggestions, appreciate it.
 
Upvote 0

Forum statistics

Threads
1,224,984
Messages
6,182,136
Members
453,092
Latest member
dcasuga

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