Home > Back-end >  sequential numbers not continuing
sequential numbers not continuing

Time:09-17

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!

CodePudding user response:

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
  • Related