-1

I'm learning VBA and I tried nesting a do-while-loop inside a for-loop, but the outer for-loop doesn't seem to have an effect (whole Sub only runs once), although its runs without errors. Could someone please assist me? :)

My code:

Sub DoWhileDemo()
    Dim Cnt As Integer
    Cells(1, 1).Activate 'just meant as an in-case

    For Cnt = 1 To 100 Step 1
        Do While ActiveCell.Value <> Empty
            ActiveCell.Value = ActiveCell.Value * 2
            ActiveCell.Offset(1, 0).Select
        Loop
    Next Cnt
End Sub
8
  • youre going to be more descriptive about the problem, please tell us more about what you would expect to happen and what actually happens and then we can help you Sep 21, 2017 at 7:57
  • Have you put a break point and check ActiveCell is not empty? And should it be ActiveCell.Offset(1, 0).Activate? If not, it just keep checking and working on A1
    – Prisoner
    Sep 21, 2017 at 7:58
  • @CodeWarrior I would expect the For loop to have an effect (ie, run the do..while loop 100 times instead of just 1 as I mentioned) and what is actually happening is that the do..while loop is only run once, as mentioned. If you look at the code (and run it), its like the For loop wasn't there. Sep 21, 2017 at 8:00
  • @Prisoner no no, its going over all my cells that aren't empty. I would have noticed :p The Do..while loop is working fine, but I would like it to be run 100 times. So that if I start with A1 value of 55, I should be getting a bigger number than 110 after running the sub Sep 21, 2017 at 8:03
  • After the first iteration where you set a value in the ActiveCell (which is not a recommended way to do this), you set the cell in the next row. If that cell is empty, it will never trigger your Do While loop and hence nothing will ever get done. Move your ActiveCell.Offset(1, 0).Select outside your Do While loop. That might help
    – Zac
    Sep 21, 2017 at 8:04

2 Answers 2

2

When cnt=1 the do while loop ends when ActiveCell is empty. At this moment the ActiveCell is empty, so for cnt=2 to 100, the do while loop is never executed.

move the cells(1,1).activate after the for instruction

2
  • To elaborate. If you want to perform the stepwise multiplication (i.e. run the do while loop mulitple times), you need to go back to the first cell, in the begining of each iteration of the for loop. Sep 21, 2017 at 8:10
  • @Noceo yes, you are correct :) I figured it out right before your comment but still thanks for the input :) Sep 21, 2017 at 8:15
1

Nop, i just needed to put Cells(1,1).Activate as first line in the For loop to make sure it kept being brought back to "start" so that the while loop could then run through, I fixed the code. Issue is resolved :)

Sub DoWhileDemo()
    Dim Cnt As Integer
    For Cnt = 1 To 100 Step 1
        Cells(1, 1).Activate
        Do While ActiveCell.Value <> Empty
            ActiveCell.Value = ActiveCell.Value * 2
            ActiveCell.Offset(1, 0).Select
        Loop
    Next Cnt
End Sub
1
  • a little confuse ... what the code ActiveCell.Offset do? And you can mark as answer later, seems 1 or 2 days.
    – Prisoner
    Sep 21, 2017 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.