VBA to print prime numbers between 2 numbers with msgbox

nhakimmm

New Member
Joined
Dec 12, 2021
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi gurus, I need a bit of help...

I need a VBA code that:
1. User inputs any 2 numbers
2. Msgbox containing only the prime number appears between the number that the user inputs.

E.g.

input box 1: please enter a number (e.g. 3)
input box 2: please enter a second number (e.g 7)

Output:
msgbox: 3 is a prime number . OK
msgbox: 5 is a prime number . OK
msgbox: 7 is a prime number . OK


Thank you
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
This is rather brute force but it gets the job done and is fast for numbers that don't get too high.

Are you sure that you want a separate MsgBox for each prime number? They could all be listed at once.

Also note that this is for positive integers greater than or equal to 2.
VBA Code:
Public Sub ListPrimes()

   Dim Lower As Long, Upper As Long
   Dim i As Long
   
   Lower = InputBox(prompt:="Please enter first number >= 2")
   Upper = InputBox(prompt:="Please enter second number > first number")
   
   If Lower < 2 Then
      MsgBox "First number must be greater than or equal to 2"
   ElseIf Upper <= Lower Then
      MsgBox "Second number must be greater than first number"
   Else
   
      For i = Lower To Upper
      
         If IsPrime(i) Then
            MsgBox i & " is a prime number"
         End If
      
      Next i

   End If
   
End Sub

Public Function IsPrime(n As Long) As Boolean

   Dim d As Long
   
   IsPrime = True
      
   For d = 2 To n \ 2
      If n Mod d = 0 Then
         IsPrime = False
         Exit Function
      End If
   Next d

End Function
 
Upvote 0
This is rather brute force but it gets the job done and is fast for numbers that don't get too high.

Are you sure that you want a separate MsgBox for each prime number? They could all be listed at once.

Also note that this is for positive integers greater than or equal to 2.
VBA Code:
Public Sub ListPrimes()

   Dim Lower As Long, Upper As Long
   Dim i As Long
  
   Lower = InputBox(prompt:="Please enter first number >= 2")
   Upper = InputBox(prompt:="Please enter second number > first number")
  
   If Lower < 2 Then
      MsgBox "First number must be greater than or equal to 2"
   ElseIf Upper <= Lower Then
      MsgBox "Second number must be greater than first number"
   Else
  
      For i = Lower To Upper
     
         If IsPrime(i) Then
            MsgBox i & " is a prime number"
         End If
     
      Next i

   End If
  
End Sub

Public Function IsPrime(n As Long) As Boolean

   Dim d As Long
  
   IsPrime = True
     
   For d = 2 To n \ 2
      If n Mod d = 0 Then
         IsPrime = False
         Exit Function
      End If
   Next d

End Function

Hi 6StringJazzer, thanks for taking the time to solve this... unfortunately one of my assignment requirement was to have a separate msgbox for every outcome between the two integers the user inputs (which i know is counterproductive). my code is a little different (and long).. took my whole Sunday but thank god it works :biggrin:

VBA Code:
Sub PrintPrimeNumbers()
    Dim input1 As String, input2 As String
    Dim num1 As Integer, num2 As Integer
    Dim smallnum As Integer, bignum As Integer
    Dim i As Integer, j As Integer
    Dim ISPRIME As Boolean
   
    input1 = InputBox(Prompt:="Please Enter The First Integer", Title:="Input Integer 1")
    If IsNumeric(input1) Then
        num1 = CDbl(input1)
    Else
        num1 = 0
    End If
   
    input2 = InputBox(Prompt:="Please Enter The Second Integer", Title:="Input Integer 2")
    If IsNumeric(input2) Then
        num2 = CDbl(input2)
    Else
        num2 = 0
    End If
   
    If num1 < num2 Then
        smallnum = num1
        bignum = num2
    Else
        smallnum = num2
        bignum = num1
    End If
   
    For i = smallnum To bignum Step 1
    For j = 2 To i - 1
        If i Mod j = 0 Then
        ISPRIME = False
    Exit For
    Else
        ISPRIME = True
    End If
    Next
    If ISPRIME = True Then
        MsgBox i & " is a Prime Number"
    End If
        
    Next i
End Sub
 
Upvote 0
Rich (BB code):
    For j = 2 To i - 1

        If i Mod j = 0 Then

        ISPRIME = False

    Exit For

You do not have to check j for all values 2 to i-1. You only have to go to i/2. No number is evenly divisible by more than half of itself.

Also you should not be asking people to do your assignments for you. Getting help is one thing but don't just put it out there and ask for a solution.
 
Upvote 0
@nhakimmm, your solution has many potential problems due to the fact that you have excluded error checking from your code. ;)
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,023
Latest member
alabaz

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