Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sequential numbers not continuing

I have a sequence of 13 numbers which represent the maximum value of sequential numbers. However, the code below only generates the first set of numbers and doesn’t continue for some reason.

For x = 1 To depthRngLastRow
maxcnt = workingWs.Range("H" & x).Value
    For i = 1 To maxcnt
        stageWs.Range("L" & i).Value = i
    Next i
Next x
Debug.Print depthRngLastRow

The first maxcnt = 122. So in column L in sheet stageWs, I get numbers from 1 to 122. This is expected for x = 1. For x =2, maxcnt = 8. I expect the numbering to restart from 1 to 8…but this is not the case.

What am I doing wrong?
Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re overwriting values:

For i = 1 To maxcnt
    stageWs.Range("L" & i).Value = i
Next i

This writes rows 1 to 122 on the first iteration of the outer loop, then overwrites rows 1 to 8 on the second iteration of the outer loop.

An alternative:

For i = 1 To maxcnt
    Dim count As Long
    count = count + 1
    stageWs.Range("L" & count).Value = i
Next i
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading