+ Reply to Thread
Results 1 to 5 of 5

Looping and For Next within an IF

  1. #1
    Registered User
    Join Date
    08-07-2022
    Location
    Louisiana, USA
    MS-Off Ver
    365
    Posts
    8

    Looping and For Next within an IF

    I have the For Next statement working to sum all numbers up to value of n, but when I try and accomplish the same thing with Do While Loop and Do Until Loops I get iteration errors that crash my excel. I want the code to continue to work for the ElseIf of summing only even numbers to n and only odd numbers to n, but I'm wondering if it is an issue with the way I typed it or if these loops can't go within the IF statement.

    If optsa.Value = True Then
    Cells(6, 4) = "Sum All"
    sum = 0
    i = 0
    For i = 0 To n Step 1
    sum = sum + i
    Cells(8 + i, 2) = i
    Cells(8 + i, 3) = sum
    Next i
    lblfn.Caption = sum
    'Do While Sum All
    sum = 0
    i = 0
    step = 0
    Do While i <= n
    sum = sum + i
    step = step + 1
    Cells(8 + step, 4) = sum
    i = i + 1
    Loop
    lbldw.Caption = sum
    'Do Until Sum All
    sum = 0
    i = 0
    step = 0
    Do Until i > n
    sum = sum + i
    step = step + 1
    Cells(8 + step, 5) = sum
    i = i + 1
    Loop
    lbldu.Caption = sum

    'ElseIf etc
    End If

  2. #2
    Valued Forum Contributor
    Join Date
    08-08-2022
    Location
    Buenos Aires
    MS-Off Ver
    Excel 2019
    Posts
    1,777

    Re: Looping and For Next within an IF

    Hello
    We should see your workbook (you forgot to upload it to the Forum).
    But it seems to me that it is not necessary to do iterations to get what you are looking for.

  3. #3
    Registered User
    Join Date
    08-07-2022
    Location
    Louisiana, USA
    MS-Off Ver
    365
    Posts
    8

    Re: Looping and For Next within an IF

    B1-TS.xlsm
    The assignment is to use all three functions For Next, Do While, and Do Until. I want to put it in an if statement that will decide if it is sum all numbers, sum all even numbers, or sum all odd numbers to a nth number. At the moment the file is crashing because the looping doesn't stop after reaching n. The For Next is working. I also have only been trying to make the sum all work so I am not sure the even and odd codes are correct for those.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Looping and For Next within an IF


    Hello,

    some school assigment for the slowest way which has no sense according to Excel design …

  5. #5
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,909

    Re: Looping and For Next within an IF

    Something like this perhaps


    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Please help Looping In VBA
    By shelim481 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-31-2018, 04:57 AM
  2. looping :(
    By cordoda in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-04-2018, 12:11 PM
  3. Looping through each row
    By dusto in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-07-2014, 09:40 PM
  4. Looping
    By itselflearn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-10-2013, 12:27 PM
  5. Do Until Looping (not looping through all other columns)
    By orle8050 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-11-2013, 10:37 AM
  6. Is this possible without looping?
    By Frustrated1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-16-2010, 09:55 AM
  7. Help with looping
    By dok112 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-01-2005, 01:09 PM

Tags for this Thread

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