At first glance it looks to me like, you need to reset 'p' to when you drop out of the 'Do Until loop'. On the Next 'For Loop' 'p' is already equal to NumericUpDown.Value and so won't perform the Do Until.

Where you have source = nothing, add on a new line p = 0 and see if that works


Cheers

Dan