Results 1 to 5 of 5

Thread: Need VBA code to display 1st PRIME number greater than value (N)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    VBAX Expert
    Joined
    Aug 2004
    Posts
    818
    Location
    using what Erik posted (too funny), this is what I came up

    Private Function IsDecimal(Number) As Boolean
        Dim splitted() As String
        splitted = Split(Number, ".")
        If UBound(splitted) <> 0 Then
            IsDecimal = True
        End If
    End Function
    Private Function IsPrimeNumber(Number) As Boolean
        For i = 2 To Number - 1
            If IsDecimal(Number / i) = False Then
                IsPrimeNumber = False
                Exit Function
            End If
        Next i
        IsPrimeNumber = True
    End Function
    Public Function NextPrime(Number As Long) As Long
        Dim lCheckPrimeNumber As Long
        Dim bPrime As Boolean
        
        lCheckPrimeNumber = Number
        Do
            lCheckPrimeNumber = lCheckPrimeNumber + 1
            bPrime = IsPrimeNumber(lCheckPrimeNumber)
            If bPrime Then
                NextPrime = lCheckPrimeNumber
            End If
        Loop While Not bPrime
    End Function
    Last edited by JKwan; 06-05-2014 at 08:43 AM. Reason: made it better

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •