Results 1 to 9 of 9

Prime numbers macro

Threaded View

kaue Prime numbers macro 10-13-2014, 02:08 PM
zbor Re: Prime numbers macro 10-13-2014, 02:12 PM
zbor Re: Prime numbers macro 10-13-2014, 03:23 PM
zbor Re: Prime numbers macro 10-13-2014, 03:11 PM
MrShorty Re: Prime numbers macro 10-13-2014, 03:35 PM
zbor Re: Prime numbers macro 10-13-2014, 03:43 PM
kalak Re: Prime numbers macro 10-15-2014, 05:54 AM
alee001 Re: Prime numbers macro 08-16-2020, 12:34 PM
alee001 Re: Prime numbers macro 08-17-2020, 09:08 AM
  1. #1
    Registered User
    Join Date
    10-13-2014
    Location
    Sao Paulo, Brazil
    MS-Off Ver
    2013
    Posts
    1

    Prime numbers macro

    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?
    Last edited by zbor; 10-13-2014 at 03:05 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. macro to print prime numbers from 1 to 200
    By rarementality in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-02-2014, 02:53 AM
  2. How to get functions about prime numbers in Excel 2013?
    By Terminatrix in forum Excel General
    Replies: 1
    Last Post: 04-21-2014, 11:49 AM
  3. Highlight all prime numbers in column
    By bluecat2013 in forum Excel General
    Replies: 3
    Last Post: 02-21-2014, 01:19 PM
  4. Prime numbers
    By Gearcutter in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 03-08-2007, 09:03 AM
  5. [SOLVED] Prime Numbers
    By Flyone in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-02-2005, 10:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1