This faintly reminds me of a non-starter test I did years ago to re-draw Excel charts into Illustrator.
I believe your problem lies in the changing amount of textFrames. You see, when you access these pageItems from the layer instead of the document object, it's only going to go 1-level down. So, if you move some of the textframes you've iterated through into a group, it's no longer part of the "layer" object even though it's really in the layer.
Your solution is simply to go backwards, change the for-loop to be (i = layer.textFrames.length -1; i > -1; i--)
You can also see the screenshot for a visual example.
Additionally, the textFrames/pageItems inside of other items inside of layers still have a handy "layer" property which will provide a reference to the parent layer. This way you can at least have some mechanism for going through the document's textFrames and acting on them based on their parent layer name - just fyi.
Thanks for that, it works great and is obvious when you point it out. Brilliant cheers
yay... now if you mark this a correct answer, other people can easily find this thread to get their solution
hmm, don't seem to be able to do that. Can't see anything under actions and there doesn't seem to be anything that says mark as correct or any ticks?
Ohh, you never marked this as a question to begin with - that's it.