Consulting

Results 1 to 5 of 5

Thread: vba for next loop

  1. #1
    Banned VBAX Regular
    Joined
    Jul 2018
    Posts
    9
    Location

    Question vba for next loop

    Dim num as integer
    sub mac1()
    For num = 1 to 50
    A = num * 2
    cells (1,1) = A
    next num
    end sub

    If i run this code cell cell(1,1) displays 100 i.e 50 * 2 instead of lowest num no i.e 1 * 2 which is equal to 2. Even if i declare A as integer problem persists please help.

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,050
    Location
    Have you tried "stepping" through the range 1 to 50?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Banned VBAX Regular
    Joined
    Jul 2018
    Posts
    9
    Location
    No. how to do that ?

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    It looks like you're replacing the A1 50 times (2, 4, 6, 8, …., 98, 100), and at the end, only the final value (100) is displayed

    Example:

    Dim num as integer sub mac1() For num = 1 to 50 A = num * 2 cells (num,1) = A next num end sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Cross-posted at: http://www.msofficeforums.com/word-v...next-loop.html
    Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq...._new_faq_item3
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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