VBA Code to Check Prime Numbers


Function fnCheckPrimeNo(iNumber As Integer)
    
    'Declare variable
    Dim iCounter As Integer
    
    'Validation
    If iNumber <= 0 Then
        MsgBox "Please enter vailid number to proceed", vbInformation
        Exit Function
    End If
    
    'Prime no
    If iNumber = 1 Or iNumber = 2 Then
        fnCheckPrimeNo = "Prime Number"
        Exit Function
    End If
    
    'Looping through check prime number
    For iCounter = 2 To iNumber - 1
        
        If iNumber Mod iCounter = 0 Then
            fnCheckPrimeNo = "Not Prime Number"
            Exit Function
        End If
    Next
    
    fnCheckPrimeNo = "Prime Number"
    
End Function