Hi, I'm a beginner at VBA and I'm trying to make a macro that lists all the prime numbers from 3 to 97 (for example).
I tried this but it didn't work well:
---------------------------------------------
Sub abc()
Dim x As Integer
Dim i As Integer
Dim k As Integer
i = 2
k = 0
For x = 3 To 97
Do While i < x
If x Mod i = 0 Then
k = k + 1
End If
If k = 0 Then
Cells(x, 1) = x
End If
i = i + 1
Loop
Next x
End Sub
---------------------------------
My idea was to run a "do while" command for each of the numbers in the interval [3,97],
testing if it's divisible by any number from 2 up to itself minus 1. If it is, then it's not prime, and
an indicator (k) should become greater than 0. If k keeps being 0, this means the number is prime, so
it should be printed on the cells.
Can anybody tell me what went wrong?
Bookmarks