Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to check if the number is prime number in Excel?

As we all known, a prime number is a natural number which contains only two distinct natural number divisors: One and itself. If you have a list of numbers in a worksheet, how could you check if the numbers are prime numbers?

Check if a number is prime number with array formula

Check if a number is prime number with User Defined Function


arrow blue right bubble Check if a number is prime number with array formula


The following formula can help you identify the number whether it is a prime number or not, please do as this:

1. Enter the following formula into a blank cell – C2 for example beside your data:

=IF(A2=2,"Prime",IF(AND(MOD(A2,ROW(INDIRECT("2:"&ROUNDUP(SQRT(A2),0))))<>0),"Prime","Not Prime")) (A2 is the cell contains the number that you want to check), and then press Ctrl + Shift + Enter keys together, and you will get the result, if the number is prime, it will display “Prime” in the cell, if not, “Not Prime” will be displayed, see screenshot:

doc check if prime 1

2. Then select the cell C2, and drag the fill handle down to the cells which you want to apply this formula, and all the numbers will be identified if is prime number or not. See screenshot:

doc check if prime 2


arrow blue right bubble Check if a number is prime number with User Defined Function

The following User Defined Function also can help you to check the numbers if prime or not, please do as follows:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Check if a number is prime or not:

Function CheckPrime(Numb As Single) As Boolean
'Updateby Extendoffice 20160614
    Dim X As Long
    If Numb < 2 Or (Numb <> 2 And Numb Mod 2 = 0) _
     Or Numb <> Int(Numb) Then Exit Function
    For X = 3 To Sqr(Numb) Step 2
        If Numb Mod X = 0 Then Exit Function
    Next
    CheckPrime = True
End Function

3. Then save and close this code, go back to the worksheet, and enter this formula: =checkprime(A2) into a blank cell beside your number list, and then drag the fill handle down to the cells which you want to contain this formula, if the number is prime number, it will display TRUE, if not, it will display FALSE, see screenshot:

doc check if prime 3


Recommended Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 71%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 82% Excel problems.

  •  Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  •  Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  •  Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  •  Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  •  No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  •  Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  •  60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  •  Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Nathan · 7 days ago
    There's a reference error when you type in numbers that have more than 12 digits.
  • To post as a guest, your comment is unpublished.
    vaibhav · 4 months ago
    well 4095 is not a prime. still it gives result as prime no though. its incorrect.
    • To post as a guest, your comment is unpublished.
      skyyang · 4 months ago
      Hello, vaibhav,
      When you pasting above formula, you should press Ctrl + Shift + Enter keys together, not just Enter key, please try it again.
  • To post as a guest, your comment is unpublished.
    Jan Smetana (Xsoft) · 1 years ago
    Your formula is not working. Eg. 1681 is NOT a prime but by formula it is.
    =IF(A2=2,"Prime",IF(AND(MOD(A2,ROW(INDIRECT("2:"&ROUNDUP(SQRT(A2),0))))<>0),"Prime","Not Prime"))
  • To post as a guest, your comment is unpublished.
    John Kennedy Aquino · 1 years ago
    [b]this is my fastest version[/b]

    Sub generateprimenumbersbetween3()
    starting_number = 1 'input value here
    last_number = 30000 'input value here
    primenumbers = ""
    For a = starting_number To last_number
    c = a
    For b = 2 To c
    If a Mod b = 0 And c b Then
    Exit For
    Else
    If b = c Then
    primenumbers = primenumbers & " " & a
    Exit For
    Else
    If Round(a / (b + 1)) + 1 > b Then
    c = Round(a / (b + 1)) + 1
    End If
    End If
    End If
    Next b
    Next a
    MsgBox primenumbers
    End Sub

    Sub ISPRIME3()
    number_to_be_checked = 2000000000 'input value here
    c = number_to_be_checked
    For b = 2 To c
    If number_to_be_checked Mod b = 0 And c b Then
    MsgBox "Not Prime. Divisible by " & b
    Exit Sub
    Else
    If b = c Then
    MsgBox "Prime"
    Exit Sub
    Else
    If Round(number_to_be_checked / (b + 1)) + 1 > b Then
    c = Round(number_to_be_checked / (b + 1)) + 1
    End If
    End If
    End If
    Next b
    End Sub
  • To post as a guest, your comment is unpublished.
    Rick · 1 years ago
    The user defined function does not seem to work for numbers above ~16777213
    • To post as a guest, your comment is unpublished.
      John Kennedy Aquino · 1 years ago
      Sub generateprimenumbersbetween()
      starting_number = 99990 'input value here
      last_number = 99999 'input value here
      primenumbers = ""
      For a = starting_number To last_number
      For b = 2 To a
      If a - b * Int(a / b) = 0 And a b Then
      Exit For
      Else
      If a = b Then
      primenumbers = primenumbers & " " & a
      End If
      End If
      Next b
      Next a
      MsgBox primenumbers
      End Sub

      Function primenumbersbetween(starting_number, last_number)
      primenumbers = ""
      For a = starting_number To last_number
      For b = 2 To a
      If a - b * Int(a / b) = 0 And a b Then
      Exit For
      Else
      If a = b Then
      primenumbers = primenumbers & " " & a
      End If
      End If
      Next b
      Next a
      primenumbersbetween = primenumbers
      End Function

      'to check if a number is prime
      Sub ISPRIME()
      number_to_be_checked = 102 'input value here
      For b = 2 To number_to_be_checked
      If number_to_be_checked - b * Int(number_to_be_checked / b) = 0 And _
      number_to_be_checked b Then
      MsgBox "Not Prime. Divisible by " & b
      Exit Sub
      Else
      If number_to_be_checked = b Then
      MsgBox "Prime"
      End If
      End If
      Next b
      End Sub

      'to check if a number is prime
      Function ISPRIME2(number_to_be_checked)
      For b = 2 To number_to_be_checked
      If number_to_be_checked - b * Int(number_to_be_checked / b) = 0 And _
      number_to_be_checked b Then
      ISPRIME2 = "Not Prime. Divisible by " & b
      Exit Function
      Else
      If number_to_be_checked = b Then
      ISPRIME2 = "Prime"
      End If
      End If
      Next b
      End Function