Create an UDF to get Odd, Even and Prime Numbers from a string

Luthius

Active Member
Joined
Apr 5, 2011
Messages
324
Guys
I would like your support on how to extract from a string the numbers and count the quantity of
Odd Numbers, Even Numbers, Prime Numbers.

The string can come this way with "-" as delimiter.
1-2-3-4-5-6-7-8-9-10-11-12-13-14-15
3-6-7-11-173-14

Code:
[COLOR=#0000cd]Public Function[/COLOR] MyUDF(strNumbers [COLOR=#0000cd]As String[/COLOR], returnType [COLOR=#0000cd]As Integer[/COLOR])[COLOR=#008000] 
  
   '1=Odd Numbers[/COLOR]
[COLOR=#008000]    '2=Even Numbers[/COLOR]
[COLOR=#008000]    '3=Prime Numbers[/COLOR]
    [COLOR=#0000cd]Select Case[/COLOR] returnType
    [COLOR=#0000cd]Case[/COLOR] 1
[COLOR=#008000]        'Will return the quantity of Odd numbers[/COLOR]
    [COLOR=#0000cd]Case[/COLOR] 2
[COLOR=#008000]        'Will return the quantity of Even numbers[/COLOR]
    [COLOR=#0000cd]Case[/COLOR] 3
[COLOR=#008000]        'Will return the quantity of Prime numbers[/COLOR]
[COLOR=#0000cd]    End Select[/COLOR]
    
[COLOR=#0000cd]End Function[/COLOR]
 
Last edited:
Thank you guys, just a last question as an additional item to this.
Is possible increment to this UDF a way to count the quantity of sequential numbers?

Example below that the higher quantity of number in sequence is 7
01-02-05-08-09-10-11-12-13-15-17-18-19-20-25

Another example
05 Numbers in sequence.
01-02-05-07-10-11-14-15-16-19-21-22-23-24-25
 
Upvote 0

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
Yes you are right.
6 numbers
01-02-05-08-09-10-11-12-13-15-17-18-19-20-25

05 Numbers in sequence.
01-02-05-07-10-11-14-15-16-19-21-22-23-24-25
 
Upvote 0
How about this?

Code:
Public Function MyUDF(strNumbers As String, returnType As Integer)
Dim SP() As String: SP = Split(strNumbers, "-")
Dim Total As Integer
   '1=Odd Numbers
    '2=Even Numbers
    '3=Prime Numbers
    
For i = LBound(SP) To UBound(SP)
    Select Case returnType
    Case 1
        If Int(SP(i)) Mod 2 = 1 Then Total = Total + 1
    Case 2
        If Int(SP(i)) Mod 2 = 0 Then Total = Total + 1
    Case 3
        If ISPRIME(Int(SP(i))) Then Total = Total + 1
    Case 4
        MyUDF = SEQUENCE(SP)
        Exit Function
    End Select
Next i

MyUDF = Total
    
End Function

Function SEQUENCE(SP() As String) As Long
Dim Total As Long: Total = 1
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")
Dim High As Long

For i = LBound(SP) + 1 To UBound(SP)
    If Int(SP(i)) - 1 = Int(SP(i - 1)) Then
        Total = Total + 1
    Else
        If Total > 1 Then AL.Add Total
        Total = 1
    End If
Next i

AL.Add Total

For j = 0 To AL.Count - 1
    If AL(j) > High Then High = AL(j)
Next j

SEQUENCE = High

End Function

Function ISPRIME(Num As Double) As Boolean
    Dim i As Double
    If Num = 1 Then ISPRIME = False: Exit Function
    If Num = 2 Then ISPRIME = True: Exit Function
    If Int(Num / 2) = (Num / 2) Then
        Exit Function
        Else
        For i = 3 To Sqr(Num) Step 2
            If Int(Num / i) = (Num / i) Then
                Exit Function
            End If
        Next i
    End If
    ISPRIME = True
End Function
 
Upvote 0
I tested and its giving an error #Value
It is one single string inside a cell like this
01-02-05-08-09-10-11-12-13-15-17-18-19-20-25
 
Upvote 0
Works fine for me. I'm calling the function like this
Code:
=MyUDF(A1,4)
 
Upvote 0
Yes you are right.
6 numbers
01-02-05-08-09-10-11-12-13-15-17-18-19-20-25

05 Numbers in sequence.
01-02-05-07-10-11-14-15-16-19-21-22-23-24-25
Just out of curiosity, are these examples representative of your actual values?

What is the largest number that could appear between delimiters?
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,300
Members
452,633
Latest member
DougMo

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