VBA Excel - Do While Loop

Anonymous
2019-02-11T18:40:18+00:00

Hey everyone so this is a part of a homework, can anytone explain what I am doing wrong? Im beginner and dont quite understand loops just yet.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2019-02-11T20:54:23+00:00

    Aside from your programming errors, the values in range A1:A10 are not going to change, so your loop would never end.

    You need to increment something that will change, either an index or a cell reference within your loop. Also, While should go first, and it needs a Wend, along the lines of

    Sub TestMacro()

    While Range("A1").Value < 11

        Range("A1").Value = Range("A1").Value + 1

    Wend

    End Sub

    See how the value is being changed? That allows the code to get out of the loop

    For an index, you can either use While or a For loop - the for loop is much preferred since you don't need to change the index with a separate line - also, you need a conditional (the problem statement use IF meaning sometimes you should and sometimes you should not make the number bold and italic. 

    Using While:

    Row = 1

    While Row <=10

        'Code to do something using Row, like  Cells(Row,"A").Value

        Row = Row +1  'This will break you out of the loop

    Wend

    Using For:

    For Row = 1 To 10

        'Code to do something using Row, like  Cells(Row,"A").Value

    Next Row

    Using Selection:

    Range("A1").Select

    While Selection.Row <= 10

        'Code to do something using Selection

        Selection.Offset(1,0).Select

    Wend

    0 comments No comments
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more