What's wrong with VBA code and please tell me the correction required?

Yashraj Jadhao

New Member
Joined
Nov 14, 2021
Messages
9
Office Version
  1. 2021
Platform
  1. MacOS
What's wrong with this code, whenever I input any number it always says not prime, I want it to tell whether its prime or not prime?

Sub Button1_Click()
Dim number As Long
Dim counter As Integer
Dim remainder As Integer
remainder = 0
remainder = Cells(3, 2)
number = Cells(2, 2)
Cells(2, 2) = InputBox("Insert positive integer")
Do While Cells(2, 2).Value < 0
MsgBox ("Your number was not a positive integer")
Cells(2, 2) = InputBox("Insert positive integer")

Loop
For counter = 1 To number / 2
Cells(3, 2) = number Mod counter
Cells(3, 2).Value = 0
Next counter

If remainder = 0 Then
MsgBox ("Your numberis not prime")
Else
MsgBox ("Your number is prime")
End If
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
VBA Code:
Sub Button1_Click()
  Dim Prime As String, i As Integer, Number As Variant
  Prime = "Prime"
  Number = InputBox("Input any positive Integer:")
  If IsNumeric(Number) = False Then
    MsgBox "No Number!"
    Exit Sub
  End If
  If Number <> CInt(Number) Or Number < 0 Then
    MsgBox "No positive Integer!"
    Exit Sub
  End If
  For i = 2 To Number / 2
    If Number Mod i = 0 Then
      Prime = "no Prime"
      Exit For
    End If
  Next i
  MsgBox Number & " is " & Prime
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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