Issues with the vba skipping the for loop

  • Hi,

    I am still new to VBA currently doing some programming but vba skips the entire for loop and ends the program whenever i try to use step into function for testing


  • Hi Rookie,


    Don't forget to read the rules and wrap your code.


    The loop you have:

    Code
    1. Do While WorksheetFunction.CountA(Workflow.Range(Cells(R, C1), Cells(R, C2))) <> 0
    2. R = R + 1
    3. Loop

    is infinite once initiated, there is nothing within the loop to allow it to stop if the range is not = 0 at the start.


    i would recommend having a look at "select case" rather than the series of else if statements you are using (https://docs.microsoft.com/en-…elp/select-case-statement)


    There is also no reason to select any range in your code.

  • I think the code should look something like this:



    I see how the loop could work now assuming it will always reach a zero value, but it has the potential to be infinite so I think should be replaced. If you cannot get the above to work upload an example sheet and I will have a look.

  • If your For loop is being skipped altogether, it suggests that x is less than 2.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • That's not actually correct. R changes within the loop so it will test each row until it finds a blank one (or errors due to running out of rows). It is however badly written because the Cells calls are not qualified with the same worksheet as the Range call.

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why